-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_process.py
281 lines (240 loc) · 8.97 KB
/
data_process.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
# coding:utf-8
import sqlite3
import csv
class DataProcess(object):
def __init__(self, dbname, table_name):
self.dbname = dbname
self.table_name = table_name
self.con = sqlite3.connect('test.db')
def get_goods(self):
cur = self.con.cursor()
cur.execute('select name from sqlite_master where type="table"')
values = cur.fetchall()
table_need_create = True
for x in values:
if x[0] == self.table_name:
table_need_create = False
break
if table_need_create is True:
cur.close()
return False, None
cur.execute('select url, price, name from {}'.format(self.table_name))
values = cur.fetchall()
cur.close()
goods_data = []
for x in values:
good = {}
good['url'] = x[0]
good['price'] = x[1]
good['name'] = x[2]
goods_data.append(good)
return True, goods_data
def add_good(self, url, price=-1, name='', date=''):
cur = self.con.cursor()
cur.execute('select name from sqlite_master where type="table"')
values = cur.fetchall()
table_need_create = True
for x in values:
if x[0] == self.table_name:
table_need_create = False
break
if table_need_create is True:
print("No table:" + self.table_name + ", need create.")
try:
cur.execute(
'create table {} (url text primary key not null, price real not null, name text, date text)'
.format(self.table_name))
except Exception as e:
print("create goods table error: " + str(e))
cur.close()
return False
try:
cur.execute(
'insert into {} values (?, ?, "{}", ?)'.format(
self.table_name, name), (url, price, date))
except Exception as e:
print("add good error: " + str(e))
cur.close()
return False
self.con.commit()
cur.close()
print("add good over.")
return True
def update_good(self, url, price, name, date):
cur = self.con.cursor()
cur.execute('select name from sqlite_master where type="table"')
values = cur.fetchall()
table_need_create = True
for x in values:
if x[0] == self.table_name:
table_need_create = False
break
if table_need_create is True:
print("No table: " + self.table_name)
cur.close()
return False
try:
cur.execute(
'update {} set price = {}, name = "{}", date = "{}" where url = "{}"'
.format(self.table_name, price, name, date, url))
except Exception as e:
print("update good's data error:" + str(e))
cur.close()
return False
self.con.commit()
cur.close()
print("update good over.")
return True
def delete_good(self, url):
cur = self.con.cursor()
cur.execute('select name from sqlite_master where type="table"')
values = cur.fetchall()
table_need_create = True
for x in values:
if x[0] == self.table_name:
table_need_create = False
break
if table_need_create is True:
print("No table:" + self.table_name)
cur.close()
return False
try:
cur.execute('delete from {} where url = "{}"'.format(
self.table_name, url))
except Exception as e:
print("delete good's data error:" + str(e))
cur.close()
return False
self.con.commit()
cur.close()
print("delete good over.")
return True
def find_good(self, url):
cur = self.con.cursor()
try:
cur.execute(
'select url from {} where url="{}"'.format(
self.table_name, url))
values = cur.fetchall()
cur.close()
if len(values) > 0:
return True
return False
except Exception as e:
print("find_good error:" + str(e))
cur.close()
return False
def export_csv(self, csvfilename):
cur = self.con.cursor()
try:
cur.execute('select * from {}'.format(self.table_name))
with open(csvfilename, 'w') as f:
f.write(",".join([x[0] for x in cur.description]) + "\n")
for x in cur:
f.write(",".join([str(i) for i in x]) + "\n")
cur.close()
return True
except Exception as e:
print("export_csv error:" + str(e))
return False
def urls_in_csvfile(self, csvfilename):
urls = []
try:
with open(csvfilename) as f:
f_csv = csv.DictReader(f)
for row in f_csv:
urls.append(row['url'])
return urls
except Exception as e:
print("open %s error: %s" % (csvfilename, str(e)))
return None
def add_from_csv(self, csvfilename):
# try:
# with open(csvfilename) as f:
# f_csv = csv.DictReader(f)
# for row in f_csv:
# ret = self.find_good(row['url'])
# if ret is False:
# print("There were no good: %s in database, add it." %
# row['url'])
# self.add_good(row['url'])
# else:
# print("Find good: %s in database." % row['url'])
# except Exception as e:
# print("open %s error: %s" % csvfilename, str(e))
urls = self.urls_in_csvfile(csvfilename)
if urls is None:
return False
for url in urls:
ret = self.find_good(url)
if ret is False:
print("There were no good: %s in database, add it." %
url)
self.add_good(url)
else:
print("Find good: %s in database." % url)
def sync_with_csv(self, csvfilename):
cur = self.con.cursor()
cur.execute('select name from sqlite_master where type="table"')
values = cur.fetchall()
table_need_create = True
for x in values:
if x[0] == self.table_name:
table_need_create = False
break
if table_need_create is True:
print("No table:" + self.table_name + ", need create.")
try:
cur.execute(
'create table {} (url text primary key not null, price real not null, name text, date text)'
.format(self.table_name))
except Exception as e:
print("create goods table error: " + str(e))
cur.close()
return False
cur.execute('select url from {}'.format(self.table_name))
values = cur.fetchall()
database_urls = [x[0] for x in values]
cur.close()
ret = self.add_from_csv(csvfilename)
if ret is False:
return False
csv_urls = self.urls_in_csvfile(csvfilename)
for x in database_urls:
if x not in csv_urls:
print("url: %s is not in: %s, delete it." % (x, csvfilename))
self.delete_good(x)
print("sync with csv files: %s over." % csvfilename)
return True
def main():
print("In function main()")
dataprocess = DataProcess('test.db', 'goods')
# ret = dataprocess.add_good(
# 'https://item.jd.com/1208740.html', 79.0,
# 'luoji',
# datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# ret = dataprocess.add_good(
# 'https://item.jd.com/5025715.html', 479.0,
# 'Yeelight 皎月LED卧室吸顶灯纯白版客厅智能吸顶灯调光调色遥控器手机控制现代简约圆形书房灯具',
# datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# ret = True
# if ret is True:
# goods_data = dataprocess.get_goods()
# print("goods_data: " + goods_data)
# ret = dataprocess.update_good(
# 'https://item.jd.com/1208740.html', 79.0,
# '罗技(Logitech)M275(M280)无线鼠标 黑色',
# datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# ret = dataprocess.delete_good(
# 'https://item.jd.com/5025715.html')
# ret = dataprocess.delete_good(
# 'https://item.jd.com/1208740.html')
# goods_data = dataprocess.get_goods()
# print("goods_data: " + goods_data)
ret = dataprocess.find_good('https://item.jd.com/1208740.html')
if ret is True:
print("found it")
else:
print("found nothing")
if __name__ == '__main__':
main()