-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwowauc_fetch.py
172 lines (147 loc) · 4.53 KB
/
wowauc_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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import pycurl
import cStringIO
import time
import os.path
import sys
exec file("wowauc.conf", "rt").read()
CURDIR = os.path.dirname(os.path.abspath(__file__)) + "/"
TMPDIR = CURDIR + dir_fetching + "/"
SAVEDIR = CURDIR + dir_fetched + "/"
QUIET = '-q' in sys.argv
def dumphdr(hdr):
if QUIET: return
print "%% got headers: %s" % repr(hdr)
return
class Writer(object):
def __init__(self, fname):
object.__init__(self)
self._fname = fname
if not QUIET:
print "+ open %s" % self._fname
self._file = open(self._fname, "wt")
self._count = 0
self._gzipped = False
self._data = ''
return
def header(self, s):
if not QUIET:
print "+ got header: {%s}" % repr(s)
k = [x.strip() for x in s.split(':')]
if len(k) < 2:
return
if k[0] == 'Content-Encoding':
self._gzipped = (k[1] == 'gzip')
if not QUIET:
print "+ encoding: %s" % k[1]
return
def write(self, data):
n = len(data)
self._count += n
# if not QUIET:
# s = "+ %d bytes received " % self._count
# sys.stdout.write("%s%s" % (s, '\b' * len(s)))
# sys.stdout.write("%s \r" % s)
self._file.write(data)
self._data += data
return
def close(self):
if not QUIET:
print "\n+ close stream. %d octets wrote" % self._count
self._file.close()
return
def getData(self):
return self._data
def fetch(region, realm):
if not QUIET:
print "* fetch for region=%s, realm=%s" % (region, realm)
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.WRITEFUNCTION, buf.write)
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'])
if not QUIET:
print "* retrieve auction url"
c.setopt(c.URL, "http://%s.battle.net/api/wow/auction/data/%s" % (region, realm))
c.perform()
s = buf.getvalue()
buf.close()
if not QUIET:
print "got data"
print s
print "--------------------"
M = eval(s)
if M.get('status') == 'nok':
if not QUIET:
print "* wowapi does not known about %s:%s" % (region, realm)
return
url = M['files'][0]['url']
t_s = M['files'][0]['lastModified'] / 1000.0
t_gm = time.gmtime(t_s)
t_lo = time.localtime(t_s)
name = "%s_%s_%s.json" \
% (time.strftime(r"%Y%m%d_%H%M%S", t_gm), region, realm)
tname = TMPDIR + name
sname = SAVEDIR + name
ts_gm = time.strftime(r"%Y-%m-%d %H:%M:%S", t_gm)
ts_lo = time.strftime(r"%Y-%m-%d %H:%M:%S", t_lo)
if not QUIET:
d_h = int(time.time() - t_s)
d_s = d_h % 60
d_h /= 60
d_m = d_h % 60
d_h /= 60
print "* timestamp: %s" % ts_gm
print "* tocaltime: %s" % ts_lo
print "* age: %d:%02d:%02d" % (d_h, d_m, d_s)
if not (os.path.exists(tname) or os.path.exists(sname)):
if not QUIET:
print "* save to %s" % tname
print "* retrieve aution data"
c.setopt(c.URL, url)
c.setopt(c.CONNECTTIMEOUT, 15)
c.setopt(c.TIMEOUT, 300)
c.setopt(c.ENCODING, 'gzip')
c.setopt(c.FOLLOWLOCATION, True)
f = Writer(tname)
c.setopt(c.WRITEFUNCTION, f.write)
c.setopt(c.HEADERFUNCTION, f.header)
c.perform()
retcode = c.getinfo(pycurl.HTTP_CODE)
c.close()
f.close()
if not QUIET:
print "* retcode=%d" % retcode
if retcode == 200:
if not QUIET:
print "* good retcode. rename results to %s" % sname
os.rename(tname, sname)
if not QUIET:
print "* ...moved"
else:
if not QUIET:
print "* retrieved data:"
print file(tname).read()
print "* erase results"
os.remove(tname)
else:
if not QUIET:
print "* skip"
if not QUIET:
print "* done"
if __name__ == '__main__':
for item in watchlist.split():
(region, realm) = item.split(':')
fetch(region, realm)