******************* (First Program with name oauth1.py) *****************
####################### Start of the Program
#!/usr/bin/python
import time
import random
import urllib
# crypto imports
import base64
import hmac
from hashlib import sha1
# this style is from python urllib implementation
nonce_alphabet = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789')
def encode(s):
"""encode a url component"""
return urllib.quote(s, "~")
def now():
"""returns the current unix timestamp"""
epoch = int(time.time())
return str(epoch)
def nonce():
"""returns a 24 letter nonce"""
choices = []
choices = [random.choice(nonce_alphabet) for i in xrange(24)]
return ''.join(choices)
def authorization_header(token, method, url, query={}, post_query={}):
"""returns the header value for key Authorization (oauth impl)"""
# build basic oauth variables
oauth_consumer_key = token['oauth_consumer_key']
oauth_nonce = nonce()
oauth_signature_method = 'HMAC-SHA1'
oauth_timestamp = now()
oauth_token = token['oauth_token']
oauth_version = '1.0'
# compute signature
dict = {
'oauth_consumer_key': oauth_consumer_key,
'oauth_nonce': oauth_nonce,
'oauth_signature_method': oauth_signature_method,
'oauth_timestamp': oauth_timestamp,
'oauth_token': oauth_token,
'oauth_version': oauth_version
}
dict.update(query)
dict.update(post_query)
param_str = urllib.urlencode(sorted(dict.iteritems())) # important step
key = "{0}&{1}".format(token['oauth_consumer_secret'],
token['oauth_token_secret'])
msg = "&".join(map(encode, [method, url, param_str]))
m = hmac.new(key, msg, sha1)
digest = m.digest()
digestb64 = base64.b64encode(digest)
oauth_signature = encode(digestb64)
# build header
auth_items = []
auth_items.append('oauth_consumer_key="' + oauth_consumer_key + '"')
auth_items.append('oauth_nonce="' + oauth_nonce + '"')
auth_items.append('oauth_signature="' + oauth_signature + '"')
auth_items.append('oauth_signature_method="' + oauth_signature_method + '"')
auth_items.append('oauth_timestamp="' + oauth_timestamp + '"')
auth_items.append('oauth_token="' + oauth_token + '"')
auth_items.append('oauth_version="' + oauth_version + '"')
return "OAuth " + ",".join(auth_items)
if __name__ == "__main__":
input = 'k1=v1&k2=v2'
print("encode '" + input + "': " + encode(input))
print("now: " + now())
print("a nonce: " + nonce())
token = {
'oauth_consumer_key': 'CON-KEY',
'oauth_consumer_secret': 'CON-S3KR3T',
'oauth_token': 'TOK',
'oauth_token_secret': 'TOK-S3KR3T'
}
print("auth header: " + authorization_header(token, 'GET',
'https://localhost'))
####################### End of the Program
************************** (Second Program which will use oauth1.py) ***************
#################### Start of the Program
#!/usr/bin/python
import os
import sys
import urllib
import urllib2
import json
#from simplejson import *
import logging
#sys.path.append(os.path.join('..', '..', 'main', 'python'))
import oauth1
#file_name=sys.argv[1]
# Path I used here to print result in file in my lappi folder. You can specify your own path
# or can also ignore if don't want to write in some output file
path='/home/nitin/Nitin/Python'
logging.basicConfig(filename='twitter.log', level=logging.INFO)
logging.info("initialized logging")
def fetch(name):
logging.info("fetching user details for '" + str(name) + "'")
url = 'https://api.twitter.com/1.1/users/show.json'
url_params = {'screen_name': name}
qs = urllib.urlencode(url_params)
#url_with_qs = url if len(qs) == 0 else url + "?" + qs
if len(qs) == 0:
url_with_qs=url
else:
url_with_qs=url + "?" + qs
req = urllib2.Request(url_with_qs)
req.add_header('Accept', '*/*')
req.add_header('User-Agent', 'ni-client v0.0.1')
req.add_header('Authorization', oauth1.authorization_header(token, 'GET', url, url_params))
try:
r = urllib2.urlopen(req)
resp = r.read()
logging.info("response status code: " + str(r.getcode()))
print resp
return resp
except:
print "Error while fetching ", name
def fetch_twitter_api():
file_handles=open(file_name,'r')
file=open(path + '/screen_firsttokenids.txt','w')
for handle in file_handles.readlines():
if handle:
resp=fetch(handle)
if resp:
json_dict=json.loads(resp)
twi_ID=json_dict['id']
twi_retweet_count=0
if json_dict.has_key('status'):
if json_dict['status']['retweet_count']:
twi_retweet_count=json_dict['status']['retweet_count']
twi_fav_count=json_dict['favourites_count']
handle=str(handle).strip()
print "Screen_name: %s , ID: %s , retweet count: %s, Fav count: %s"% (handle, twi_ID,twi_retweet_count,twi_fav_count)
output = "\nScreen_name: %s , ID: %s , retweet count: %s, Fav count: %s"% (handle, twi_ID, twi_retweet_count, twi_fav_count)
#output="\nScreen_name %s , ID %s"% (handle, ID)
file.write(output)
else:
handle=str(handle).strip()
print "Got Error for Screen_name %s"% (handle)
output="\nGot Error for Screen_name %s"% (handle)
file.write(output)
file.close()
if __name__ == "__main__":
token = {
'oauth_consumer_key': 'o5XSukmMltriI3O78BgRw',
'oauth_consumer_secret': 'tuNfB4MpagDr9Znhg4y9s6x7LDfxtZj0XmSYnoDYo4',
'oauth_token': '1062703507-p6fMLHSa0C9v9VbtsNzBsRtDdZQNJ2C9yEX4VyN',
'oauth_token_secret': 'sjxIXvmAtTYsvCdzvpN72sYQQahLmqxlkRqEm4bmI70'
}
#print(fetch('syncapse'))
#print(fetch('samsungtweets'))
fetch('nitin89syncapse')
#fetch('_1E')
#fetch_twitter_api()
############################# End of the Program
####################### Start of the Program
#!/usr/bin/python
import time
import random
import urllib
# crypto imports
import base64
import hmac
from hashlib import sha1
# this style is from python urllib implementation
nonce_alphabet = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789')
def encode(s):
"""encode a url component"""
return urllib.quote(s, "~")
def now():
"""returns the current unix timestamp"""
epoch = int(time.time())
return str(epoch)
def nonce():
"""returns a 24 letter nonce"""
choices = []
choices = [random.choice(nonce_alphabet) for i in xrange(24)]
return ''.join(choices)
def authorization_header(token, method, url, query={}, post_query={}):
"""returns the header value for key Authorization (oauth impl)"""
# build basic oauth variables
oauth_consumer_key = token['oauth_consumer_key']
oauth_nonce = nonce()
oauth_signature_method = 'HMAC-SHA1'
oauth_timestamp = now()
oauth_token = token['oauth_token']
oauth_version = '1.0'
# compute signature
dict = {
'oauth_consumer_key': oauth_consumer_key,
'oauth_nonce': oauth_nonce,
'oauth_signature_method': oauth_signature_method,
'oauth_timestamp': oauth_timestamp,
'oauth_token': oauth_token,
'oauth_version': oauth_version
}
dict.update(query)
dict.update(post_query)
param_str = urllib.urlencode(sorted(dict.iteritems())) # important step
key = "{0}&{1}".format(token['oauth_consumer_secret'],
token['oauth_token_secret'])
msg = "&".join(map(encode, [method, url, param_str]))
m = hmac.new(key, msg, sha1)
digest = m.digest()
digestb64 = base64.b64encode(digest)
oauth_signature = encode(digestb64)
# build header
auth_items = []
auth_items.append('oauth_consumer_key="' + oauth_consumer_key + '"')
auth_items.append('oauth_nonce="' + oauth_nonce + '"')
auth_items.append('oauth_signature="' + oauth_signature + '"')
auth_items.append('oauth_signature_method="' + oauth_signature_method + '"')
auth_items.append('oauth_timestamp="' + oauth_timestamp + '"')
auth_items.append('oauth_token="' + oauth_token + '"')
auth_items.append('oauth_version="' + oauth_version + '"')
return "OAuth " + ",".join(auth_items)
if __name__ == "__main__":
input = 'k1=v1&k2=v2'
print("encode '" + input + "': " + encode(input))
print("now: " + now())
print("a nonce: " + nonce())
token = {
'oauth_consumer_key': 'CON-KEY',
'oauth_consumer_secret': 'CON-S3KR3T',
'oauth_token': 'TOK',
'oauth_token_secret': 'TOK-S3KR3T'
}
print("auth header: " + authorization_header(token, 'GET',
'https://localhost'))
####################### End of the Program
************************** (Second Program which will use oauth1.py) ***************
#################### Start of the Program
#!/usr/bin/python
import os
import sys
import urllib
import urllib2
import json
#from simplejson import *
import logging
#sys.path.append(os.path.join('..', '..', 'main', 'python'))
import oauth1
#file_name=sys.argv[1]
# Path I used here to print result in file in my lappi folder. You can specify your own path
# or can also ignore if don't want to write in some output file
path='/home/nitin/Nitin/Python'
logging.basicConfig(filename='twitter.log', level=logging.INFO)
logging.info("initialized logging")
def fetch(name):
logging.info("fetching user details for '" + str(name) + "'")
url = 'https://api.twitter.com/1.1/users/show.json'
url_params = {'screen_name': name}
qs = urllib.urlencode(url_params)
#url_with_qs = url if len(qs) == 0 else url + "?" + qs
if len(qs) == 0:
url_with_qs=url
else:
url_with_qs=url + "?" + qs
req = urllib2.Request(url_with_qs)
req.add_header('Accept', '*/*')
req.add_header('User-Agent', 'ni-client v0.0.1')
req.add_header('Authorization', oauth1.authorization_header(token, 'GET', url, url_params))
try:
r = urllib2.urlopen(req)
resp = r.read()
logging.info("response status code: " + str(r.getcode()))
print resp
return resp
except:
print "Error while fetching ", name
def fetch_twitter_api():
file_handles=open(file_name,'r')
file=open(path + '/screen_firsttokenids.txt','w')
for handle in file_handles.readlines():
if handle:
resp=fetch(handle)
if resp:
json_dict=json.loads(resp)
twi_ID=json_dict['id']
twi_retweet_count=0
if json_dict.has_key('status'):
if json_dict['status']['retweet_count']:
twi_retweet_count=json_dict['status']['retweet_count']
twi_fav_count=json_dict['favourites_count']
handle=str(handle).strip()
print "Screen_name: %s , ID: %s , retweet count: %s, Fav count: %s"% (handle, twi_ID,twi_retweet_count,twi_fav_count)
output = "\nScreen_name: %s , ID: %s , retweet count: %s, Fav count: %s"% (handle, twi_ID, twi_retweet_count, twi_fav_count)
#output="\nScreen_name %s , ID %s"% (handle, ID)
file.write(output)
else:
handle=str(handle).strip()
print "Got Error for Screen_name %s"% (handle)
output="\nGot Error for Screen_name %s"% (handle)
file.write(output)
file.close()
if __name__ == "__main__":
token = {
'oauth_consumer_key': 'o5XSukmMltriI3O78BgRw',
'oauth_consumer_secret': 'tuNfB4MpagDr9Znhg4y9s6x7LDfxtZj0XmSYnoDYo4',
'oauth_token': '1062703507-p6fMLHSa0C9v9VbtsNzBsRtDdZQNJ2C9yEX4VyN',
'oauth_token_secret': 'sjxIXvmAtTYsvCdzvpN72sYQQahLmqxlkRqEm4bmI70'
}
#print(fetch('syncapse'))
#print(fetch('samsungtweets'))
fetch('nitin89syncapse')
#fetch('_1E')
#fetch_twitter_api()
############################# End of the Program
No comments:
Post a Comment