increase the delay
[shapado:piglops-shapado.git] / script / twitter_bot
1 #!/usr/bin/env ruby
2
3 require File.dirname(__FILE__)+"/../config/environment"
4
5 gem 'twitter', '0.9.8'
6 gem 'crack', '0.1.6'
7
8 require 'twitter'
9 require 'whatlanguage'
10
11 class TwitterBot
12   def initialize
13     @extractor = Object.new
14     @extractor.extend(Twitter::Extractor)
15   end
16
17   def search(keyword, &block)
18     begin
19       Twitter::Search.new(keyword).each do |status|
20         block.call(status)
21       end
22     rescue Crack::ParseError
23     end
24   end
25
26   def run!
27     loop do
28       Group.find_each("share.enable_twitter" => true) do |group|
29         begin
30           process_group(group)
31         rescue Exception => e
32           puts "WARNING: #{e} #{e.backtrace.join("\n\t")}"
33         end
34       end
35       sleep 5.minutes
36     end
37   end
38
39   private
40   def process_group(group)
41     return if group.share.twitter_user.blank?
42
43     share = group.share
44     default_date = (Time.now - 5.days).utc
45     last_date = (group[:last_twitter_question_at] ||default_date) rescue default_date
46
47     since = last_date.strftime("%Y-%m-%d")
48
49     query = "#{share.twitter_pattern} to:#{share.twitter_user} ?"
50     puts "Processing #{group.name}. query=#{query.inspect}"
51     self.search(query.strip) do |status|
52       id = status[:id]
53       date = Time.parse(status[:created_at]).utc
54       if date < last_date
55         next
56       end
57
58       if Question.first(:twitter_source => status[:id], :select => [:_id]).present?
59         next
60       end
61
62       status_text = status[:text]
63       username = status[:from_user]
64
65       puts "#{status_text.inspect} --@#{username}"
66
67       user = User.first(:twitter_login => username)
68       if user.present?
69         username = user.login
70       else
71         user = User.new(:anonymous => true, :login => "#{username}_twitter", :twitter_login => username, :from_twitter => true)
72         user.save(:validate => false)
73       end
74
75       title = status_text.gsub(/^@#{share.twitter_user}\s+/, "")
76       tags = Set.new(@extractor.extract_hashtags(status_text))
77       body = ""
78       if !user
79         body = %@<a rel="nofollow" href="http://twitter.com/#{username}/status/#{status[:id]}">#{username}</a> pregunta desde twitter:<br/><br/>@
80       end
81       body += title
82
83       language = group.language
84       if language.nil?
85         language = find_language(status_text)
86       end
87
88       if user.nil?
89         user = group.owner.present? ? group.owner : User.first(:login => "admin")
90       end
91
92       question = Question.create!(:title => title[0,100],
93                                   :tags => tags,
94                                   :body => body,
95                                   :group => group,
96                                   :user => user,
97                                   :language => language,
98                                   :twitter_source => status[:id])
99     end
100
101     group.set({:last_twitter_question_at => last_date})
102     group[:last_twitter_question_at] = last_date
103   end
104
105   private
106   def find_language(text)
107     case text.gsub(/<\/?[^>]*>/, "").language
108     when :spanish
109       'es'
110     when :french
111       'fr'
112     when :portuguese
113       'pt'
114     else
115       'en'
116     end
117   end
118 end
119
120
121 TwitterBot.new.run!