File: streaming.rb

package info (click to toggle)
mikutter 3.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,256 kB
  • ctags: 2,165
  • sloc: ruby: 19,079; sh: 205; makefile: 20
file content (89 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (2)
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
#-*- coding: utf-8 -*-
#
# Revolution!
#

require 'timeout'
miquire :core, 'userconfig'

Module.new do

  @thread = nil

  plugin = Plugin::create(:streaming)

  plugin.add_event(:boot){ |service|
    @service = service
    start if UserConfig[:realtime_rewind] }

  UserConfig.connect(:realtime_rewind){ |key, new_val, before_val, id|
    if new_val
      Delayer.new{ self.start }
    else
      @thread.kill if @thread
    end
  }

  def self.start
    unless @thread and @thread.alive?
      @thread = Thread.new{
        while(UserConfig[:realtime_rewind])
          sleep(3)
          catch(:streaming_break){
            start_streaming{ |q|
              throw(:streaming_break) unless(UserConfig[:realtime_rewind])
              Delayer.new(Delayer::NORMAL, q.strip, &method(:trigger_event)) } } end } end end

  def self.trigger_event(query)
    begin
      return nil if not /^\{.*\}$/ === query
      json = JSON.parse(query)
      case
      when json['friends'] then
      when json['event'] == 'favorite' then
        by = @service.__send__(:parse_json, json['source'], :user_show)
        to = @service.__send__(:parse_json, json['target_object'], :status_show)
        to.first.add_favorited_by(by.first, Time.parse(json['created_at']))
      when json['event'] == 'unfavorite' then
        by = @service.__send__(:parse_json, json['source'], :user_show)
        to = @service.__send__(:parse_json, json['target_object'], :status_show)
        to.first.remove_favorited_by(by.first)
      when json['event'] == 'follow' then
        source = @service.__send__(:parse_json, json['source'], :user_show).first
        target = @service.__send__(:parse_json, json['target'], :user_show).first
        if(target.me?)
          Plugin.call(:followers_created, @service, [source])
        elsif(source.me?)
          Plugin.call(:followings_created, @service, [target])
        end
      when json['delete'] then
        if $debug
          Plugin.call(:update, nil, [Message.new(:message => YAML.dump(json),
                                                 :system => true)]) end
      when !json.has_key?('event') then
        messages = @service.__send__(:parse_json, json, :streaming_status)
        if messages
          messages.each{ |msg|
            Plugin.call(:update, @service, [msg])
            Plugin.call(:mention, @service, [msg]) if msg.to_me?
            Plugin.call(:mypost, @service, [msg]) if msg.from_me? }
        elsif $debug
          Plugin.call(:update, nil, [Message.new(:message => YAML.dump(json),
                                                 :system => true)]) end
      when $debug
        Plugin.call(:update, nil, [Message.new(:message => YAML.dump(json),
                                               :system => true)])
      end
    rescue Exception => e
      notice e
    end end

  def self.start_streaming(&proc)
    begin
      @service.streaming(&proc)
    rescue Exception => e
      error e
    end
  end
end