@@ -0,0 +1,30 @@ |
||
| 1 |
+module TwitterConcern |
|
| 2 |
+ extend ActiveSupport::Concern |
|
| 3 |
+ |
|
| 4 |
+ included do |
|
| 5 |
+ self.validate :validate_twitter_options |
|
| 6 |
+ self.after_initialize :configure_twitter |
|
| 7 |
+ end |
|
| 8 |
+ |
|
| 9 |
+ def validate_twitter_options |
|
| 10 |
+ unless options[:consumer_key].present? && |
|
| 11 |
+ options[:consumer_secret].present? && |
|
| 12 |
+ options[:oauth_token].present? && |
|
| 13 |
+ options[:oauth_token_secret].present? |
|
| 14 |
+ errors.add(:base, "consumer_key, consumer_secret, oauth_token and oauth_token_secret are required to authenticate with the Twitter API") |
|
| 15 |
+ end |
|
| 16 |
+ end |
|
| 17 |
+ |
|
| 18 |
+ def configure_twitter |
|
| 19 |
+ Twitter.configure do |config| |
|
| 20 |
+ config.consumer_key = options[:consumer_key] |
|
| 21 |
+ config.consumer_secret = options[:consumer_secret] |
|
| 22 |
+ config.oauth_token = options[:oauth_token] |
|
| 23 |
+ config.oauth_token_secret = options[:oauth_token_secret] |
|
| 24 |
+ end |
|
| 25 |
+ end |
|
| 26 |
+ |
|
| 27 |
+ module ClassMethods |
|
| 28 |
+ |
|
| 29 |
+ end |
|
| 30 |
+end |
@@ -0,0 +1,29 @@ |
||
| 1 |
+module WeiboConcern |
|
| 2 |
+ extend ActiveSupport::Concern |
|
| 3 |
+ |
|
| 4 |
+ included do |
|
| 5 |
+ self.validate :validate_weibo_options |
|
| 6 |
+ end |
|
| 7 |
+ |
|
| 8 |
+ def validate_weibo_options |
|
| 9 |
+ unless options[:app_key].present? && |
|
| 10 |
+ options[:app_secret].present? && |
|
| 11 |
+ options[:access_token].present? |
|
| 12 |
+ errors.add(:base, "app_key, app_secret and access_token are required") |
|
| 13 |
+ end |
|
| 14 |
+ end |
|
| 15 |
+ |
|
| 16 |
+ def weibo_client |
|
| 17 |
+ unless @weibo_client |
|
| 18 |
+ WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY |
|
| 19 |
+ WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET |
|
| 20 |
+ @weibo_client = WeiboOAuth2::Client.new |
|
| 21 |
+ @weibo_client.get_token_from_hash :access_token => options[:access_token] |
|
| 22 |
+ end |
|
| 23 |
+ @weibo_client |
|
| 24 |
+ end |
|
| 25 |
+ |
|
| 26 |
+ module ClassMethods |
|
| 27 |
+ |
|
| 28 |
+ end |
|
| 29 |
+end |
@@ -2,6 +2,7 @@ require "twitter" |
||
| 2 | 2 |
|
| 3 | 3 |
module Agents |
| 4 | 4 |
class TwitterPublishAgent < Agent |
| 5 |
+ include TwitterConcern |
|
| 5 | 6 |
cannot_be_scheduled! |
| 6 | 7 |
|
| 7 | 8 |
description <<-MD |
@@ -19,13 +20,9 @@ module Agents |
||
| 19 | 20 |
|
| 20 | 21 |
def validate_options |
| 21 | 22 |
unless options[:username].present? && |
| 22 |
- options[:expected_update_period_in_days].present? && |
|
| 23 |
- options[:consumer_key].present? && |
|
| 24 |
- options[:consumer_secret].present? && |
|
| 25 |
- options[:oauth_token].present? && |
|
| 26 |
- options[:oauth_token_secret].present? |
|
| 27 |
- errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required") |
|
| 28 |
- end |
|
| 23 |
+ options[:expected_update_period_in_days].present? |
|
| 24 |
+ errors.add(:base, "username and expected_update_period_in_days are required") |
|
| 25 |
+ end |
|
| 29 | 26 |
end |
| 30 | 27 |
|
| 31 | 28 |
def working? |
@@ -72,13 +69,6 @@ module Agents |
||
| 72 | 69 |
end |
| 73 | 70 |
|
| 74 | 71 |
def publish_tweet text |
| 75 |
- Twitter.configure do |config| |
|
| 76 |
- config.consumer_key = options[:consumer_key] |
|
| 77 |
- config.consumer_secret = options[:consumer_secret] |
|
| 78 |
- config.oauth_token = options[:oauth_token] |
|
| 79 |
- config.oauth_token_secret = options[:oauth_token_secret] |
|
| 80 |
- end |
|
| 81 |
- |
|
| 82 | 72 |
Twitter.update(text) |
| 83 | 73 |
end |
| 84 | 74 |
|
@@ -2,6 +2,8 @@ require "twitter" |
||
| 2 | 2 |
|
| 3 | 3 |
module Agents |
| 4 | 4 |
class TwitterUserAgent < Agent |
| 5 |
+ include TwitterConcern |
|
| 6 |
+ |
|
| 5 | 7 |
cannot_receive_events! |
| 6 | 8 |
|
| 7 | 9 |
description <<-MD |
@@ -39,9 +41,10 @@ module Agents |
||
| 39 | 41 |
default_schedule "every_1h" |
| 40 | 42 |
|
| 41 | 43 |
def validate_options |
| 42 |
- unless options[:username].present? && options[:expected_update_period_in_days].present? && options[:consumer_key].present? && options[:consumer_secret].present? && options[:oauth_token].present? && options[:oauth_token_secret].present? |
|
| 43 |
- errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required") |
|
| 44 |
- end |
|
| 44 |
+ unless options[:username].present? && |
|
| 45 |
+ options[:expected_update_period_in_days].present? |
|
| 46 |
+ errors.add(:base, "username and expected_update_period_in_days are required") |
|
| 47 |
+ end |
|
| 45 | 48 |
end |
| 46 | 49 |
|
| 47 | 50 |
def working? |
@@ -60,13 +63,6 @@ module Agents |
||
| 60 | 63 |
end |
| 61 | 64 |
|
| 62 | 65 |
def check |
| 63 |
- Twitter.configure do |config| |
|
| 64 |
- config.consumer_key = options[:consumer_key] |
|
| 65 |
- config.consumer_secret = options[:consumer_secret] |
|
| 66 |
- config.oauth_token = options[:oauth_token] |
|
| 67 |
- config.oauth_token_secret = options[:oauth_token_secret] |
|
| 68 |
- end |
|
| 69 |
- |
|
| 70 | 66 |
since_id = memory[:since_id] || nil |
| 71 | 67 |
opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
|
| 72 | 68 |
opts.merge! :since_id => since_id unless since_id.nil? |
@@ -3,6 +3,8 @@ require "weibo_2" |
||
| 3 | 3 |
|
| 4 | 4 |
module Agents |
| 5 | 5 |
class WeiboPublishAgent < Agent |
| 6 |
+ include WeiboConcern |
|
| 7 |
+ |
|
| 6 | 8 |
cannot_be_scheduled! |
| 7 | 9 |
|
| 8 | 10 |
description <<-MD |
@@ -19,11 +21,8 @@ module Agents |
||
| 19 | 21 |
|
| 20 | 22 |
def validate_options |
| 21 | 23 |
unless options[:uid].present? && |
| 22 |
- options[:expected_update_period_in_days].present? && |
|
| 23 |
- options[:app_key].present? && |
|
| 24 |
- options[:app_secret].present? && |
|
| 25 |
- options[:access_token].present? |
|
| 26 |
- errors.add(:base, "expected_update_period_in_days, uid, and access_token are required") |
|
| 24 |
+ options[:expected_update_period_in_days].present? |
|
| 25 |
+ errors.add(:base, "expected_update_period_in_days and uid are required") |
|
| 27 | 26 |
end |
| 28 | 27 |
end |
| 29 | 28 |
|
@@ -73,12 +72,7 @@ module Agents |
||
| 73 | 72 |
end |
| 74 | 73 |
|
| 75 | 74 |
def publish_tweet text |
| 76 |
- WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY |
|
| 77 |
- WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET |
|
| 78 |
- client = WeiboOAuth2::Client.new |
|
| 79 |
- client.get_token_from_hash :access_token => options[:access_token] |
|
| 80 |
- |
|
| 81 |
- client.statuses.update text |
|
| 75 |
+ weibo_client.statuses.update text |
|
| 82 | 76 |
end |
| 83 | 77 |
|
| 84 | 78 |
def unwrap_tco_urls text, tweet_json |
@@ -3,6 +3,8 @@ require "weibo_2" |
||
| 3 | 3 |
|
| 4 | 4 |
module Agents |
| 5 | 5 |
class WeiboUserAgent < Agent |
| 6 |
+ include WeiboConcern |
|
| 7 |
+ |
|
| 6 | 8 |
cannot_receive_events! |
| 7 | 9 |
|
| 8 | 10 |
description <<-MD |
@@ -69,11 +71,8 @@ module Agents |
||
| 69 | 71 |
|
| 70 | 72 |
def validate_options |
| 71 | 73 |
unless options[:uid].present? && |
| 72 |
- options[:expected_update_period_in_days].present? && |
|
| 73 |
- options[:app_key].present? && |
|
| 74 |
- options[:app_secret].present? && |
|
| 75 |
- options[:access_token].present? |
|
| 76 |
- errors.add(:base, "expected_update_period_in_days, uid, app_key, app_secret and access_token are required") |
|
| 74 |
+ options[:expected_update_period_in_days].present? |
|
| 75 |
+ errors.add(:base, "expected_update_period_in_days and uid are required") |
|
| 77 | 76 |
end |
| 78 | 77 |
end |
| 79 | 78 |
|
@@ -92,18 +91,12 @@ module Agents |
||
| 92 | 91 |
end |
| 93 | 92 |
|
| 94 | 93 |
def check |
| 95 |
- WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY |
|
| 96 |
- WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET |
|
| 97 |
- client = WeiboOAuth2::Client.new |
|
| 98 |
- client.get_token_from_hash :access_token => options[:access_token] |
|
| 99 |
- |
|
| 100 |
- |
|
| 101 | 94 |
since_id = memory[:since_id] || nil |
| 102 | 95 |
opts = {:uid => options[:uid].to_i}
|
| 103 | 96 |
opts.merge! :since_id => since_id unless since_id.nil? |
| 104 | 97 |
|
| 105 | 98 |
# http://open.weibo.com/wiki/2/statuses/user_timeline/en |
| 106 |
- resp = client.statuses.user_timeline opts |
|
| 99 |
+ resp = weibo_client.statuses.user_timeline opts |
|
| 107 | 100 |
if resp[:statuses] |
| 108 | 101 |
|
| 109 | 102 |
|