Advertisement
Guest User

peertubeclient.rb

a guest
Aug 3rd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.12 KB | None | 0 0
  1. # dependencies: sudo gem install httparty pp
  2.  
  3. require 'httparty'
  4. require 'pp'
  5. require 'net/http'
  6. require 'uri'
  7. require 'dotenv/load'
  8. require 'httmultiparty'
  9.  
  10. module PeerTube
  11.    
  12.   class Client
  13.    
  14.     def initialize
  15.      
  16.       # Assign data from the .env file to instance variables
  17.      
  18.       @domain = ENV['DOMAIN']
  19.       @username = ENV['USERNAME']
  20.       @password = ENV['PASSWORD']
  21.       @url = "#{@domain}/api/v1/"
  22.      
  23.       # Initialization routine:
  24.      
  25.       puts "Authenticating..."
  26.      
  27.       get_client_tokens
  28.      
  29.     end
  30.    
  31.     def get_client_tokens
  32.        
  33.       path = "#{@url}oauth-clients/local" # build the URL to the API endpoint
  34.       response = HTTParty.get(path) # send a 'get' request to that URL
  35.       data = response.parsed_response # parse the response into usable data
  36.       pp data # just prints the tokens to console
  37.       get_client_bearer(data) # triggers the function below and passes it the above response's data
  38.      
  39.     end
  40.  
  41.     def get_client_bearer(client_keys)
  42.        
  43.       @client_id = client_keys["client_id"] # add the response's client_id as a local variable
  44.       @client_secret = client_keys["client_secret"]
  45.       path = "#{@url}users/token"
  46.      
  47.       options = { :body =>
  48.           {
  49.         "client_id" => @client_id,
  50.         "client_secret" => @client_secret,
  51.         "grant_type" => "password",
  52.         "password" => ENV['PASSWORD'], # load the PASSWORD variable from the .env file
  53.         "response_type" => "code",
  54.         "username" => ENV['USERNAME']
  55.           }
  56.       }
  57.  
  58.       response = HTTParty.post(path, options)
  59.       data = response.parsed_response # retrieve the bearer token...
  60.       @bearer = data["access_token"]  # ... and save it to a local variable
  61.      
  62.     end
  63.  
  64.     def get_a_channel_id
  65.      
  66.       path = "#{@url}/accounts/#{ENV['USERNAME']}"
  67.       response = HTTParty.get(path)
  68.       id = response.parsed_response["uuid"]
  69.      
  70.     end
  71.  
  72.     def get_own_user_data
  73.      
  74.         path = "#{@url}/users/me"
  75.         data = { :headers => { "Authorization" => "Bearer #{@bearer}" },
  76.                  :body => {} }
  77.         response = HTTParty.get(path, data)
  78.        
  79.     end
  80.  
  81.     def get_own_channel_id
  82.      
  83.         id = get_own_user_data
  84.         id["id"]
  85.        
  86.     end
  87.  
  88.     def upload_video(filename, title)
  89.        
  90.         path = "#{@url}/videos/upload"
  91.         id = get_own_channel_id
  92.         auth = "Bearer #{@bearer}"
  93.        
  94.         options = {
  95.             "videofile" => File.open(filename),
  96.             "channelId" => "596242b9-519c-4c7d-918a-506764704f5a",
  97.             "name" => title
  98.         }
  99.        
  100.         response = HTTParty.post(path, :headers => {"Authorization" => auth, "Content-Type" => "multipart/form-data"}, :body => options)
  101.         pp response.parsed_response
  102.     end
  103.    
  104.   end
  105.  
  106. end
  107.  
  108.  
  109. ### COMMANDS
  110.  
  111. ## make a new object to interface with PeerTube
  112.  
  113. peertube = PeerTube::Client.new
  114.  
  115. ## upload the file specified at the first argument, and name it the second argument:
  116.  
  117. peertube.upload_video("/home/benjamin/Downloads/1.webm", "API Test at #{Time.now}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement