Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter API favouriter
from twitter import Twitter, OAuth, TwitterHTTPError
OAUTH_TOKEN = 'foo'
OAUTH_SECRET = 'bar'
CONSUMER_KEY = 'baz'
CONSUMER_SECRET = 'bat'
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
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'], 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)
@ErisBlastar

This comment has been minimized.

Copy link

@ErisBlastar ErisBlastar commented Aug 2, 2013

I get an error in line 19, invalid syntax:

C:\Python33>python tweetbot.py
Traceback (most recent call last):
File "tweetbot.py", line 1, in
import twitter_favouriter
File "C:\Python33\twitter_favouriter.py", line 19
print "Favorited: %s, %s" % (result['text'], result['id'])
^
SyntaxError: invalid syntax

This was with Python 3.3, it works in Python 2.7 but it has an error when it uses an UNICODE character.

@Braunson

This comment has been minimized.

Copy link

@Braunson Braunson commented Aug 2, 2013

@EricBlastar @jmoz I've updated it to work with Python 3.x here https://gist.github.com/Braunson/6144191

@EricBlastar you are receiving that because the code was for <2.2 but you've got 3.x installed. A few things have changed. That kind of string formatting has been deprecated since like 2.2 and they removed it for >3.x

@ErisBlastar

This comment has been minimized.

Copy link

@ErisBlastar ErisBlastar commented Aug 3, 2013

@jmoz @BraunsonI fixed it for Python 2.7 here: https://gist.github.com/ErisBlastar/6144424

BTW it is Eris and not Eric, thanx for the update.

@holic

This comment has been minimized.

Copy link

@holic holic commented Oct 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment