#!/usr/bin/env ruby # encoding: utf-8 class AyaDN # experimenting without curl # def ayadnFileUpload(file_name) # # puts "\nUploading ".green + file_name.brown + "\n" # # response = @api.createIncompleteFileUpload(file_name) # # puts response.inspect #SUCCESS # # THEN multipart => #FAIL # end # def ayadnDeactivateChannel(channel_id) # resp = @api.deactivateChannel(channel_id) # puts resp # end ### experiment ### not DRY at all, this is ok, chill out # def ayadnRecord(item) # # first create with curl -i -H 'Authorization: BEARER xxx' "https://stream-channel.app.net/stream/user?auto_delete=1&include_annotations=1" # # it stays open and returns a stream_id in the headers # # TODO: replace curl with a good connection system with HTTP or Rest-Client # command = "sleep 1; curl -i -H 'Authorization: BEARER #{@token}' 'https://stream-channel.app.net/stream/user?auto_delete=1&include_annotations=1'" # pid = Process.spawn(command) # Process.detach(pid) # puts "Enter stream id: " # stream_id = STDIN.gets.chomp # last_page_id = nil # start = Time.now # case item # when "global" # works :) # @url = "https://alpha-api.app.net/stream/0/posts/stream/global?connection_id=#{stream_id}&since_id=#{last_page_id}" # when "unified" # doesn't work, have to implement some sort of real keep-alive connection # @url = "https://alpha-api.app.net/stream/0/posts/stream/unified?connection_id=#{stream_id}&since_id=#{last_page_id}&include_directed_posts=1" # end # puts "\nRecording stream in #{$tools.ayadn_configuration[:files_path]}/rec-#{item}.json\n\n" # uri = URI.parse(@url) # https = Net::HTTP.new(uri.host,uri.port) # https.use_ssl = true # https.verify_mode = OpenSSL::SSL::VERIFY_NONE # request = Net::HTTP::Get.new(uri.request_uri) # request["Authorization"] = "Bearer #{@token}" # request["Content-Type"] = "application/json" # response = https.request(request) # @hash = JSON.parse(response.body) # big_stream = @hash['data'] # f = File.new($tools.ayadn_configuration[:files_path] + "/rec-#{item}.json", 'w') # f.puts(big_stream.to_json) # f.close # stream, last_page_id = completeStream # displayScrollStream(stream) # number_of_connections = 1 # file_operations_timer = 0 # loop do # begin # case item # when "global" # @url = "https://alpha-api.app.net/stream/0/posts/stream/global?connection_id=#{stream_id}&since_id=#{last_page_id}" # when "unified" # @url = "https://alpha-api.app.net/stream/0/posts/stream/unified?connection_id=#{stream_id}&since_id=#{last_page_id}&include_directed_posts=1" # end # uri = URI.parse(@url) # request = Net::HTTP::Get.new(uri.request_uri) # request["Authorization"] = "Bearer #{@token}" # request["Content-Type"] = "application/json" # before_request_id = last_page_id # response = https.request(request) # @hash = JSON.parse(response.body) # stream, last_page_id = completeStream # displayScrollStream(stream) # if last_page_id == nil # last_page_id = before_request_id # sleep 0.5 # trying to play nice with the API limits # number_of_connections += 1 # next # end # # don't let it run for days, it will eat your RAM # # + the simple file dump isn't ok in the long run # big_stream << @hash['data'] # number_of_connections += 1 # file_operations_timer += 1 # sleep 0.2 # trying to play nice with the API limits # if file_operations_timer == 10 # puts "\nRecording stream in #{$tools.ayadn_configuration[:files_path]}/rec-#{item}.json\n\n".green # #big_json = JSON.parse(IO.read($tools.ayadn_configuration[:files_path] + "/rec-#{item}.json")) # f = File.new($tools.ayadn_configuration[:files_path] + "/rec-#{item}.json", 'w') # f.puts(big_stream.to_json) # f.close # file_operations_timer = 0 # end # rescue Exception => e # puts e.inspect # puts e.to_s # finish = Time.now # elapsed = finish.to_f - start.to_f # mins, secs = elapsed.divmod 60.0 # puts "\nRequests: #{number_of_connections}\n" # puts "Elapsed time (min:secs.msecs): " # puts("%3d:%04.2f"%[mins.to_i, secs]) # puts "\nStream recorded in #{$tools.ayadn_configuration[:files_path]}/rec-#{item}.json" # exit # end # end # end end