fix the twitter stream agent. It requires OAuth credentials now.

Andrew Cantino 11 年 前
コミット
1bc8a3bc05
共有4 個のファイルを変更した28 個の追加14 個の削除を含む
  1. 3 3
      app/models/agents/twitter_publish_agent.rb
  2. 15 5
      app/models/agents/twitter_stream_agent.rb
  3. 1 1
      app/models/agents/twitter_user_agent.rb
  4. 9 5
      bin/twitter_stream.rb

+ 3 - 3
app/models/agents/twitter_publish_agent.rb

@@ -7,10 +7,10 @@ module Agents
7 7
     description <<-MD
8 8
       The TwitterPublishAgent publishes tweets from the events it receives.
9 9
 
10
-      You must set up a Twitter app and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`,
11
-      (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to publish as.
10
+      You [must set up a Twitter app](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token) and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`,
11
+      (also knows as "Access token" on the Twitter developer's site), along with the `username` of the Twitter user to publish as.
12 12
 
13
-      The `oauth_token` and `oauth_token_secret` specified determine which user the tweet will be sent as.
13
+      The `oauth_token` and `oauth_token_secret` determine which user the tweet will be sent as.
14 14
 
15 15
       You must also specify a `message_path` parameter: a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value to tweet.
16 16
 

+ 15 - 5
app/models/agents/twitter_stream_agent.rb

@@ -5,9 +5,11 @@ module Agents
5 5
     description <<-MD
6 6
       The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
7 7
 
8
-      You must provide a `twitter_username` and `twitter_password`, as well as an array of `filters`.  Multiple words in a filter
8
+      You must provide an oAuth `consumer_key`, `consumer_secret`, `access_key`, and `access_secret`, as well as an array of `filters`.  Multiple words in a filter
9 9
       must all show up in a tweet, but are independent of order.
10 10
 
11
+      To get oAuth credentials for Twitter, [follow these instructions](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token).
12
+
11 13
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
12 14
 
13 15
       `generate` should be either `events` or `counts`.  If set to `counts`, it will output event summaries whenever the Agent is scheduled.
@@ -46,8 +48,14 @@ module Agents
46 48
     default_schedule "11pm"
47 49
 
48 50
     def validate_options
49
-      unless options[:twitter_username].present? && options[:twitter_password].present? && options[:filters].present? && options[:expected_update_period_in_days].present? && options[:generate].present?
50
-        errors.add(:base, "expected_update_period_in_days, generate, twitter_username, twitter_password, and filters are required")
51
+      unless options[:consumer_key].present? &&
52
+             options[:consumer_secret].present? &&
53
+             options[:access_key].present? &&
54
+             options[:access_secret].present? &&
55
+             options[:filters].present? &&
56
+             options[:expected_update_period_in_days].present? &&
57
+             options[:generate].present?
58
+        errors.add(:base, "expected_update_period_in_days, generate, consumer_key, consumer_secret, access_key, access_secret, and filters are required fields")
51 59
       end
52 60
     end
53 61
 
@@ -57,8 +65,10 @@ module Agents
57 65
 
58 66
     def default_options
59 67
       {
60
-          :twitter_username => "---",
61
-          :twitter_password => "---",
68
+          :consumer_key => "---",
69
+          :consumer_secret => "---",
70
+          :access_key => "---",
71
+          :access_secret => "---",
62 72
           :filters => %w[keyword1 keyword2],
63 73
           :expected_update_period_in_days => "2",
64 74
           :generate => "events"

+ 1 - 1
app/models/agents/twitter_user_agent.rb

@@ -7,7 +7,7 @@ module Agents
7 7
     description <<-MD
8 8
       The TwitterUserAgent follows the timeline of a specified Twitter user.
9 9
 
10
-      You must set up a Twitter app and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`, (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to monitor.
10
+      You [must set up a Twitter app](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token) and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`, (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to monitor.
11 11
 
12 12
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
13 13
     MD

+ 9 - 5
bin/twitter_stream.rb

@@ -14,10 +14,15 @@ require 'twitter/json_stream'
14 14
 require 'em-http-request'
15 15
 require 'pp'
16 16
 
17
-def stream!(username, password, filters, &block)
17
+def stream!(filters, options = {}, &block)
18 18
   stream = Twitter::JSONStream.connect(
19 19
     :path    => "/1/statuses/#{(filters && filters.length > 0) ? 'filter' : 'sample'}.json#{"?track=#{filters.map {|f| CGI::escape(f) }.join(",")}" if filters && filters.length > 0}",
20
-    :auth    => "#{username}:#{password}",
20
+    :oauth => {
21
+      :consumer_key    => options[:consumer_key],
22
+      :consumer_secret => options[:consumer_secret],
23
+      :access_key      => options[:access_key],
24
+      :access_secret   => options[:access_secret]
25
+    },
21 26
     :ssl     => true
22 27
   )
23 28
 
@@ -55,12 +60,11 @@ def load_and_run(agents)
55 60
       end
56 61
     end
57 62
 
58
-    username = agents.first.options[:twitter_username]
59
-    password = agents.first.options[:twitter_password]
63
+    options = agents.first.options.slice(:consumer_key, :consumer_secret, :access_key, :access_secret)
60 64
 
61 65
     recent_tweets = []
62 66
 
63
-    stream!(username, password, filter_to_agent_map.keys) do |status|
67
+    stream!(filter_to_agent_map.keys, options) do |status|
64 68
       if status["retweeted_status"].present? && status["retweeted_status"].is_a?(Hash)
65 69
         puts "Skipping retweet: #{status["text"]}"
66 70
       elsif recent_tweets.include?(status["id_str"])