Last active
August 29, 2015 14:08
-
-
Save rmamba/4627f0f2e50e3737253b to your computer and use it in GitHub Desktop.
tweetFollowBot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config = [ | |
{ | |
'consumer_key': 'KEY', | |
'consumer_secret': 'SECRET', | |
'access_token': 'TOKEN', | |
'access_token_secret': 'SECRET' | |
'user_name': 'YOUR_TWITTER_USER' | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import tweepy | |
import json | |
from config import config | |
consumer_key = config[0]['consumer_key'] | |
consumer_secret = config[0]['consumer_secret'] | |
access_token = config[0]['access_token'] | |
access_token_secret = config[0]['access_token_secret'] | |
username = config[0]['user_name'] | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
user = api.get_user(username) | |
friends = api.friends_ids(username) | |
#print friends | |
#for friend in friends: | |
# print friend.screen_name | |
waitList = [] | |
def followUsers(userIDs): | |
# limits = api.rate_limit_status() | |
# print limits | |
for userID in userIDs: | |
print '>>> ', userID | |
api.create_friendship(user_id=userID, follow=True) | |
friends.append(userID) | |
waitList = [] | |
# This is the listener, resposible for receiving data | |
class StdOutListener(tweepy.StreamListener): | |
def on_status(self, status): | |
print status.text | |
def on_data(self, data): | |
# Twitter returns data in JSON format - we need to decode it first | |
decoded = json.loads(data) | |
newUsers = [] | |
if (decoded['user']['id'] not in friends) and (decoded['user']['id'] not in waitList): | |
# Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users | |
newUsers.append(decoded['user']['id']) | |
print decoded['user']['id'], ': ', decoded['user']['screen_name'] | |
for mention in decoded['entities']['user_mentions']: | |
if (mention['id'] not in friends) and (mention['id'] not in waitList): | |
newUsers.append(mention['id']) | |
print "\t", mention['id'], ': ', mention['screen_name'] | |
followUsers(newUsers) | |
# print '@%s: %s @ %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'), decoded['coordinates']) | |
# print decoded['entities'] | |
# print '' | |
def on_error(self, status): | |
print status | |
def on_timeout(self): | |
print >> sys.stderr, 'Timeout...' | |
return True # Don't kill the stream | |
l = StdOutListener() | |
print "Searching for users to follow..." | |
# There are different kinds of streams: public stream, user stream, multi-user streams | |
# In this example follow #programming tag | |
# For more details refer to https://dev.twitter.com/docs/streaming-apis | |
stream = tweepy.Stream(auth, l) | |
stream.filter(track=['raspberrypi', 'raspberry_pi']) #, language=['eng']) #locations=[-180.0,-90.0,180.0,90.0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment