Created
October 28, 2014 01:51
-
-
Save yamaguchiyuto/84c513ff11590a92dc6d 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
import sys | |
import random | |
import os | |
import tweepy | |
def get_auth(key_file): | |
consumer_key, consumer_secret, access_token, access_token_secret = open(key_file, 'r').readline().rstrip().split(' ') | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
return auth | |
def get_favs_count(api, screen_name): | |
user = api.get_user(screen_name) | |
return user.favourites_count | |
def get_random_favs(api, screen_name, n): | |
nfavs = get_favs_count(api, screen_name) | |
r = random.randint(0,nfavs/n) | |
try: | |
favs = api.favorites(id=screen_name, count=n, page=r) | |
except tweepy.error.TweepError, inst: | |
error_code = inst[0][0]['code'] | |
error_message = inst[0][0]['message'] | |
print >> sys.stderr, error_code, error_message | |
exit() | |
return favs | |
def get_url(fav): | |
screen_name = fav.user.screen_name | |
tid = fav.id | |
return "http://twitter.com/%s/status/%s" % (screen_name, tid) | |
def open_url(url): | |
os.system('open %s' % url) | |
key_file = sys.argv[1] | |
screen_name = sys.argv[2] | |
n = int(sys.argv[3]) | |
auth = get_auth(key_file) | |
api = tweepy.API(auth) | |
favs = get_random_favs(api, screen_name, n) | |
urls = [get_url(f) for f in favs] | |
map(open_url, urls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment