diff --git a/data/driver.py b/data/driver.py index 8975d12..bda315f 100644 --- a/data/driver.py +++ b/data/driver.py @@ -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() diff --git a/service/xhs/logic/search.py b/service/xhs/logic/search.py index 581f8b1..45078b5 100644 --- a/service/xhs/logic/search.py +++ b/service/xhs/logic/search.py @@ -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]: """ 请求小红书获取搜索信息 """ diff --git a/service/xhs/models.py b/service/xhs/models.py index b394717..7adb911 100644 --- a/service/xhs/models.py +++ b/service/xhs/models.py @@ -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") \ No newline at end of file +accounts = CommonAccount("data/xhs/xhs.db") \ No newline at end of file