-
Notifications
You must be signed in to change notification settings - Fork 80
/
fetch.py
305 lines (260 loc) · 8.6 KB
/
fetch.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding:utf-8 -*-
"""
WPTools Fetch module.
"""
from __future__ import print_function
try: # python2
from urllib import unquote
except ImportError: # python3
from urllib.parse import unquote
from io import BytesIO
from string import Template
import random
import sys
import certifi
import pycurl
from . import __title__, __contact__, __version__
class WPToolsFetch(object):
"""
Supports MediaWiki:API, RESTBase, Wikidata API HTTP requests
"""
EMOJI = [
u'\U0001f355',
u'\U0001f35c',
u'\U0001f363',
u'\U0001f36a',
u'\U0001f370',
]
QUERY = {
"list": Template((
"${WIKI}/w/api.php?action=query"
"&format=json"
"&formatversion=2"
"&list=${_list}")),
"imageinfo": Template((
"${WIKI}/w/api.php?action=query"
"&format=json"
"&formatversion=2"
"&iiprop=size|url|timestamp"
"&prop=imageinfo"
"&titles=${thing}")),
"parse": Template((
"${WIKI}/w/api.php?action=parse"
"&contentmodel=text"
"&disableeditsection="
"&disablelimitreport="
"&disabletoc="
"&format=json"
"&formatversion=2"
"&prop="
"text|iwlinks|parsetree|wikitext|displaytitle|properties"
"&redirects"
"&page=${thing}")),
"query": Template((
"${WIKI}/w/api.php?action=query"
"&cllimit=500"
"&clshow=!hidden"
"&exintro"
"&format=json"
"&formatversion=2"
"&imlimit=500"
"&inprop=url|watchers"
"&list=random"
"&lllimit=500"
"&pithumbsize=240"
"&ppprop=wikibase_item"
"&prop=categories|contributors|extracts|images|info|langlinks"
"|pageimages|pageprops|pageterms|pageviews"
"&redirects"
"&rnlimit=1"
"&rnnamespace=0"
"&titles=${thing}")),
"random": Template((
"${WIKI}/w/api.php?action=query"
"&format=json"
"&formatversion=2"
"&list=random"
"&rnlimit=1"
"&rnnamespace=0")),
"rest": Template((
"${WIKI}/api/rest_v1${entrypoint}")),
"wikidata": Template((
"${WIKI}/w/api.php?action=wbgetentities"
"&format=json"
"&formatversion=2"
"&ids=${ids}"
"&languages=${lang}"
"&props=${props}"
"&redirects=yes"
"&sites=${site}"
"&titles=${title}"))
}
action = None
info = None
silent = False
thing = None
title = None
def __init__(self, **kwargs):
self.lang = kwargs.get('lang')
self.silent = kwargs.get('silent') or False
self.variant = kwargs.get('variant')
self.verbose = kwargs.get('verbose') or False
self.wiki = kwargs.get('wiki')
self.curl_setup(kwargs.get('proxy'), kwargs.get('timeout'))
def __del__(self):
self.cobj.close()
def curl(self, url):
"""
in favor of python-requests for speed
"""
# consistently faster than requests by 3x
#
# r = requests.get(url,
# headers={'User-Agent': self.user_agent})
# return r.text
crl = self.cobj
try:
crl.setopt(pycurl.URL, url)
except UnicodeEncodeError:
crl.setopt(pycurl.URL, url.encode('utf-8'))
if not self.silent:
print(self.status_line(), file=sys.stderr)
return self.curl_perform(crl)
def curl_perform(self, crl):
"""
performs HTTP GET and returns body of response
"""
bfr = BytesIO()
crl.setopt(crl.WRITEFUNCTION, bfr.write)
crl.perform()
info = curl_info(crl)
if info:
if self.verbose and not self.silent:
for item in sorted(info):
print(" %s: %s" % (item, info[item]), file=sys.stderr)
self.info = info
body = bfr.getvalue()
bfr.close()
return body
def curl_setup(self, proxy=None, timeout=0):
"""
set curl options
"""
crl = pycurl.Curl()
crl.setopt(pycurl.USERAGENT, user_agent())
crl.setopt(pycurl.FOLLOWLOCATION, True)
crl.setopt(pycurl.CAINFO, certifi.where())
if proxy:
crl.setopt(pycurl.PROXY, proxy)
if timeout: # 0 = wait forever
crl.setopt(pycurl.CONNECTTIMEOUT, timeout)
crl.setopt(pycurl.TIMEOUT, timeout)
if self.verbose and not self.silent:
crl.setopt(pycurl.VERBOSE, True)
self.cobj = crl
def query(self, action, thing, pageid=False):
"""
returns API query string
"""
if not self.wiki or self.wiki == 'www.wikidata.org':
self.wiki = "%s.wikipedia.org" % self.lang
tmpl_wiki = self.wiki
if not tmpl_wiki.startswith('http'):
tmpl_wiki = 'https://' + self.wiki
if action == 'rest':
qry = self.QUERY['rest'].substitute(WIKI=tmpl_wiki,
entrypoint=thing)
elif action == 'category':
qry = self.QUERY['list'].substitute(WIKI=tmpl_wiki,
_list='categorymembers')
limit = thing.get('limit')
namespace = thing.get('namespace')
pageid = thing.get('pageid')
title = thing.get('title')
if limit:
qry += "&cmlimit=%d" % limit
if namespace is not None and namespace >= 0:
qry += "&cmnamespace=%d" % namespace
if title:
qry += "&cmtitle=" + title
thing = title
if pageid:
qry += "&cmpageid=%d" % pageid
thing = pageid
elif action == 'wikidata' or action == 'claims':
ids = ''
site = ''
title = ''
props = "info|claims|descriptions|labels|sitelinks"
self.wiki = 'www.wikidata.org'
tmpl_wiki = 'https://' + self.wiki
if thing.get('props'):
props = thing['props']
if thing.get('id'):
ids = thing.get('id')
thing = ids
else:
site = thing.get('site')
title = thing.get('title')
thing = title
qry = self.QUERY['wikidata'].substitute(
WIKI=tmpl_wiki,
ids=ids,
lang=self.variant or self.lang,
props=props,
site=site,
title=title)
else:
qry = self.QUERY[action].substitute(
WIKI=tmpl_wiki,
thing=thing)
if action == 'parse' and pageid:
qry = qry.replace('&page=', '&pageid=').replace('&redirects', '')
if action == 'query' and pageid:
qry = qry.replace('&titles=', '&pageids=')
if self.variant:
qry += '&variant=' + self.variant
self.action = action
self.thing = thing
return qry
def status_line(self):
"""
returns request status line
"""
try:
thing = unquote(self.thing)
except (AttributeError, TypeError):
thing = self.thing
status = "%s (%s) %s" % (self.wiki, self.action,
thing or random.choice(self.EMOJI))
if len(status) > 80:
status = status[:72] + '...'
return status
def curl_info(crl):
"""
returns curl (response) info from Pycurl object
"""
kbps = crl.getinfo(crl.SPEED_DOWNLOAD) / 1000.0
url = crl.getinfo(crl.EFFECTIVE_URL)
url = url.replace("&format=json", '').replace("&formatversion=2", '')
return {"url": url,
"user-agent": user_agent(),
"content": crl.getinfo(crl.CONTENT_TYPE),
"status": crl.getinfo(crl.RESPONSE_CODE),
"bytes": crl.getinfo(crl.SIZE_DOWNLOAD),
"seconds": "%5.3f" % crl.getinfo(crl.TOTAL_TIME),
"kB/s": "%3.1f" % kbps}
def get(action, title):
"""
returns GET result from API
"""
obj = WPToolsFetch()
return obj.curl(obj.query(action, title))
def user_agent():
"""
returns the wptools user-agent string
"""
return "%s/%s (%s) %s" % (__title__,
__version__,
__contact__,
pycurl.version)