forked from ajing2/coupon-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seckill.py
229 lines (139 loc) · 5.23 KB
/
seckill.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import os
from base import MatchesItem, Seckill
from network import Network
from utils import OutputPath
class SeckillInfo:
def __init__(self, gid, db):
self.seckillInfo = None
self.matchesList = None
self.skuIdList = None
self.seckillList = None
self.gid = gid
self.db = db
def update(self, page=1, force=False):
path = OutputPath.getDataPath('seckill-{}-{}'.format(self.gid, page), 'json')
if page is 1:
url = 'https://ms.m.jd.com/seckill/seckillList.json?gid={}'.format(self.gid)
else:
url = 'https://ms.m.jd.com/seckill/seckillListPage.json?isPagination=true&gid={}&page={}'.format(self.gid,
page)
ret = Network.saveHttpData(path, url, force=force)
if ret < 0:
return False
return self.parse(path)
def parse(self, path):
def getJsonString(path):
with open(path) as fh:
return 0, fh.read()
return -1, ""
ret, data = getJsonString(path)
if ret < 0:
print 'Wrong format: {}'.format(path)
return False
obj = json.loads(data)
self.seckillInfo = obj.pop('seckillInfo')
return True
def getMatchesList(self):
if self.matchesList is not None:
return self.matchesList
self.matchesList = list()
for data in self.seckillInfo['matchesList']:
self.matchesList.append(MatchesItem(data))
return self.matchesList
def getSkuIdList(self):
if self.skuIdList is not None:
return self.self.skuIdList
self.skuIdList = list()
for data in self.seckillInfo['itemList']:
self.skuIdList.append(data['wareId'])
return self.skuIdList
def getSeckillList(self):
if self.seckillList is not None:
return self.seckillList
self.getMatchesList()
# Find current matchesItem
for matchesItem in self.matchesList:
if matchesItem.data['gid'] == self.gid:
startTime = matchesItem.data['startTime']
endTime = matchesItem.data['endTime']
break;
else:
return None
self.seckillList = list()
self.newSkuIdList = list()
for data in self.seckillInfo['itemList']:
if self.db.findOne('SeckillTable', skuid=data['wareId']):
# Already exists
continue
self.newSkuIdList.append(data['wareId'])
seckill = Seckill(data)
seckill.setPeriod(startTime, endTime)
seckill.insert(self.db, 'SeckillTable')
self.seckillList.append(seckill)
return self.seckillList
class SeckillManager:
def __init__(self, db, isLocal=False):
self.db = db
self.isLocal = isLocal
self.skuIdList = None
self.seckillInfoList = None
self.seckillList = None
try:
os.mkdir('data')
except OSError:
pass
def getGidList(self):
ENTRANCE_GID = 26
seckillInfo = SeckillInfo(ENTRANCE_GID, self.db)
if not seckillInfo.update(force=True):
return None
matchesList = seckillInfo.getMatchesList()
gids = list()
for matchesItem in matchesList:
gids.append(matchesItem.data['gid'])
return gids
def updateSeckillInfoList(self):
self.seckillInfoList = list()
gids = self.getGidList()
for gid in gids:
page = 1
totalPage = 1
while page is 1 or page < totalPage:
seckillInfo = SeckillInfo(gid, self.db)
if not seckillInfo.update(page):
continue
page = seckillInfo.seckillInfo['page'] + 1
totalPage = seckillInfo.seckillInfo['totalPage']
self.seckillInfoList.append(seckillInfo)
def updateSkuIdList(self):
self.skuIdList = list()
for seckillInfo in self.seckillInfoList:
self.skuIdList.extend(seckillInfo.getSkuIdList())
def updateSeckillList(self):
self.seckillList = list()
for seckillInfo in self.seckillInfoList:
self.seckillList.extend(seckillInfo.getSeckillList())
def updateNewSkuIdList(self):
self.newSkuIdList = list()
for seckillInfo in self.seckillInfoList:
self.newSkuIdList.extend(seckillInfo.newSkuIdList)
def updateDb(self):
if self.skuIdList is None or len(self.skuIdList) == 0:
return
sql = ''' SELECT COUNT(*) AS num FROM `SeckillTable`
WHERE skuid NOT IN ({}) '''.format(', '.join(self.skuIdList))
result = self.db.query(sql)
for row in result:
print 'Delete', row['num'], 'records in SeckillTable'
sql = ''' DELETE FROM `SeckillTable`
WHERE skuid NOT IN ({}) '''.format(', '.join(self.skuIdList))
self.db.query(sql)
def update(self):
self.updateSeckillInfoList()
self.updateSkuIdList()
self.updateSeckillList()
self.updateNewSkuIdList()
self.updateDb()