This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
models.py
70 lines (57 loc) · 1.9 KB
/
models.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
import datetime
import hashlib
import json
import pymysql
from peewee import *
from conf import config
from peewee import __exception_wrapper__
db = None
class RetryOperationalError(object):
def execute_sql(self, sql, params=None, commit=True):
try:
cursor = super(RetryOperationalError, self).execute_sql(
sql, params, commit)
except OperationalError:
if not self.is_closed():
self.close()
with __exception_wrapper__:
cursor = self.cursor()
cursor.execute(sql, params or ())
if commit and not self.in_transaction():
self.commit()
return cursor
class RetryMySQLDatabase(RetryOperationalError, MySQLDatabase):
pass
if config.mysql_or_sqlite:
Links = {
'host': config.mysql_host,
'port': config.mysql_port,
'user': config.mysql_usr,
'password': config.mysql_pass,
}
try:
con = pymysql.connect(**Links)
with con.cursor() as cursor:
cursor.execute(
f'create database {config.db} character set UTF8mb4 collate utf8mb4_bin')
con.close()
except pymysql.err.ProgrammingError as e:
if '1007' in str(e):
pass
except Exception as e:
raise e
Links['database'] = config.db
db = RetryMySQLDatabase(**Links, charset='utf8mb4')
else:
db = SqliteDatabase(config.db)
class UAS(Model):
uid = AutoField(primary_key=True, null=True) # 自增ID
useragent = TextField(unique=True) # useragent
software = CharField(null=True) # 软件类型
engine = CharField(null=True) # 引擎
types = CharField(null=True) # 硬件类型
popularity = CharField(null=True) # 通用性
class Meta:
database = db # 指定数据库
db.connect() # 连接数据库
db.create_tables([UAS]) # 初始化创建不存在的库