-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdatabase.py
139 lines (117 loc) · 3.77 KB
/
database.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
import json
import logging
import sqlite3
import string
from datetime import datetime
import redis
# redis config
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
# 所有的magnet存在这里
REDIS_ALL_KEY = 'all-magnet'
# 进行过转换的magnet
REDIS_USED_KEY = 'used-magnet'
# 能下载的magnet
REDIS_AVAIL_KEY = 'magnet'
# mysql config
MYSQL_HOST = '127.0.0.1'
MYSQL_PORT = 3306
# sqlite3
SQLITE_DATABASE_NAME = 'magnet.db'
class RedisClient:
def __init__(self, host=REDIS_HOST, port=REDIS_PORT):
pool = redis.ConnectionPool(host=host, port=port, db=0)
self.client = redis.Redis(connection_pool=pool)
def add(self, magnet, key=REDIS_ALL_KEY):
self.client.sadd(key, magnet)
def count(self, key=REDIS_ALL_KEY):
return self.client.scard(key)
def get(self, count, keys=REDIS_ALL_KEY):
"""用于转换magnet"""
# keys = (REDIS_ALL_KEY, REDIS_USED_KEY)
diff_set = self.client.sdiff(keys)
length = len(diff_set)
count = count if length > count else length
magnets = [diff_set.pop() for _ in range(count)]
return magnets
def diff(self, keys, count):
diff_set = self.client.sdiff(keys)
length = len(diff_set)
count = count if length > count else length
magnets = [diff_set.pop() for _ in range(count)]
return magnets
class MysqlClient:
pass
class SqliteClient:
def __init__(self, db):
self.db = db
# sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.
self.conn = sqlite3.connect(db, check_same_thread=False)
def insert(self, magnet, data):
insert_sql = '''
insert into {table_name}
(magnet, torrent_name, content, create_date)
values(?, ?, ?, ?);
'''
cursor = self.conn.cursor()
table_name = 'magnet_{}'.format(magnet[-40].lower())
# table_name = 'magnet_{}'.format(magnet[0])
try:
params = (magnet, data.get('name', None), json.dumps(data), datetime.now(),)
cursor.execute(insert_sql.format(table_name=table_name), params)
self.conn.commit()
except sqlite3.Error as e:
print(e)
finally:
cursor.close()
self.conn.commit()
def count(self):
pass
def create_tables(db):
create_sql = '''
create table {table_name}
(
id integer not null primary key autoincrement,
magnet varchar(30) not null unique,
torrent_name varchar(500),
content text,
create_date datetime(6)
);
'''
drop_sql = '''
drop table {table_name};
'''
table_names = ['magnet_' + i for i in string.digits + string.ascii_lowercase]
exec_tables = []
conn = sqlite3.connect(db)
cursor = conn.cursor()
try:
for name in table_names:
try:
cursor.execute(create_sql.format(table_name=name))
exec_tables.append(name)
conn.commit()
print('table {} created successful'.format(name))
except sqlite3.OperationalError as e:
# logging.exception(e)
if 'already exists' in str(e):
print(e)
continue
else:
raise Exception
except sqlite3.OperationalError as e:
logging.exception(e)
except KeyboardInterrupt:
for name in exec_tables:
cursor.execute(drop_sql.format(table_name=name))
conn.commit()
finally:
cursor.close()
conn.commit()
conn.close()
if __name__ == '__main__':
# rc = RedisClient()
# magnets = rc.get(20)
# print(magnets)
# print(len(magnets))
create_tables('magnet.db')