-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwowitem_fetch.py
320 lines (251 loc) · 7.95 KB
/
wowitem_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import pycurl
import cStringIO
import time
import os.path
import sys
import anydbm
import re
import glob
import shutil
exec file("wowauc.conf", "rt").read()
#if ("item_locales" not in dir()):
# item_locales = "en_US ru_RU"
ids = file(items_ids, "rt").read().split()
locales = items_locales.split()
CURDIR = os.path.dirname(os.path.abspath(__file__)) + "/"
SAVEDIR = CURDIR + dir_fetched_items + "/"
DBFILE = re.sub(r"/+$", "", os.path.abspath(SAVEDIR)) + ".anydbm"
QUIET = '-q' in sys.argv
db = None # value will initialized in init_d()
class FS_Base:
def __init__(self, basedir):
self.open(basedir)
return
def open(self, basedir):
self.basedir = basedir
return
def close(self):
return
def has_item(self, id, loc):
if type(id) not in (int, long): id = int(id)
fname = "%s/%06d-%s.json" % (self.basedir, id, loc)
return os.path.exists(fname)
def put(self, id, loc, json):
if type(id) not in (int, long): id = int(id)
fname = "%s/%06d-%s.json" % (self.basedir, id, loc)
file(fname, "wt").write(json)
def get(self, id, loc):
if not self.has_item(id, loc): return None
if type(id) not in (int, long): id = int(id)
fname = "%s/%06d-%s.json" % (self.basedir, id, loc)
return file(fname, "rt").read()
def remove(self, id, loc):
if type(id) not in (int, long): id = int(id)
fname = "%s/%06d-%s.json" % (self.basedir, id, loc)
os.remove(fname)
return
def keys(self):
x = re.compile(r".*/(\d+)-(\w+)\.json$")
R = []
for k in glob.glob("%s/*.json" % self.basedir):
rx = x.search(k)
if not rx:
print "BAD KEY: {%s}" % k
continue
R.append({'id': int(rx.group(1)), 'loc': rx.group(2)})
return R
###
class D_Base:
def __init__(self, dbfile):
self.open(dbfile)
return
def backup_file(self):
S = ('.5','.4','.3','.2','.1', '')
for i in range(len(S) - 1):
dst = self.fname + S[i]
src = self.fname + S[i + 1]
if os.path.exists(src):
if os.path.exists(dst):
os.remove(dst)
if src == self.fname:
shutil.copy2(src, dst)
else:
shutil.move(src, dst)
return
def open(self, fname):
self.fname = fname
self.backup_file()
self.db = anydbm.open(self.fname, "c", 0666)
return
def close(self):
self.db.sync()
self.db.close()
return
def has_item(self, id, loc):
if type(id) not in (int, long): id = int(id)
key ="%06d-%s" % (id, loc)
return self.db.has_key(key)
def put(self, id, loc, json):
if type(id) not in (int, long): id = int(id)
key ="%06d-%s" % (id, loc)
self.db[key] = json
# self.db.sync()
return
def get(self, id, loc):
if type(id) not in (int, long): id = int(id)
key ="%06d-%s" % (id, loc)
return self.db.get(key, None)
def remove(self, id, loc):
if type(id) not in (int, long): id = int(id)
key ="%06d-%s" % (id, loc)
del self.db[key]
return
def keys(self):
x = re.compile(r"^(\d{6})-(\w+)$")
R = []
for k in self.db.keys():
rx = x.search(k)
if not rx:
print "BAD KEY: {%s}" % k
continue
R.append({'id': int(rx.group(1)), 'loc': rx.group(2)})
return R
####################################################################
def baseUpdate(src, dst):
n_copied = 0
n_processed = 0
for R in src.keys():
id = R['id']
loc = R['loc']
if dst.has_item(id, loc):
print "%06d-%-6s skipped" % (id, loc)
else:
print "%06d-%-6s stored" % (id, loc)
dst.put(id, loc, src.get(id, loc))
n_copied += 1
n_processed += 1
print "done. processed: %d, copied: %d" % (n_processed, n_copied)
return
def checkJSONs(src):
n_good = 0
n_bad = 0
for R in src.keys():
id = R['id']
loc = R['loc']
v = src.get(id, loc)
if v.find('"id":%d' % id) == -1:
print "%06d-%-6s bad: %s" % (id, loc, v)
db.remove(id, loc)
n_bad += 1
else:
n_good += 1
print "done. good: %d, bad: %d" % (n_good, n_bad)
return
def check404(src):
n_good = 0
n_bad = 0
for R in src.keys():
id = R['id']
loc = R['loc']
v = src.get(id, loc)
if v.find('ERROR_404' % id) == -1:
n_good += 1
else:
print "%06d-%-6s delete: %s" % (id, loc, v)
db.remove(id, loc)
n_bad += 1
print "done. good: %d, bad: %d" % (n_good, n_bad)
return
def dumphdr(hdr):
if QUIET: return
print "%% got headers: %s" % repr(hdr)
return
def fetch(region, db):
left = items_fetch_per_session
if not QUIET:
print "* fetching for %d unfetched items" % left
c = pycurl.Curl()
c.setopt(c.CONNECTTIMEOUT, 15)
c.setopt(c.TIMEOUT, 15)
if "http_proxy" in dir():
PXY = http_proxy
else:
PXY = os.getenv("http_proxy")
if PXY:
c.setopt(c.PROXY, PXY)
c.setopt(c.ENCODING, 'gzip')
c.setopt(c.FOLLOWLOCATION, True)
c.setopt(c.HEADERFUNCTION, dumphdr)
c.setopt(c.HTTPHEADER, ['Pragma: no-cache', 'Cache-Control: no-cache'])
for id in ids:
if left <= 0:
print "limit reached"
break
iid = int(id)
showed = False
for loc in locales:
if db.has_item(iid, loc):
#fname = "%s/%06d-%s.json" % (SAVEDIR, iid, loc)
#if os.path.exists(fname):
continue
buf = cStringIO.StringIO()
c.setopt(c.WRITEFUNCTION, buf.write)
url = "http://%s.battle.net/api/wow/item/%d?locale=%s" % (region, iid, loc)
c.setopt(c.URL, url)
if not QUIET:
if not showed:
print "* get item %d" % iid
showed = True
print "* retrieve url %s" % url
c.perform()
retcode = c.getinfo(pycurl.HTTP_CODE)
left -= 1
s = buf.getvalue()
buf.close()
if retcode == 200:
if not QUIET:
print "got data"
print s
print "--------------------"
db.put(iid, loc, s)
else:
print ""
print "%06d-%s ERROR: item %d got retcode %d" % (iid, loc, iid, retcode)
print "url: %s" % url
print ""
db.put(iid, loc, '{"id":%d,"name":"ITEM#%06d","description":"ERROR_404"}')
#file(fname, "wt").write(s)
# r = s.replace(':false', ':False').replace(':true', ':True')
# V = eval(r)
if not QUIET:
print "* done"
return
if __name__ == '__main__':
if False:
print "open old base"
db_old= FS_Base(SAVEDIR)
print "open new base"
db = D_Base(DBFILE)
print "update new base"
baseUpdate(db_old, db)
db_old.close()
db.close()
sys.exit(0)
if False:
print "open base"
db = D_Base(DBFILE)
print "check for broken entries"
checkJSONs(db)
# check404(db)
print "close base"
db.close()
print "done"
sys.exit(0)
db = D_Base(DBFILE)
print "fetch new items to new base"
fetch(items_realms, db)
db.close()
print "done"