aboutsummaryrefslogtreecommitdiffstats
path: root/client/worker.rb
blob: 2d162e29cb8fdf13303e8714361b3aebb170c0c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
61
62
63
64
65
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-
require "em-twitter"
require "yajl"
require "msgpack"
require "./settings"
require "./logger"

class Worker
  class DBProxyClient < EM::Connection
    def send_object(data)
      send_data(data.to_msgpack)
    end

    def initialize
      @clients = {}
      @pac = MessagePack::Unpacker.new
      @excludes = []
    end

    def escape_colon(str); str.gsub(":", "%3A").gsub("<", "%3C").gsub(">", "%3E"); end

    def format_text(status)
      chars = status[:text].to_s.split(//)

      entities = status[:entities].map{|k, v| v.map{|n| n.update(:type => k)}}.flatten.sort_by{|entity| entity[:indices].first}

      result = []
      last_index = entities.inject(0) do |last_index, entity|
        result << chars[last_index...entity[:indices].first]
        case entity[:type]
        when :urls, :media
          result << "<url:#{escape_colon(entity[:expanded_url])}:#{escape_colon(entity[:display_url])}>"
        when :hashtags
          result << "<hashtag:#{escape_colon(entity[:text])}>"
        when :user_mentions
          result << "<mention:#{escape_colon(entity[:screen_name])}>"
        when :symbols
          result << "<symbol:#{escape_colon(entity[:text])}>"
        end

        entity[:indices].last
      end
      result << chars[last_index..-1]

      result.flatten.join
    end

    def format_source(status)
      if status[:source].index("<a")
        url = status[:source].scan(/href="(.+?)"/).flatten.first
        name = status[:source].scan(/>(.+?)</).flatten.first
        "<url:#{escape_colon(url)}:#{escape_colon(name)}>"
      else
        status[:source]
      end
    end

    def favbot?(source, target_object)
      favs = source[:favourites_count]
      tweets = source[:statuses_count]
      bio = source[:description]
      name = source[:name]
      # えたふぉ
      if (Time.now - Time.parse(target_object[:created_at])) < 2
        case
        when
          (tweets < 1000 && favs > tweets * 2),
          (source[:friends_count] > source[:followers_count] * 10 && favs > tweets)
          return true
        end
      end
      if
        case
        when
          favs > tweets * 30,
          (tweets < 100 && favs > tweets * 2),
          (source[:default_profile_image] == true && favs > tweets * 2)
          return true
        end
      end

      false
    end

    def bot?(status)
      sources = [
        /^<a href="http:\/\/twittbot\.net\/" rel="nofollow">twittbot\.net<\/a>$/,
        /^<a href="http:\/\/botmaker\.dplays\.net\/" rel="nofollow">BotMaker<\/a>$/,
        /^<a href="http:\/\/makebot\.sh\/" rel="nofollow">makebot\.sh( [0-9])?<\/a>$/,
        /^<a href="http:\/\/lbattery\.dip\.jp\/twihaialert\/" rel="nofollow">ツイ廃あらーと<\/a>$/,
        /^<a href="http:\/\/app\.xmgn\.com\/tweetcounter\/" rel="nofollow">ツイート数カウントくん<\/a>$/,
        /^<a href="http:\/\/gumu-lab\.com\/replychecker\/" rel="nofollow">リプライ数チェッカ<\/a>$/,
        /^<a href="http:\/\/bit.ly\/SiHFe6" rel="nofollow">劣化コピー<\/a>$/,
      ]
      sources.any?{|r| r =~ status[:source]}
    end

    def send_spam(user)
      send_object({
        type: "spam",
        id: user[:id]
      })
    end

    def send_user(user)
      send_object({
        :type => "user",
        :id => user[:id],
        :screen_name => user[:screen_name],
        :name => user[:name],
        :profile_image_url => user[:profile_image_url_https],
        :protected => user[:protected]
      })
    end

    def send_tweet(status)
      unless bot?(status)
        send_user(status[:user])
        send_object({
          :type => "tweet",
          :id => status[:id],
          :text => format_text(status),
          :source => format_source(status),
          :tweeted_at => status[:created_at],
          :user_id => status[:user][:id]
        })
      end
    end

    def send_favorite(source, target_object)
      if @excludes.include?(source[:id])
        # ignored
      else
        if favbot?(source, target_object)
          # ignored
          send_spam(source)
          $logger.info("Add #{source[:screen_name]}(##{source[:id]}) to ignore list")
          @excludes << source[:id]
        elsif !bot?(target_object)
          send_tweet(target_object)
          send_user(source)
          send_object({
            :type => "favorite",
            :tweet_id => target_object[:id],
            :user_id => source[:id]
          })
        end
      end
    end

    def send_unfavorite(source, target_object)
      send_object({
        :type => "delete",
        :tweet_id => target_object[:id],
        :user_id => source[:id]
      })
    end

    def send_retweet(status)
      unless bot?(status[:retweeted_status])
        send_tweet(status[:retweeted_status])
        send_user(status[:user])
        send_object({
          :type => "retweet",
          :id => status[:id],
          :tweet_id => status[:retweeted_status][:id],
          :user_id => status[:user][:id]
        })
      end
    end

    def send_delete(deleted_status_id, deleted_user_id)
      send_object({
        :type => "delete",
        :id => deleted_status_id,
        :user_id => deleted_user_id
      })
    end
    ### end - send to receiver

    def receive_account(msg)
      user_id = msg["user_id"]
      account_id = msg["id"]

      conopts = {:host => "userstream.twitter.com",
                 :path => "/1.1/user.json",
                 :oauth => {
                   :consumer_key => Settings.consumer[msg["consumer_version"].to_i].key,
                   :consumer_secret => Settings.consumer[msg["consumer_version"].to_i].secret,
                   :token => msg["oauth_token"],
                   :token_secret => msg["oauth_token_secret"]},
                 :method => "GET"}
      if @clients[account_id]
        unless @clients[account_id].options[:oauth][:token] == conopts[:oauth][:token]
          @clients[account_id].connection.update(conopts)
          $logger.info("Updated(##{account_id}/#{user_id}/#{msg["consumer_version"].to_i})")
        else
          $logger.info("Not Updated(##{account_id}/#{user_id}/#{msg["consumer_version"].to_i})")
        end
        return
      end
      @clients[account_id] = client = EM::Twitter::Client.new(conopts)

      client.on_error do |message|
        $logger.warn("Unknown Error(##{account_id}/#{user_id}): #{message}")
      end

      client.on_unauthorized do
        # revoked?
        $logger.warn("Unauthorized(##{account_id}/#{user_id})")
        out = {:type => "unauthorized", :user_id => user_id, :id => account_id}
        send_object(out)
        client.connection.stop
        @clients.delete(account_id)
      end

      client.on_enhance_your_calm do
        # limit?
        $logger.warn("Enhance your calm(##{account_id}/#{user_id})")
      end

      client.on_no_data_received do
        # (?)
        $logger.warn("No data received(##{account_id}/#{user_id})")
        client.close_connection
      end

      client.each do |chunk|
        begin
          hash = Yajl::Parser.parse(chunk, :symbolize_keys => true)
        rescue Yajl::ParseError
          $logger.warn("Unexpected chunk(##{account_id}/#{user_id}): #{chunk}")
          next
        end

        if hash[:warning]
          $logger.info("Stall warning(##{account_id}/#{user_id}): #{hash[:warning]}")
        elsif hash[:delete] && hash[:delete][:status]
          deleted_status_id = hash[:delete][:status][:id]
          deleted_user_id = hash[:delete][:status][:user_id]
          send_delete(deleted_status_id, deleted_user_id)
          $logger.debug("Delete(##{account_id}/#{user_id}): #{deleted_user_id} => #{deleted_status_id}")
        elsif hash[:limit]
          $logger.warn("UserStreams Limit Event(##{account_id}/#{user_id}): #{hash[:limit][:track]}")
        elsif hash[:event]
          case hash[:event]
          when "favorite"
            source = hash[:source]
            target_object = hash[:target_object]
            if target_object && target_object[:user] &&
              (!target_object[:user][:protected] ||
                target_object[:user][:id] == user_id)
              send_favorite(source, target_object)
              $logger.debug("Favorite(##{account_id}/#{user_id}): #{source[:screen_name]} => #{target_object[:id]}")
            end
          when "unfavorite"
            send_unfavorite(hash[:source], hash[:target_object])
            $logger.debug("Unfavorite(##{account_id}/#{user_id}): #{hash[:source][:screen_name]} => #{hash[:target_object][:id]}")
          end
        elsif hash[:user]
          # tweet
          if hash[:retweeted_status]
            if hash[:retweeted_status][:user][:id] == user_id || hash[:user][:id] == user_id
              send_retweet(hash)
              $logger.debug("Retweet(##{account_id}/#{user_id}): #{hash[:user][:screen_name]} => #{hash[:retweeted_status][:id]}")
            end
          elsif hash[:user][:id] == user_id
            # update: exclude not favorited tweet
            send_tweet(hash)
            $logger.debug("Tweet(##{account_id}/#{user_id}): #{hash[:user][:screen_name]} => #{hash[:id]}")
          end
        elsif hash[:friends]
          # monyo
        else
          $logger.debug("??(##{account_id}/#{user_id})")
        end
      end

      client.on_reconnect do |timeout, retries|
        $logger.warn("Reconnected(##{account_id}/#{user_id}): #{retries}")
      end

      client.on_max_reconnects do |timeout, retries|
        $logger.warn("Max reconnects(##{account_id}/#{user_id}): #{retries}")
        client.connection.stop
        @clients.delete(account_id)
      end

      client.connect
      $logger.info("Connected(##{account_id}/#{user_id}/#{msg["consumer_version"].to_i})")
    end

    def post_init
      out = {:type => "init",
             :secret_key => Settings.secret_key,
             :worker_number => Settings.worker_number}
      send_object(out)
    end

    def unbind
      $logger.info("Connection closed")
      EM.add_timer(10) do
        reconnect(Settings.db_proxy_host, Settings.db_proxy_port)
        post_init
      end
    end

    def receive_data(data)
      @pac.feed_each(data) do |msg|
        unless msg.is_a?(Hash) && msg["type"]
          $logger.warn("Unknown data: #{msg}")
          return
        end

        case msg["type"]
        when "ok"
          $logger.info("ok: #{msg["message"]}")
        when "error"
          $logger.info("error: #{msg["message"]}")
        when "fatal"
          $logger.info("fatal: #{msg["message"]}")
        when "bye"
          $logger.info("bye: #{msg["message"]}")
        when "account"
          begin
            receive_account(msg)
          rescue
            $logger.error($!)
            $logger.error($@)
          end
        else
          $logger.info("Unknown message type: #{msg}")
        end
      end
    end

    def stop_all
      @clients.map{|k, v| v.connection.stop}
      send_object({:type => "quit", :reason => "stop_all"})
    end
  end

  def initialize
    $logger = Aclog::Logger.new(:info)
  end

  def start
    $logger.info("Worker ##{Settings.worker_number} started")
    EM.run do
      connection = EM.connect(Settings.db_proxy_host, Settings.db_proxy_port, DBProxyClient)

      stop = Proc.new do
        connection.stop_all
        EM.stop
      end
      Signal.trap(:INT, &stop)
      Signal.trap(:TERM, &stop)
    end
  end
end