-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.py
More file actions
157 lines (126 loc) · 4.86 KB
/
Request.py
File metadata and controls
157 lines (126 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Vinay Ayyala Twitter Request Example
import Queue
import json
import sys
import oauth2 as oauth
import urllib2 as urllib
# pickle is a cool package to serialize python objects. It's really helpful
# because we want to store our data in case we get rate limited from twitter
# or in case we have an error with one of our requests
import pickle
from time import gmtime, strftime
import random
from keys import access_token_key, access_token_secret, consumer_key, consumer_secret
if len(access_token_key) == 0 or len(access_token_secret) == 0 or len(consumer_key) == 0 or len(consumer_secret) == 0:
raise KeyError("Did you forget to add your keys to keys.py?")
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
# example of using TwitterRequest
# this example takes an set of users, finds all the ids of users they follow ("friends") and returns them
# # # # # # # # # # # # # # # # # #
# Arguments:
# # # # input_set: the set of Twitter usernames we want to find the friends of. Can be a set, list, or anything iterable.
# # # # pickle_file: The name of the file that we want to store our data to
# Results:
# # # # dictonary of <username>:[<friend id strings>] pairs
def get_friends(input_set, pickle_file):
friends = {}
q = Queue.Queue()
# we put our input set of users in a queue
for x in input_set:
q.put(str(x))
# For every user in our queue, we want to send a request to twitter, pull their friends, and store them in our dictionary
while (q.empty() is False):
s = q.get()
parameters = []
cursor1 = '-1'
print s
friends[s] = set()
while (cursor1 != '0'):
try:
# send the request to twitter
resp = dict(json.loads(TwitterRequest('friends/ids.json?screen_name='+s+'&stringify_ids=true&count=5000&cursor='+cursor1, "GET", parameters, pickle_file, friends)))
# check for errors
if ('errors' in resp):
if ('message' in 'errors'):
if (resp['errors']['code'] == 88):
print 'sleeping for 100 seconds'
time.sleep(100)
if (resp['errors']['code'] == 34):
print 'page does not exist'
cursor1 = '0'
cursor1 = '0'
# if no errors, parse our response
else:
ids = resp['ids']
for id_ in ids:
friends[s].add(id_)
cursor1 = str(resp['next_cursor'])
except LookupError:
cursor1 = '0'
except (IOError, ValueError):
time.sleep(10)
# we open up our pickle file and store our data
file1 = open(pickle_file,"wb")
pickle.dump(friends, file1)
file1.close()
return friends
# send a generic request to twitter and parse response
# Arguments:
# # # # endpoint: the API request you want to make to twitter, check the Twitter Docs
# # # # method: "GET", "POST"
# # # # parameters: []
# # # # pickle_file: where you want "dict_" to be stored in the event of a rate limite
# # # # dict_: file you want stored in the event of a rate limit
# Result:
# # # # jsonified Twitter Response
def TwitterRequest(endpoint, method, parameters, pickle_file, dict_):
url = "https://api.twitter.com/1.1/"+endpoint
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_handler = urllib.HTTPHandler(debuglevel= 0)
https_handler = urllib.HTTPSHandler(debuglevel= 0)
req = oauth.Request.from_consumer_and_token(oauth_consumer,
token=oauth_token,
http_method="GET",
http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
if ('x-rate-limit-remaining' not in response.headers):
print str(response)
print str(response.headers)
print url
return TwttrRequest(endpoint, method, parameters)
else:
if (response.headers['x-rate-limit-remaining'] == '0'):
print 'waiting'
reset_time = float(response.headers['x-rate-limit-reset'])
current_time = float(time.time())
file1 = open(pickle_file, "wb")
pickle.dump(dict_, file1)
file1.close()
if (current_time < reset_time):
#reset_time is rounded down, so offset by 1.0 second to prevent any rate limit errors
time_wait = ((reset_time - current_time) + 5.0)
print time_wait
print strftime("%Y-%m-%d %H:%M:%S", gmtime())
time.sleep(time_wait)
else:
time.sleep(10)
print 'success ' + endpoint + ' ' + str(response.headers['x-rate-limit-remaining'])
a = ''
for line in response:
a = a + line
return a
if __name__ == "__main__":
# Example
seed = ['jack', 'biz', 'ev']
pickle_output = "results.pickle"
friends = get_friends(seed, pickle_output)
print friends