forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
black_list_sql.py
92 lines (73 loc) · 2.29 KB
/
black_list_sql.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
# -*-coding=utf-8-*-
# 股市黑名单
from settings import get_mysql_conn,llogger,DATA_PATH
import os
import codecs
logger = llogger('log/blacklist.log')
def create_tb(conn):
cmd = '''CREATE TABLE IF NOT EXISTS `tb_blacklist` (DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP,CODE VARCHAR(6) PRIMARY KEY,NAME VARCHAR(60),REASON TEXT);'''
cur = conn.cursor()
try:
cur.execute(cmd)
conn.commit()
except Exception as e:
logger.info(e)
conn.rollback()
def update_data(filename,conn):
cur = conn.cursor()
with codecs.open(filename, 'r',encoding='utf8') as f:
content = f.readlines()
if not content:
return
for line in content:
(code, name, reason) = line.strip().split(';')
cmd = '''INSERT INTO `tb_blacklist` (CODE,NAME,REASON) VALUES (\"%s\",\"%s\",\"%s\")''' % (code, name, reason)
try:
cur.execute(cmd)
conn.commit()
except Exception as e:
logger.info(e)
logger.info('dup code {}'.format(code))
conn.rollback()
continue
else:
logger.info('insert successfully {}'.format(name))
# 调试
def get_name_number():
filename = os.path.join(DATA_PATH, 'blacklist.csv')
with codecs.open(filename, 'r', encoding='utf8') as f:
content = f.readlines()
if not content:
return
logger.info('len of content {}'.format(len(content)))
code_list = []
for i in content:
code_list.append(i.split(';')[0])
logger.info(code_list)
logger.info(len(set(code_list)))
# 找出重复
seen = set()
dup_list = []
for i in code_list:
if i in seen:
dup_list.append(i)
else:
seen.add(i)
logger.info('dup item {}'.format(dup_list))
def main():
filename = os.path.join(DATA_PATH, 'blacklist.csv')
# 本地更新
logger.info('update local')
db_name = 'db_stock'
conn = get_mysql_conn(db_name, local='local')
create_tb(conn)
update_data(filename,conn)
# 远程更新
# db_name = 'db_stock'
logger.info('update remote')
remote_conn = get_mysql_conn('qdm225205669_db', local='ali')
create_tb(remote_conn)
update_data(filename,remote_conn)
if __name__ == '__main__':
main()
# get_name_number()