Module: TrovoBot

Defined in:
lib/trovobot/common.rb,
lib/trovobot.rb

Overview

this might some day before a separate gem

Defined Under Namespace

Modules: Common

Constant Summary collapse

SEMAPHORE_NETHTTP =
Async::Semaphore.new 5

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.queueObject

Returns the value of attribute queue.



62
63
64
# File 'lib/trovobot.rb', line 62

def queue
  @queue
end

Class Method Details

.access_tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/trovobot.rb', line 13

def self.access_token
  JSON.load( ( Common::cache_text "tokens.json" do
    NetHTTPUtils.request_data "https://open-api.trovo.live/openplatform/exchangetoken", :POST, :json, header: {
      "Client-ID" => File.read("clientid"),
    }, form: {
      client_secret: File.read("clientsecret"),
      grant_type: "authorization_code",
      code: File.read("auth_code"),
      redirect_uri: "https://trovo.live/",
    }
  end ) ).fetch("access_token")
end

.name_to_id(name) ⇒ Object



57
58
59
# File 'lib/trovobot.rb', line 57

def self.name_to_id name
  request("getusers", {user: [name || fail]})["users"].map{ |_| _["channel_id"] }[0].to_i
end

.request(mtd, form = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trovobot.rb', line 25

def self.request mtd, form = {}
  Sync do
    sleep SEMAPHORE_TIME.async{ [@prev + 0.1 - Time.now, 0].max.tap{ @prev = Time.now } }.wait
    JSON.load( SEMAPHORE_NETHTTP.async do
      NetHTTPUtils.request_data "https://open-api.trovo.live/openplatform/#{mtd}", :POST, :json, header: {
        "Client-ID" => File.read("clientid"),
        "Authorization" => "OAuth #{access_token}",
      }, form: form
    rescue NetHTTPUtils::Error => e
      p e.body
      case e.code
      when 401
        fail unless 11714 == JSON.load(e.body).fetch("status")
        Common::cache_text "tokens.json", true do
          NetHTTPUtils.request_data "https://open-api.trovo.live/openplatform/refreshtoken", :POST, :json, header: {
            "Client-ID" => File.read("clientid"),
          }, form: {
            client_secret: File.read("clientsecret"),
            grant_type: "refresh_token",
            refresh_token: JSON.load(File.read"tokens.json").fetch("refresh_token"),
          }
        end
        sleep 1   # TODO: remove?
        retry
      when 400
        fail unless 20000 == JSON.load(e.body).fetch("status")
        pp [mtd, form]
        raise
      end
    end.wait )
  end
end

.startObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/trovobot.rb', line 66

def self.start
  Thread.new do
    loop do
      content, channel_id = queue.pop
      fail unless channel_id  # omitting (for sending to own channel) isn't implemented yet
      pp request "chat/send", {content: content, channel_id: channel_id}
      sleep 1
    end
  end.abort_on_exception = true
  puts "admin -- #{ARGV[0] || fail}"
  puts "channel -- #{ARGV[1] || fail}"
  require "async/websocket/client"
  require "async/http/endpoint"
  Async do |task|
    puts "debug: Async"
    channel_id = name_to_id ARGV[1]
    chat_token = JSON.load( SEMAPHORE_NETHTTP.async do
      NetHTTPUtils.request_data "https://open-api.trovo.live/openplatform/chat/channel-token/#{channel_id}", header: {
        "Accept" => "application/json",
        "Client-ID" => File.read("clientid"),
      }
    end.wait )["token"]
    loop do
      puts "debug: loop"
      Async::WebSocket::Client.connect(Async::HTTP::Endpoint.parse "wss://open-chat.trovo.live/chat", alpn_protocols: Async::HTTP::Protocol::HTTP11.names) do |connection|
        puts "debug: connection"
        ping_task = task.async do
          loop do
            sleep 30
            connection.write( {type: "PING", nonce: ""} )
            connection.flush
          end
        end
        connection.write( {type: "AUTH", nonce: "", data: {token: chat_token}} )
        # TODO: wrap the file in a semaphore
        require "fileutils"
        FileUtils.touch "processed.jsonl" unless File.exist? "processed.jsonl"
        to_skip = File.read("processed.jsonl").split("\n")
        # trovo may send the same message but with slightly different attributes, such as avatar, so we store only ids
        while msg = connection.read
          # outdated?
          #   for efficiency if I start processing then I immediately stop skipping,
          #   so I don't process until it's type="CHAT" with new id
          # for now we only yield and track CHATs
          # next if msg.fetch(:type) == "PONG"
          next unless msg[:type] == "CHAT"
          new_msgs = msg[:data].fetch(:chats, []).reject{ |_| (to_skip || []).include? _.fetch :message_id }
          next if new_msgs.empty?
          to_skip = nil
          puts "< #{Time.now} #{Base64.strict_encode64 msg.to_s}"
          puts msg.pretty_inspect.gsub(/^/, "< ")
          new_msgs.each do |msg|
            yield msg, channel_id
          rescue
            puts $!.full_message
            queue.push ["error at #{ARGV[1]}: #{$!}, #{$!.backtrace.first}", name_to_id(ARGV[0])]
          else
            File.open("processed.jsonl", "a"){ |_| _.puts msg.fetch :message_id }
          end
        end
      ensure
        ping_task&.stop
      end
    rescue Async::WebSocket::ProtocolError, OpenSSL::SSL::SSLError
      p $!
      sleep 5
      retry
    end
  end # we wanted to rescue Async::* errors to retry this but it appeared to also throw other kinds of exceptions so we just loop
end