Skip to content

Commit

Permalink
Major overhaul to use search rather than public timeine, works much b…
Browse files Browse the repository at this point in the history
…etter.
  • Loading branch information
Vishnu Gopal committed May 30, 2009
1 parent 1d842fd commit 03234b0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 64 deletions.
18 changes: 9 additions & 9 deletions README
Expand Up @@ -2,30 +2,30 @@

The twitter search follow bot:
* Searches twitter public timeline for configurable keywords
* Can restrict those users to a particular location too, and,
* Follows users who tweet with those keywords

Changes in this version:
* Use search rather than public timeline fetch directly.
* Much more effective, finds a lot more users!
* New configuration
* Now only need to run every 30 minutes rather than every other minute!

Dependencies:
sudo gem install jnunemaker-twitter --source=http://gems.github.com

Configure:

config.yml should be self explanatory.

You can monitor specific keywords from users in particular locations and belonging to particular directories.

Currently it identifies Indian users from the tweepleindia directory as well as from location strings.
You can monitor specific keywords from users within a particular lat/long radius.

Install:
It's meant to be run as a cron and is not a daemon. Run every ten to fifteen minutes or so.
It's meant to be run as a cron and is not a daemon. Run every thirty minutes or so.

To run:
ruby -rubygems search-follow-bot.rb


To run periodically, try:
watch -n 60 "ruby -rubygems search-follow-bot.rb"

For production install on cron of course.

--
All code is Copyright (C) Vishnu Gopal. 2009.
Expand Down
4 changes: 2 additions & 2 deletions TODO
@@ -1,3 +1,3 @@

* Filter based on location, for e.g. only users from "India" (done, 30 May)
* Search older updates of users for keywords and then follow.
* Filter based on location, for e.g. only users from "India" (done, 30 May 2009)
* Search older updates of users for keywords and then follow. (done, 30 May 2009)
11 changes: 3 additions & 8 deletions config.yml
Expand Up @@ -4,14 +4,9 @@ username: 'mobshare'
password: 'our1account'

# any of these words
search_words: ['mobile', 'phone', 'mms', 'internet', 'twitter', 'airtel', 'aircel', 'bsnl', 'mtnl', '#ipl', '#indiavotes09']

# AND: (
# any of these words in location
location_match: ["india", "gurgaon", "delhi", "bombay", "kolkata", "hyderabad", "calcutta", "chennai", "noida", "bangalore"]
# OR:
check_in_tweeple_india_directory: true
# )
search_words: ['mobile', 'phone', 'internet', 'airtel', 'aircel', 'bsnl', 'mtnl', '#ipl', '#indiavotes09']
geocode: ['21.248422', '78.442383'] #lat, long, this is around the middle of India
within: '750mi' #750 miles

# check if profile image is set.
profile_image_check: false
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter_public_timeline.rb → lib/twitter_extensions.rb
@@ -1,5 +1,5 @@

module TwitterPublicTimeline
module TwitterExtensions
def self.included(base)
base.class_eval do
def public_timeline
Expand All @@ -11,6 +11,6 @@ def public_timeline

module Twitter
class Base
include TwitterPublicTimeline
include TwitterExtensions
end
end
63 changes: 20 additions & 43 deletions search-follow-bot.rb
Expand Up @@ -5,7 +5,7 @@

require 'config_store'
require 'twitter'
require 'twitter_public_timeline'
require 'twitter_extensions'
require 'pp'


Expand All @@ -19,18 +19,25 @@ def initialize(config)
process
end

def process
twitter.public_timeline.each do |update|
#out "Analyzing user: #{update['user']['screen_name']}, update: #{update['text']}..."
if relevant_update?(update)
pp update['user']
out "Found user: #{update['user']['screen_name']}(id: #{update['user']['id']}) with the tweet: #{update['text']}"
out "Starting to follow..."
twitter.friendship_create(update['user']['id'], true) #Start following!
out "Done!"
def process
config['search_words'].each do |term|
Twitter::Search.new.geocode(config['geocode'][0], config['geocode'][1], '750mi').containing("#{term}").fetch().results.each do |update|
begin
out "Found user: #{update['from_user']}(id: #{update['from_user_id']}) with the tweet: #{update['text']}"
out "Starting to follow..."
twitter.friendship_create(update['from_user'], true)
out "Done!"
sleep 0.5
rescue Twitter::NotFound
out "User ID not found correctly, trying next"
sleep 0.5 and next
rescue Twitter::General => e
out "Error: #{e.inspect}"
sleep 0.5 and next
end
end
end
rescue Twitter::RateLimitExceeded
rescue Twitter::RateLimitExceeded, Net::HTTPServerError
out "Rate limit exceeded, try spacing out cron intervals!"
end

Expand All @@ -40,39 +47,9 @@ def text_contains_any_of(text, array)
end
false
end

def relevant_update?(update)
if text_contains_any_of(update['text'], config['search_words'])
out "Text matched, let's see if location matches..."
out "(was analyzing user: #{update['user']['screen_name']}, location: #{update['user']['location']}, update: #{update['text']})"
if indian_user?(update['user'])
out "User found to be indian too, great!"
out "(was analyzing user: #{update['user']['screen_name']}, location: #{update['user']['location']}, update: #{update['text']})"
return true
end
end
false
end


def indian_user?(user)
if text_contains_any_of(user['location'], config['location_match'])
out "User #{user['screen_name']} location matched #{user['location']}, tagging as Indian."
return true
end
if config['check_in_tweeple_india_directory']
return in_tweeple_india_directory?(user)
end
false
end

# This works by checking for friendship between tweeple_india and the current user
def in_tweeple_india_directory?(user)
if twitter.friendship_exists?("tweepleindia", user['screen_name'])
out "User #{user['screen_name']} found in tweeple directory. Tagging as indian"
return true
end
false
def profile_image_for_user?(user)
return user['profile_image_url'].strip != ''
end

def out(text)
Expand Down

0 comments on commit 03234b0

Please sign in to comment.