Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dependencies: sudo gem install httparty pp
- require 'httparty'
- require 'pp'
- require 'net/http'
- require 'uri'
- require 'dotenv/load'
- require 'httmultiparty'
- module PeerTube
- class Client
- def initialize
- # Assign data from the .env file to instance variables
- @domain = ENV['DOMAIN']
- @username = ENV['USERNAME']
- @password = ENV['PASSWORD']
- @url = "#{@domain}/api/v1/"
- # Initialization routine:
- puts "Authenticating..."
- get_client_tokens
- end
- def get_client_tokens
- path = "#{@url}oauth-clients/local" # build the URL to the API endpoint
- response = HTTParty.get(path) # send a 'get' request to that URL
- data = response.parsed_response # parse the response into usable data
- pp data # just prints the tokens to console
- get_client_bearer(data) # triggers the function below and passes it the above response's data
- end
- def get_client_bearer(client_keys)
- @client_id = client_keys["client_id"] # add the response's client_id as a local variable
- @client_secret = client_keys["client_secret"]
- path = "#{@url}users/token"
- options = { :body =>
- {
- "client_id" => @client_id,
- "client_secret" => @client_secret,
- "grant_type" => "password",
- "password" => ENV['PASSWORD'], # load the PASSWORD variable from the .env file
- "response_type" => "code",
- "username" => ENV['USERNAME']
- }
- }
- response = HTTParty.post(path, options)
- data = response.parsed_response # retrieve the bearer token...
- @bearer = data["access_token"] # ... and save it to a local variable
- end
- def get_a_channel_id
- path = "#{@url}/accounts/#{ENV['USERNAME']}"
- response = HTTParty.get(path)
- id = response.parsed_response["uuid"]
- end
- def get_own_user_data
- path = "#{@url}/users/me"
- data = { :headers => { "Authorization" => "Bearer #{@bearer}" },
- :body => {} }
- response = HTTParty.get(path, data)
- end
- def get_own_channel_id
- id = get_own_user_data
- id["id"]
- end
- def upload_video(filename, title)
- path = "#{@url}/videos/upload"
- id = get_own_channel_id
- auth = "Bearer #{@bearer}"
- options = {
- "videofile" => File.open(filename),
- "channelId" => "596242b9-519c-4c7d-918a-506764704f5a",
- "name" => title
- }
- response = HTTParty.post(path, :headers => {"Authorization" => auth, "Content-Type" => "multipart/form-data"}, :body => options)
- pp response.parsed_response
- end
- end
- end
- ### COMMANDS
- ## make a new object to interface with PeerTube
- peertube = PeerTube::Client.new
- ## upload the file specified at the first argument, and name it the second argument:
- peertube.upload_video("/home/benjamin/Downloads/1.webm", "API Test at #{Time.now}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement