Skip to content

Commit

Permalink
FIX:修复数据库模型错误
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilongLee committed Jun 10, 2024
1 parent a6c44b8 commit 43383a0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 77 deletions.
4 changes: 2 additions & 2 deletions data/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def _get_connection(self):
return conn

class CommonAccount(SqliteStore):
def __init__(self, store_path, pool_size=5):
super().__init__(store_path, pool_size)
def __init__(self, store_path):
super().__init__(store_path)
self.primary_key = 'id'
self.table_name = 'account'
self._create_table()
Expand Down
2 changes: 1 addition & 1 deletion service/xhs/logic/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import execjs


def request_search(keyword: str, cookie: str, offset: int = 0, limit: int = 10) -> tuple[dict, bool]:
def request_search(keyword: str, cookie: str, offset: int = 0, limit: int = 20) -> tuple[dict, bool]:
"""
请求小红书获取搜索信息
"""
Expand Down
76 changes: 2 additions & 74 deletions service/xhs/models.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,3 @@
from data.driver import SqliteStore
from contextlib import closing
from lib.logger import logger
import time
from data.driver import CommonAccount

class XhsAccount(SqliteStore):
def __init__(self, store_path='xhs.db'):
super().__init__(store_path)
self.primary_key = 'id'
self.table_name = 'account'
self._create_table()

def _create_table(self):
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
try:
sql = f'''
CREATE TABLE IF NOT EXISTS {self.table_name} (
{self.primary_key} VARCHAR(2048) PRIMARY KEY NOT NULL,
cookie VARCHAR(2048) NOT NULL,
expired INTEGER NOT NULL,
ct INTEGER NOT NULL,
ut INTEGER NOT NULL
)
'''
cursor.execute(sql)
conn.commit()
except Exception as e:
logger.error(f'failed to create table, error: {e}')

def save(self, id: str, cookie: str, expired: int) -> bool:
ct = ut = int(time.time())
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
try:
sql = f'UPDATE {self.table_name} SET cookie = ?, expired = ?, ut = ? WHERE id = ?'
cursor.execute(sql, (cookie, expired, ut, id))
if cursor.rowcount == 0:
sql = f'INSERT INTO {self.table_name} (cookie, expired, ct, ut, id) VALUES (?, ?, ?, ?, ?)'
cursor.execute(sql, (cookie, expired, ct, ut, id))
conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
conn.rollback()
return False

def load(self, offset: int = 0, limit: int = 0) -> list:
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
try:
if limit == 0:
sql = f'SELECT * FROM {self.table_name}'
cursor.execute(sql)
else:
sql = f'SELECT * FROM {self.table_name} LIMIT ? OFFSET ?'
cursor.execute(sql, (limit, offset))
results = cursor.fetchall()
return [dict(row) for row in results]
except Exception as e:
logger.error(f'failed to load cookies, error: {e}')
conn.rollback()
return []

def expire(self, id: str) -> bool:
ut = int(time.time())
with closing(self._get_connection()) as conn, closing(conn.cursor()) as cursor:
try:
sql = f'UPDATE {self.table_name} SET expired = ?, ut = ? WHERE id = ?'
cursor.execute(sql, (1, ut, id))
conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
conn.rollback()
return False

accounts = XhsAccount("data/xhs/xhs.db")
accounts = CommonAccount("data/xhs/xhs.db")

0 comments on commit 43383a0

Please sign in to comment.