#!/usr/bin/python
#import requests
import base64
import json
import cgi
import urllib2
import urllib
TWITTER_KEY = 'kq9tH5rSWLra9zhDJZKGOvl1I'
TWITTER_SECRET = 'OExf8GfO312wh2kV9ozza4mh69a7u4KZPL7CZ8RhDgEotCrhnf'
TWITTER_TOKEN_URL = 'https://api.twitter.com/oauth2/token'
TWITTER_TOKEN_CONTENT_TYPE = 'application/x-www-form-urlencoded;charset=UTF-8'
TWITTER_TOKEN_REQUEST_DATA = 'grant_type=client_credentials'
TWITTER_TWITTS_URL = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
TWITTER_FRIENDS_URL = 'https://api.twitter.com/1.1/friends/list.json'
TWITTER_USER_URL = 'https://api.twitter.com/1.1/users/show.json'
TWITTER_REQUEST_LIMIT_URL = 'https://api.twitter.com/1.1/application/rate_limit_status.json'
def get_bearer():
auth_str = '%s:%s' % (TWITTER_KEY, TWITTER_SECRET)
b64_auth_str = base64.b64encode(auth_str)
#headers = {'Authorization': 'Basic %s' % b64_auth_str,
# 'Content-Type': TWITTER_TOKEN_CONTENT_TYPE}
#r = requests.post(TWITTER_TOKEN_URL, data=TWITTER_TOKEN_REQUEST_DATA, headers=headers)
req = urllib2.Request(TWITTER_TOKEN_URL, data=TWITTER_TOKEN_REQUEST_DATA)
req.add_header('Authorization', 'Basic %s' % b64_auth_str)
req.add_header('Content-Type', TWITTER_TOKEN_CONTENT_TYPE)
resp = urllib2.urlopen(req)
content = resp.read()
res_json = json.loads(content)
#return json.loads(r.text)['access_token']
return res_json['access_token']
def get_friends(bearer, user):
friends = []
#headers = {'Authorization': 'Bearer %s' % bearer}
cursor = -1
while cursor!=0:
#r = requests.get(TWITTER_FRIENDS_URL, headers=headers,
# params={'screen_name': user,
# 'cursor': cursor})
#res_json = json.loads(r.text)
params = urllib.urlencode({'screen_name': user, 'cursor': cursor})
#req = urllib2.Request('%s?screen_name=%s&cursor=%s' % (TWITTER_FRIENDS_URL, user, cursor))
req = urllib2.Request('%s?%s' % (TWITTER_FRIENDS_URL, params))
req.add_header('Authorization', 'Bearer %s' % bearer)
resp = urllib2.urlopen(req)
content = resp.read()
res_json = json.loads(content)
cursor = res_json['next_cursor']
friends = friends + res_json['users']
return friends
def get_profile(bearer, user):
#headers = {'Authorization': 'Bearer %s' % bearer}
#r = requests.get(TWITTER_USER_URL, headers=headers,
# params={'screen_name': user})
#res_json = json.loads(r.text)
params = urllib.urlencode({'screen_name': user})
#req = urllib2.Request('%s?screen_name=%s' % (TWITTER_USER_URL, user))
req = urllib2.Request('%s?%s' % (TWITTER_USER_URL, params))
req.add_header('Authorization', 'Bearer %s' % bearer)
resp = urllib2.urlopen(req)
content = resp.read()
res_json = json.loads(content)
return res_json
def get_limits(bearer):
req = urllib2.Request(TWITTER_REQUEST_LIMIT_URL)
req.add_header('Authorization', 'Bearer %s' % bearer)
resp = urllib2.urlopen(req)
content = resp.read()
res_json = json.loads(content)
return res_json
def generate_html(user, bearer, friends, profile):
print "Content-type: text/html; charset=utf-8"
print
#print '<h3>Bearer: %s</h3>' % bearer
#print '<h3>Friends: %s</h3>' % json.dumps(friends)
#print '<h3>Profile: %s</h3>' % json.dumps(profile)
print '<h2>User: %s</h2>' % user
print '<h2>Name: %s</h2>' % profile['name'].encode('utf-8')
print '<h2>Description: %s</h2>' % profile['description'].encode('utf-8')
print '<img src=%s />' % profile['profile_image_url'].replace('_normal', '')
print '<h2>Following</h2>'
print '<ol>'
for i in range(len(friends)):
print '<li>%s</li>' % (friends[i]['name'].encode('utf-8'))
print '</ol>'
def generate_error_html():
print "Content-type: text/html; charset=utf-8"
print
print '<h2>Request limit reached, try again in 20 minutes</h2>'
def main():
arguments = cgi.FieldStorage()
user = arguments['user'].value if 'user' in arguments else 'barak_ehud'
bearer = get_bearer()
limits = get_limits(bearer)
if limits['resources']['friends']['/friends/list']['remaining'] < 20:
generate_error_html()
return
#print json.dumps(limits)
friends = get_friends(bearer, user)
profile = get_profile(bearer, user)
generate_html(user, bearer, friends, profile)
#print profile['profile_image_url']
#for i in range(len(friends)):
# print '%s: %s' % (i, friends[i]['name'])
if __name__ == '__main__':
main()