Skip to content

Commit 43383a0

Browse files
committed
FIX:修复数据库模型错误
1 parent a6c44b8 commit 43383a0

File tree

3 files changed

+5
-77
lines changed

3 files changed

+5
-77
lines changed

data/driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def _get_connection(self):
1313
return conn
1414

1515
class CommonAccount(SqliteStore):
16-
def __init__(self, store_path, pool_size=5):
17-
super().__init__(store_path, pool_size)
16+
def __init__(self, store_path):
17+
super().__init__(store_path)
1818
self.primary_key = 'id'
1919
self.table_name = 'account'
2020
self._create_table()

service/xhs/logic/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import execjs
33

44

5-
def request_search(keyword: str, cookie: str, offset: int = 0, limit: int = 10) -> tuple[dict, bool]:
5+
def request_search(keyword: str, cookie: str, offset: int = 0, limit: int = 20) -> tuple[dict, bool]:
66
"""
77
请求小红书获取搜索信息
88
"""

service/xhs/models.py

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,3 @@
1-
from data.driver import SqliteStore
2-
from contextlib import closing
3-
from lib.logger import logger
4-
import time
1+
from data.driver import CommonAccount
52

6-
class XhsAccount(SqliteStore):
7-
def __init__(self, store_path='xhs.db'):
8-
super().__init__(store_path)
9-
self.primary_key = 'id'
10-
self.table_name = 'account'
11-
self._create_table()
12-
13-
def _create_table(self):
14-
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
15-
try:
16-
sql = f'''
17-
CREATE TABLE IF NOT EXISTS {self.table_name} (
18-
{self.primary_key} VARCHAR(2048) PRIMARY KEY NOT NULL,
19-
cookie VARCHAR(2048) NOT NULL,
20-
expired INTEGER NOT NULL,
21-
ct INTEGER NOT NULL,
22-
ut INTEGER NOT NULL
23-
)
24-
'''
25-
cursor.execute(sql)
26-
conn.commit()
27-
except Exception as e:
28-
logger.error(f'failed to create table, error: {e}')
29-
30-
def save(self, id: str, cookie: str, expired: int) -> bool:
31-
ct = ut = int(time.time())
32-
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
33-
try:
34-
sql = f'UPDATE {self.table_name} SET cookie = ?, expired = ?, ut = ? WHERE id = ?'
35-
cursor.execute(sql, (cookie, expired, ut, id))
36-
if cursor.rowcount == 0:
37-
sql = f'INSERT INTO {self.table_name} (cookie, expired, ct, ut, id) VALUES (?, ?, ?, ?, ?)'
38-
cursor.execute(sql, (cookie, expired, ct, ut, id))
39-
conn.commit()
40-
return True
41-
except Exception as e:
42-
logger.error(f'failed to save cookies, error: {e}')
43-
conn.rollback()
44-
return False
45-
46-
def load(self, offset: int = 0, limit: int = 0) -> list:
47-
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
48-
try:
49-
if limit == 0:
50-
sql = f'SELECT * FROM {self.table_name}'
51-
cursor.execute(sql)
52-
else:
53-
sql = f'SELECT * FROM {self.table_name} LIMIT ? OFFSET ?'
54-
cursor.execute(sql, (limit, offset))
55-
results = cursor.fetchall()
56-
return [dict(row) for row in results]
57-
except Exception as e:
58-
logger.error(f'failed to load cookies, error: {e}')
59-
conn.rollback()
60-
return []
61-
62-
def expire(self, id: str) -> bool:
63-
ut = int(time.time())
64-
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
65-
try:
66-
sql = f'UPDATE {self.table_name} SET expired = ?, ut = ? WHERE id = ?'
67-
cursor.execute(sql, (1, ut, id))
68-
conn.commit()
69-
return True
70-
except Exception as e:
71-
logger.error(f'failed to save cookies, error: {e}')
72-
conn.rollback()
73-
return False
74-
75-
accounts = XhsAccount("data/xhs/xhs.db")
3+
accounts = CommonAccount("data/xhs/xhs.db")

0 commit comments

Comments
 (0)