forked from ajing2/coupon-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
143 lines (92 loc) · 3.7 KB
/
history.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
import json
from base import Sku, PromotionHistory, PriceHistoryData
from js import JsExecutor
from network import Network
from utils import OutputPath
class PriceHistoryManager:
def __init__(self, db=None):
self.priceHistoryDataList = None
self.db = db
self.executor = JsExecutor('js/huihui.js')
def update(self):
tableNames = ['SkuTable']
for tableName in tableNames:
self.updateHistory(tableName)
def updateHistory(self, tableName):
sql = 'SELECT id FROM HistoryTable LIMIT 1'
result = self.db.query(sql)
sql = 'SELECT skuid, title FROM {}'.format(tableName)
where = ' WHERE skuid NOT IN (SELECT skuid FROM HistoryTable) '
if result is not None:
sql += where
result = self.db.query(sql)
if result is None:
return
for row in result:
if self.priceHistoryDataList is None:
self.priceHistoryDataList = list()
priceHistoryData = self.create(row)
self.priceHistoryDataList.append(priceHistoryData)
def create(self, data):
skuid = data['skuid']
if self.db is not None and self.db.findOne('HistoryTable', skuid=skuid):
# Already exists
return None
title = data['title']
priceHistoryData = PriceHistoryManager.getPriceHistoryData(self.executor, data)
if priceHistoryData is None:
return None
priceHistoryData.insert(self.db, 'HistoryTable')
return priceHistoryData
@staticmethod
def getPriceHistoryData(executor, data):
skuid = data['skuid']
title = data['title']
url = 'http://item.jd.com/{}.html'.format(skuid) # For history searching
# Get URL for price history
url = executor.context.requestPriceInfo(title, url)
# Get price histories
path = OutputPath.getDataPath(skuid, 'js')
ret = Network.saveHttpData(path, url)
#print 'Update', path, ':', ret, ':', title
if ret < 0:
return None
obj = PriceHistoryManager.parse(path)
if obj is None:
return None
return PriceHistoryManager.generatePriceHistoryData(data, obj)
@staticmethod
def parse(path):
def getJsonString(path):
try:
with open(path) as fh:
for line in fh.readlines():
if len(line) > 1024:
start = line.find('{')
end = line.rfind('}')
return line[start:end+1]
except IOError:
pass
return None
data = getJsonString(path)
if not data:
print 'Wrong format: {}'.format(path)
return None
return json.loads(data)
@staticmethod
def generatePriceHistoryData(data, obj):
priceHistoryData = None
promotionHistoryList = None
if 'promotionHistory' in obj.keys():
for promotionHistory in obj['promotionHistory']:
if promotionHistoryList is None:
promotionHistoryList = list()
promotionHistoryList.append(PromotionHistory(promotionHistory))
if 'priceHistoryData' in obj.keys():
priceHistory = obj['priceHistoryData']
priceHistory['skuid'] = data['skuid']
priceHistoryData = PriceHistoryData(priceHistory)
priceHistoryData.updatePromotion(promotionHistoryList)
priceHistoryData.data['list'] = json.dumps(priceHistoryData.data.pop('list'),
ensure_ascii=False, indent=4, sort_keys=True)
return priceHistoryData