mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-19 05:11:25 +00:00
Merge pull request #363 from alias1/twitteruseragent-tweaks
TwitterUserAgent Tweaks
This commit is contained in:
commit
94a846563c
2 changed files with 55 additions and 16 deletions
|
@ -17,11 +17,15 @@ module Agents
|
||||||
|
|
||||||
You must also provide the `username` of the Twitter user to monitor.
|
You must also provide the `username` of the Twitter user to monitor.
|
||||||
|
|
||||||
|
Set `include_retweets` to `false` to not include retweets (default: `true`)
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
Set `starting_at` to the date/time (eg. `Mon Jun 02 00:38:12 +0000 2014`) you want to start receiving tweets from (default: agent's `created_at`)
|
||||||
MD
|
MD
|
||||||
|
|
||||||
event_description <<-MD
|
event_description <<-MD
|
||||||
Events are the raw JSON provided by the Twitter API. Should look something like:
|
Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
|
||||||
|
|
||||||
{
|
{
|
||||||
... every Tweet field, including ...
|
... every Tweet field, including ...
|
||||||
|
@ -46,38 +50,60 @@ module Agents
|
||||||
|
|
||||||
default_schedule "every_1h"
|
default_schedule "every_1h"
|
||||||
|
|
||||||
def validate_options
|
|
||||||
unless options['username'].present? &&
|
|
||||||
options['expected_update_period_in_days'].present?
|
|
||||||
errors.add(:base, "username and expected_update_period_in_days are required")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def working?
|
def working?
|
||||||
event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
|
event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_options
|
def default_options
|
||||||
{
|
{
|
||||||
'username' => "tectonic",
|
'username' => 'tectonic',
|
||||||
'expected_update_period_in_days' => "2"
|
'include_retweets' => 'true',
|
||||||
|
'expected_update_period_in_days' => '2'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_options
|
||||||
|
errors.add(:base, "username is required") unless options['username'].present?
|
||||||
|
errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
|
||||||
|
|
||||||
|
if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
|
||||||
|
errors.add(:base, "include_retweets must be a boolean value string (true/false)")
|
||||||
|
end
|
||||||
|
|
||||||
|
if options[:starting_at].present?
|
||||||
|
Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def starting_at
|
||||||
|
if options[:starting_at].present?
|
||||||
|
Time.parse(options[:starting_at]) rescue created_at
|
||||||
|
else
|
||||||
|
created_at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_retweets?
|
||||||
|
options[:include_retweets] != "false"
|
||||||
|
end
|
||||||
|
|
||||||
def check
|
def check
|
||||||
since_id = memory['since_id'] || nil
|
since_id = memory['since_id'] || nil
|
||||||
opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
|
opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => false, :include_entities => true, :contributor_details => true}
|
||||||
opts.merge! :since_id => since_id unless since_id.nil?
|
opts.merge! :since_id => since_id unless since_id.nil?
|
||||||
|
|
||||||
|
# http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
|
||||||
tweets = twitter.user_timeline(options['username'], opts)
|
tweets = twitter.user_timeline(options['username'], opts)
|
||||||
|
|
||||||
tweets.each do |tweet|
|
tweets.each do |tweet|
|
||||||
memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
|
if tweet.created_at >= starting_at
|
||||||
|
memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
|
||||||
|
|
||||||
create_event :payload => tweet.attrs
|
create_event :payload => tweet.attrs
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
save!
|
save!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,10 +4,11 @@ describe Agents::TwitterUserAgent do
|
||||||
before do
|
before do
|
||||||
# intercept the twitter API request for @tectonic's user profile
|
# intercept the twitter API request for @tectonic's user profile
|
||||||
stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
|
stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
|
||||||
|
|
||||||
@opts = {
|
@opts = {
|
||||||
:username => "tectonic",
|
:username => "tectonic",
|
||||||
:expected_update_period_in_days => "2",
|
:expected_update_period_in_days => "2",
|
||||||
|
:starting_at => "Jan 01 00:00:01 +0000 2000",
|
||||||
:consumer_key => "---",
|
:consumer_key => "---",
|
||||||
:consumer_secret => "---",
|
:consumer_secret => "---",
|
||||||
:oauth_token => "---",
|
:oauth_token => "---",
|
||||||
|
@ -25,4 +26,16 @@ describe Agents::TwitterUserAgent do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
describe "#check with starting_at=future date" do
|
||||||
|
it "should check for changes starting_at a future date, thus not find any" do
|
||||||
|
opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })
|
||||||
|
|
||||||
|
checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
|
||||||
|
checker.user = users(:bob)
|
||||||
|
checker.save!
|
||||||
|
|
||||||
|
lambda { checker.check }.should change { Event.count }.by(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue