-
Notifications
You must be signed in to change notification settings - Fork 3
/
SEAPI.py
162 lines (140 loc) · 6.96 KB
/
SEAPI.py
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
158
159
160
161
162
import requests
try:
import simplejson as json
except:
import json
from time import time, sleep
class SEAPI():
def __init__(self, **kwargs):
"""Use kwargs to set default parameters, e.g.
>>> se = SEAPI(site="stackoverflow")"""
self.default_params = {"pagesize": 100} # explicit "pagesize" is required
self.default_params.update(kwargs)
self.api_address = "http://api.stackexchange.com"
self.api_version = "2.1"
self.key = "" # Replace with your own API key
self.access_token = ""
if self.key:
self.default_params['key'] = self.key
if self.access_token:
self.default_params['access_token'] = self.access_token
self.last_call = ["", {}]
self.last_status = {}
self.last_response_times = []
# def _replace_placeholders(self, command, **kwargs):
# """Parses API command to find placeholders
# for elements '{foo}' or lists '{foos}',
# replacing them with kwargs foo=number, or foos=list;
# EXAMPLE:
# >>> seapi._replace_placeholders("users/{ids}/comments/{toid}",
# ids = [1, 2], toid = 5)
# "users/1;2/comments/5" """
# parts = command.split("/")
# for i, part in enumerate(parts):
# if part.startswith("{"):
# if part.endswith("s}"):
# parts[i] = ";".join(map(str, kwargs[part[1:-1]]))
# elif part.endswith("}"):
# parts[i] = str(kwargs[part[1:-1]])
# return "/".join(parts)
def _find_placeholders(self, raw_command, **kwargs):
parts = raw_command.split("/")
curly_params = {} # e.g. for "{id}"
curly_list_params = {} # e.g. for "{ids}"; and dict with max 1 element
for part in parts:
if part.startswith("{"):
if part.endswith("s}"):
curly_list_params[part[1:-1]] = kwargs[part[1:-1]]
elif part.endswith("}"):
curly_params[part[1:-1]] = kwargs[part[1:-1]]
return (parts, curly_params, curly_list_params)
def _combine_placeholders(self, parts, curly_params, curly_list_params):
parts_filled = parts[:]
for i, part in enumerate(parts_filled):
if part.startswith("{"):
if part.endswith("s}"):
parts_filled[i] = ";".join(map(str, curly_list_params[part[1:-1]]))
elif part.endswith("}"):
parts_filled[i] = str(curly_params[part[1:-1]])
return "/".join(parts_filled)
def _chunks(self, li, k):
"""Splits a list in list of k elements, e.g.
>>> split([2, 3, 5, 7, 11, 13, 17, 19], 3)
[[2, 3, 5], [7, 11, 13], [17, 19]]"""
return [li[i:(i + k)] for i in xrange(0, len(li), k)]
def fetch_one(self, command, subcommand=False, parse_curly_parameters=True, **kwargs):
"""Returns one page of results for a given command;
EXAMPLE:
>>> seapi.fetch_one("users/{ids}", ids=[1,3,7,9,13], site="stackoverflow")
>>> seapi.fetch_one("posts", order="desc", sort="votes", site="stackoverflow", page=3)"""
parameters = self.default_params.copy()
if parse_curly_parameters:
parts, curly_params, curly_list_params = self._find_placeholders(command, **kwargs)
command = self._combine_placeholders(parts, curly_params, curly_list_params)
parameters.update(
dict([(k, v) for k, v in kwargs.items()
if (k not in curly_params) and (k not in curly_list_params)])
)
else:
parameters.update(kwargs)
url = "%s/%s/%s" % (self.api_address, self.api_version, command)
t0 = time()
r = requests.get(url, params=parameters)
if not subcommand:
self.last_response_times = []
self.last_response_times.append(time() - t0)
data = json.loads(r.content)
self.last_call = [url, parameters]
self.last_status = dict([(k, data[k]) for k in data if k != 'items'])
return data['items']
def fetch(self, command, starting_page=1, page_limit=2000, print_progress=True, min_delay=0.05, **kwargs):
"""Returns all pages (withing the limit) of results for a given command;
automatically splits lists for {ids} in appropriate chunks
EXAMPLE -> check for fetch_one (without 'page' parameter!)
NOTE: Here is a lot of room for improvements and additional features, e.g.:
- gevent for concurrency to get optimal rate
- dealing with 'Violation of backoff parameter' """
res = []
self.last_response_times = []
parts, curly_params, curly_list_params = self._find_placeholders(command, **kwargs)
parameters = self.default_params.copy()
parameters.update(
dict([(k, v) for k, v in kwargs.items()
if (k not in curly_params) and (k not in curly_list_params)])
)
if curly_list_params:
assert(len(curly_list_params) == 1)
curly_name, curly_list = curly_list_params.items()[0]
pieces = self._chunks(curly_list, parameters['pagesize'])
for piece in pieces:
command = self._combine_placeholders(parts, curly_params, {curly_name: piece})
res += self.fetch_one(command, subcommand=True, parse_curly_parameters=False, **parameters)
# ^ maybe also with try/except/once_again?
if self.last_response_times[-1] < min_delay:
sleep(min_delay - self.last_response_times[-1])
if print_progress:
print "waited %.2f" % (min_delay - self.last_response_times[-1])
else:
command = self._combine_placeholders(parts, curly_params, {})
if print_progress:
print "Fetching pages: ",
for i in xrange(starting_page, starting_page + page_limit):
if print_progress:
print i,
try:
res += self.fetch_one(command, subcommand=True, parse_curly_parameters=False, page=i, **parameters)
except:
print self.last_status
print "waiting 60s and trying once more" # mainly for 'Violation of backoff parameter'
sleep(60.)
res += self.fetch_one(command, subcommand=True, parse_curly_parameters=False, page=i, **parameters)
if not self.last_status['has_more']:
break
if self.last_response_times[-1] < min_delay:
sleep(min_delay - self.last_response_times[-1])
if print_progress:
print "waited %.2f" % (min_delay - self.last_response_times[-1])
return res
def status_of(self, command, **kwargs):
self.fetch_one(command, **kwargs)
return self.last_status