Last active
December 20, 2015 14:59
-
-
Save mjhea0/6150846 to your computer and use it in GitHub Desktop.
This file contains 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
from twitter import Twitter, OAuth, TwitterHTTPError | |
import sys | |
OAUTH_TOKEN = 'GET' | |
OAUTH_SECRET = 'YOUR' | |
CONSUMER_KEY = 'OWN' | |
CONSUMER_SECRET = 'TOKENS' | |
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET)) | |
print sys.stdout.encoding | |
def search_tweets(q, count=100, max_id=None): | |
return t.search.tweets(q=q, result_type='recent', count=count, lang="en", max_id=max_id) | |
def favorites_create(tweet): | |
try: | |
result = t.favorites.create(_id=tweet['id']) | |
print "Favorited: %s, %s" % (result['text'].encode(sys.stdout.encoding, errors='replace'), result['id']) | |
return result | |
except TwitterHTTPError as e: | |
print "Error: ", e | |
return None | |
def search_and_fav(q, count=100, max_id=None): | |
result = search_tweets(q, count, max_id) | |
first_id = result['statuses'][0]['id'] | |
last_id = result['statuses'][-1]['id'] | |
success = 0 | |
for t in result['statuses']: | |
if favorites_create(t) is not None: | |
success += 1 | |
print "Favorited total: %i of %i" % (success, len(result['statuses'])) | |
print "First id %s last id %s" % (first_id, last_id) | |
q = '#python' | |
search_and_fav(q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment