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