-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlutil.py
566 lines (427 loc) · 20 KB
/
sqlutil.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# -*- coding: utf-8 -*-
import sqlite3
import csv
import random
class SqlUtil(object):
def __init__(self):
self.conn = None
def connect(self):
self.conn = sqlite3.connect('zhihu.db')
def create_account_table(self):
self.connect()
try:
self.conn.execute('''CREATE TABLE account
(id INTEGER PRIMARY KEY AUTOINCREMENT ,
type CHAR(20),
username CHAR(50),
password CHAR(50),
fullname CHAR(100),
avatarUrl CHAR(100),
link CHAR(100),
headline CHAR(100),
location CHAR(100),
business CHAR(100),
employment CHAR(100),
education CHAR(100),
description CHAR(200),
sinaWeiboUrl CHAR(100),
gender CHAR(10),
avatar CHAR(10),
status CHAR(10),
remark CHAR(100),
create_time TIMESTAMP default (datetime('now', 'localtime')) );''')
except:
pass
self.conn.close()
def create_task_table(self):
self.connect()
try:
self.conn.execute('''CREATE TABLE task
(id INTEGER PRIMARY KEY AUTOINCREMENT ,
type CHAR(20) ,
task_url CHAR(200) ,
task_count CHAR(20) ,
begin_count CHAR(20) ,
current_count CHAR(20) ,
status CHAR(10) ,
task_order CHAR(20) ,
task_interval CHAR(20) ,
customer CHAR(100) ,
deliver CHAR(10) ,
remark CHAR(200) ,
create_time TIMESTAMP default (datetime('now', 'localtime'))
);''')
except:
pass
self.conn.close()
def create_history_table(self):
self.connect()
try:
self.conn.execute('''CREATE TABLE history
(id INTEGER PRIMARY KEY AUTOINCREMENT ,
task_url CHAR(200) ,
user_name CHAR(60) ,
status CHAR(10) ,
create_time TIMESTAMP default (datetime('now', 'localtime'))
);''')
except:
pass
self.conn.close()
def create_setting_table(self):
self.connect()
try:
self.conn.execute('''CREATE TABLE setting
(id INTEGER PRIMARY KEY AUTOINCREMENT ,
type CHAR(20) ,
value1 CHAR(60) ,
value2 CHAR(60) ,
value3 CHAR(60) );''')
except:
pass
self.conn.close()
def create_zhihu_user_info_table(self):
self.connect()
try:
self.conn.execute('''CREATE TABLE zhihu_user_info
(id INTEGER PRIMARY KEY AUTOINCREMENT ,
name CHAR(100) ,
avatarUrl CHAR(200) ,
link CHAR(200) ,
headline CHAR(200) ,
location CHAR(100) ,
business CHAR(100) ,
employment CHAR(100),
education CHAR(100) ,
description CHAR(600) ,
sinaWeiboUrl CHAR(200),
gender CHAR(20),
create_time TIMESTAMP default (datetime('now', 'localtime'))
);''')
except:
pass
self.conn.close()
def drop_task_table(self):
self.connect()
self.conn.execute('''DROP TABLE task''')
self.conn.close()
def drop_account_table(self):
self.connect()
self.conn.execute('''DROP TABLE account''')
self.conn.close()
def drop_history_table(self):
self.connect()
self.conn.execute('''DROP TABLE history''')
self.conn.close()
def drop_setting_table(self):
self.connect()
self.conn.execute('''DROP TABLE setting''')
self.conn.close()
def drop_zhihu_user_info_table(self):
self.connect()
self.conn.execute('''DROP TABLE zhihu_user_info''')
self.conn.close()
def check_account(self, username):
self.connect()
select_sql = "SELECT id, username, password, type, status, remark FROM account WHERE username = '%s'" % (username)
cursor = self.conn.execute(select_sql)
if len(cursor) > 0:
print('exists')
return True
self.conn.close()
def insert_task(self, type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark):
self.connect()
insert_sql = "INSERT INTO task(type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark) VALUES ( \'"+type+"',\'" +task_url+ "\',\'" +task_count+ "\',\'" +begin_count+ "\' ,\'" +current_count+ "\',\'" +status+ "\',\'" +task_order+ "\',\'" +task_interval+ "\',\'" +customer+ "\',\'" +deliver+ "\',\'" +remark+ "\')"
#print('insert_sql = '+insert_sql)
self.conn.execute(insert_sql)
self.conn.commit()
self.conn.close()
def select_task(self):
self.connect()
select_sql = 'SELECT id, type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark, create_time FROM task'
cursor = self.conn.execute(select_sql)
tasks = []
for row in cursor:
tasks.append(row)
self.conn.close()
return tasks
def delete_task(self, taskUrl):
self.connect()
delete_sql = "DELETE FROM task WHERE task_url = '%s'" % (taskUrl)
cursor = self.conn.execute(delete_sql)
self.conn.commit()
self.conn.close()
def query_tasks(self, type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark):
self.connect()
select_sql = "SELECT id, type, task_url, task_count, begin_count,current_count, status, task_order, task_interval, customer, deliver, remark, create_time FROM task WHERE type LIKE '%"+type+"%' AND task_url LIKE '%"+task_url+"%' AND task_count LIKE '%"+task_count+"%' AND begin_count LIKE '%"+begin_count+"%' AND current_count LIKE '%"+current_count+"%' AND status LIKE '%"+status+"%' AND task_order LIKE '%"+task_order+"%' AND task_interval LIKE '%"+task_interval+"%' AND customer LIKE '%"+customer+"%' AND deliver LIKE '%"+deliver+"%' AND remark LIKE '%"+remark+"%' "
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
tasks = []
for row in cursor:
tasks.append(row)
self.conn.close()
return tasks
def get_pending_task(self):
self.connect()
select_sql = "SELECT id, type, task_url, task_count, begin_count, current_count, task_interval FROM task WHERE status = '待执行'"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
tasks = []
for row in cursor:
tasks.append(row)
self.conn.close()
if len(tasks) > 0:
return tasks[0]
else:
return None
def get_task_begin_count(self, task_url):
self.connect()
select_sql = "SELECT begin_count FROM task WHERE task_url = '"+task_url+"'"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
begin_count = '0'
for row in cursor:
begin_count = row[0]
self.conn.close()
return begin_count
def get_task_current_count(self, task_url):
self.connect()
select_sql = "SELECT current_count FROM task WHERE task_url = '"+task_url+"'"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
current_count = '0'
for row in cursor:
current_count = row[0]
self.conn.close()
return current_count
def get_pending_accounts(self, task_url):
#print('get_pending_accounts')
self.connect()
select_sql = "SELECT id, username, password, avatar, avatarUrl, headline FROM account WHERE status = '正常' AND NOT EXISTS(SELECT 'x' FROM history WHERE task_url = '"+task_url+"' AND user_name = username)"
#select_sql = "SELECT id, username, password FROM account WHERE status = '正常' "
cursor = self.conn.execute(select_sql)
accounts = []
for row in cursor:
accounts.append(row)
self.conn.close()
return accounts
def get_random_account(self):
self.connect()
select_sql = "SELECT id, username, password FROM account WHERE status = '正常' "
cursor = self.conn.execute(select_sql)
accounts = []
for row in cursor:
accounts.append(row)
self.conn.close()
i = random.randint(0, len(accounts) - 1)
return accounts[i]
def query_task(self, id):
self.connect()
select_sql = "SELECT id, type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark, create_time FROM task WHERE id = "+id
print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
task = []
for row in cursor:
task.append(row)
self.conn.close()
return task
def update_task(self, type, task_url, task_count, begin_count, current_count, status, task_order, task_interval, customer, deliver, remark, id):
self.connect()
update_sql = "UPDATE task SET type = '"+type+"', task_url = '"+task_url+"',task_count = '"+task_count+"',begin_count = '"+begin_count+"',current_count = '"+current_count+"',status = '"+status+"',task_order = '"+task_order+"',task_interval = '"+task_interval+"',customer = '"+customer+"',deliver = '"+deliver+"',remark = '"+remark+"' WHERE id = " + id
cursor = self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def update_task_batch(self, status, task_order, task_interval, deliver, id):
self.connect()
update_sql = "UPDATE task SET status = '"+status+"',task_order = '"+task_order+"',task_interval = '"+task_interval+"' ,deliver = '"+deliver+"' WHERE id = " + id
cursor = self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def update_task_status(self, status, id):
self.connect()
update_sql = "UPDATE task SET status = '"+status+"' WHERE id = " + id
self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def update_task_begin_count(self, begin_count, id):
self.connect()
update_sql = "UPDATE task SET begin_count = '"+begin_count+"' WHERE id = " + id
self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def update_task_current_count(self, current_count, id):
self.connect()
update_sql = "UPDATE task SET current_count = '"+current_count+"' WHERE id = " + id
self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def insert_account(self, username, password, fullname, avatarUrl, type, avatar, status, remark):
self.connect()
insert_sql = "INSERT INTO account(username, password, fullname, avatarUrl,type, avatar, status, remark) VALUES ( \'"+username+"',\'" +password+ "\',\'"+fullname+"',\'" +avatarUrl+ "\',\'" +type+ "\',\'" +avatar+ "\',\'" +status+ "\',\'" +remark+ "\')"
self.conn.execute(insert_sql)
self.conn.commit()
self.conn.close()
def select_account(self):
self.connect()
select_sql = 'SELECT id, username, password, type,avatar, status, remark FROM account'
cursor = self.conn.execute(select_sql)
accounts = []
for row in cursor:
accounts.append(row)
self.conn.close()
return accounts
def query_accounts(self, type, username, password, avatar, status, remark):
self.connect()
select_sql = "SELECT id, username, password, type,avatar, status, remark FROM account WHERE type LIKE '%"+type+"%' AND username LIKE '%"+username+"%' AND password LIKE '%"+password+"%' AND avatar LIKE '%"+avatar+"%' AND status LIKE '%"+status+"%' AND remark LIKE '%"+remark+"%' "
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
accounts = []
for row in cursor:
accounts.append(row)
self.conn.close()
return accounts
def update_account(self, username, status, errMsg):
self.connect()
update_sql = "UPDATE account SET status = '"+status+"' , remark = '"+errMsg+"' WHERE username = '"+username+"'"
cursor = self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def delete_account(self, username):
self.connect()
delete_sql = "DELETE FROM account WHERE username = '%s'" % (username)
cursor = self.conn.execute(delete_sql)
self.conn.commit()
self.conn.close()
def insert_history(self, task_url, user_name, status):
self.connect()
insert_sql = "INSERT INTO history(task_url, user_name, status) VALUES ( \'"+task_url+"',\'" +user_name+ "\',\'" +status+ "\')"
self.conn.execute(insert_sql)
self.conn.commit()
self.conn.close()
def select_history(self):
self.connect()
select_sql = 'SELECT task_url, user_name, status FROM history'
cursor = self.conn.execute(select_sql)
historys = []
for row in cursor:
historys.append(row)
self.conn.close()
return historys
def insert_setting(self, type, value1, value2, value3):
self.connect()
insert_sql = "INSERT INTO setting(type, value1, value2, value3) VALUES ( \'"+type+"',\'" +value1+ "\',\'" +value2+ "\',\'" +value3+ "\')"
self.conn.execute(insert_sql)
self.conn.commit()
self.conn.close()
def update_setting(self, type, value1, value2, value3):
self.connect()
update_sql = "UPDATE setting SET value1 = '"+value1+"', value2 = '"+value2+"',value3 = '"+value3+"' WHERE type = '" + type +"'"
#print('update_sql = '+update_sql)
cursor = self.conn.execute(update_sql)
self.conn.commit()
self.conn.close()
def select_setting(self):
self.connect()
select_sql = 'SELECT type, value1, value2, value3 FROM setting'
cursor = self.conn.execute(select_sql)
settings = []
for row in cursor:
settings.append(row)
self.conn.close()
return settings
def sava_setting(self, type, value1, value2, value3):
settings = self.query_settings(type)
if len(settings) == 0:
self.insert_setting(type, value1, value2, value3)
elif len(settings) > 0:
self.update_setting(type, value1, value2, value3)
def query_settings(self, type):
self.connect()
select_sql = "SELECT value1, value2, value3 FROM setting WHERE type LIKE '%"+type+"%' "
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
settings = []
for row in cursor:
settings.append(row)
self.conn.close()
return settings
def read_csv(self, fn='data1.csv'):
with open(fn, encoding="utf-8") as fr:
csvfr = csv.reader(fr)
rows = [row for row in csvfr]
return rows
def insert_zhihu_user_info(self):
self.connect()
rows = self.read_csv('./data/zhihu_user_info.csv')
del rows[0]
for row in rows:
#print('row[0] = '+row[0])
try:
insert_sql = "INSERT INTO zhihu_user_info(name, avatarUrl, link, headline, location, business, employment, education, description, sinaWeiboUrl) VALUES ( \'"+row[0]+"',\'" +row[1]+ "\',\'" +row[2]+ "\',\'" +row[3]+ "\',\'" +row[4]+ "\',\'" +row[5]+ "\',\'" +row[6]+ "\',\'" +row[7]+ "\',\'" +row[8]+ "\',\'" +row[9]+ "\')"
#print('insert_sql = '+insert_sql)
self.conn.execute(insert_sql)
except:
pass
self.conn.commit()
self.conn.close()
def query_user_info(self):
self.connect()
select_sql = "SELECT name, avatarUrl, link, headline, location, business, education, description FROM zhihu_user_info"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
user_infos = []
for row in cursor:
user_infos.append(row)
self.conn.close()
return user_infos
def get_fullname_avatar(self):
self.connect()
select_sql = "SELECT u.name, u.avatarUrl, u.headline FROM zhihu_user_info u WHERE headline <> '' AND NOT EXISTS(SELECT 'x' FROM account a WHERE a.avatarUrl = u.avatarUrl)"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
user_infos = []
for row in cursor:
user_infos.append(row)
self.conn.close()
i = random.randint(0, len(user_infos) - 1)
return user_infos[i][0], user_infos[i][1]
def get_headline(self, avatarUrl):
self.connect()
select_sql = "SELECT u.headline FROM zhihu_user_info u WHERE avatarUrl = '"+avatarUrl+"'"
#print('select_sql = '+select_sql)
cursor = self.conn.execute(select_sql)
headline = ' '
for row in cursor:
headline = row[0]
self.conn.close()
return headline
if __name__ == '__main__':
sqlUtil = SqlUtil()
#sqlUtil.drop_account_table()
#name, avatarUrl = sqlUtil.get_fullname_avatar()
#print('name = '+name)
#print('avatarUrl = '+avatarUrl)
#current_count = sqlUtil.get_task_current_count('123')
#print('current_count = '+ current_count)
#print('password = '+ account[2])
#sqlUtil.create_zhihu_user_info_table()
#sqlUtil.insert_zhihu_user_info()
#sqlUtil.insert_history('https://www.zhihu.com/question/55761843/answer/181595645', '17065382153', '正常')
#fullname, avatarUrl, headline = sqlUtil.get_fullname_avatar()
#print('fullname = '+fullname)
#print('avatarUrl = '+avatarUrl)
#print('headline = '+headline)
#headline = sqlUtil.get_headline('https://pic1.zhimg.com/db1cc506845369c27761eec0c3d21294_is.jpg')
#print('headline = '+headline)
'''
user_infos = sqlUtil.query_user_info()
for user_info in user_infos:
print('name = '+user_info[0])
print('avatarUrl = '+user_info[1])
print('link = '+user_info[2])
print('headline = '+user_info[3])
'''
task = sqlUtil.get_pending_task()
if task == None:
print('None')
else:
print('taskUrl = '+str(task[2]))