forked from ShilongLee/Crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
190 lines (171 loc) · 7.33 KB
/
driver.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from contextlib import asynccontextmanager, closing
from lib.logger import logger
import time
import sqlite3
import aiosqlite
class SqliteStore:
def __init__(self, db_path):
self.db_path = db_path
@asynccontextmanager
async def _get_connection(self):
async with aiosqlite.connect(self.db_path) as conn:
conn.row_factory = aiosqlite.Row
yield conn
def _get_sync_connection(self):
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
class CommonAccount(SqliteStore):
def __init__(self, store_path):
super().__init__(store_path)
self.primary_key = 'id'
self.table_name = 'account'
self._create_table()
def _create_table(self):
with closing(self._get_sync_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}')
async def save(self, id: str, cookie: str, expired: int) -> bool:
ct = ut = int(time.time())
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET cookie = ?, expired = ?, ut = ? WHERE id = ?'
await conn.execute(sql, (cookie, expired, ut, id))
if conn.total_changes == 0:
sql = f'INSERT INTO {self.table_name} (cookie, expired, ct, ut, id) VALUES (?, ?, ?, ?, ?)'
await conn.execute(sql, (cookie, expired, ct, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
await conn.rollback()
return False
async def load(self, offset: int = 0, limit: int = 0) -> list:
async with self._get_connection() as conn:
try:
if limit == 0:
sql = f'SELECT * FROM {self.table_name}'
cursor = await conn.execute(sql)
else:
sql = f'SELECT * FROM {self.table_name} LIMIT ? OFFSET ?'
cursor = await conn.execute(sql, (limit, offset))
results = await cursor.fetchall()
return [dict(row) for row in results]
except Exception as e:
logger.error(f'failed to load cookies, error: {e}')
await conn.rollback()
return []
async def expire(self, id: str) -> bool:
ut = int(time.time())
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET expired = ?, ut = ? WHERE id = ?'
await conn.execute(sql, (1, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save cookies, error: {e}')
await conn.rollback()
return False
class Proxies(SqliteStore):
def __init__(self, store_path):
super().__init__(store_path)
self.primary_key = 'id'
self.table_name = 'proxies'
self._create_table()
def _create_table(self):
with closing(self._get_sync_connection()) as conn, closing(conn.cursor()) as cursor:
try:
sql = f'''
CREATE TABLE IF NOT EXISTS {self.table_name} (
{self.primary_key} INTEGER PRIMARY KEY AUTOINCREMENT,
url VARCHAR(512) NOT NULL,
enable 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}')
async def save(self, url: str = '', enable: int = 1, id: int = 0) -> bool:
ct = ut = int(time.time())
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET url = ?, enable = ?, ut = ? WHERE id = ?'
await conn.execute(sql, (url, enable, ut, id))
if conn.total_changes == 0:
sql = f'INSERT INTO {self.table_name} (url, enable, ct, ut) VALUES (?, ?, ?, ?)'
await conn.execute(sql, (url, enable, ct, ut))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save proxy, error: {e}')
await conn.rollback()
return False
async def remove(self, id: int) -> bool:
async with self._get_connection() as conn:
try:
sql = f'DELETE FROM {self.table_name} WHERE id = ?'
await conn.execute(sql, (id,))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to remove proxy, error: {e}')
await conn.rollback()
return False
async def load(self, enable: int = -1, offset: int = 0, limit: int = 0) -> list:
async with self._get_connection() as conn:
try:
base_sql = f'SELECT * FROM {self.table_name}'
params = []
if enable != -1:
base_sql += ' WHERE enable = ?'
params.append(enable)
if limit > 0:
base_sql += ' LIMIT ? OFFSET ?'
params.extend([limit, offset])
cursor = await conn.execute(base_sql, params)
results = await cursor.fetchall()
return [dict(row) for row in results]
except Exception as e:
logger.error(f'failed to load proxies, error: {e}')
await conn.rollback()
return []
async def enable(self, id: int) -> bool:
ut = int(time.time())
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET enable = ?, ut = ? WHERE id = ?'
await conn.execute(sql, (1, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save proxies, error: {e}')
await conn.rollback()
return False
async def disable(self, id: int) -> bool:
ut = int(time.time())
async with self._get_connection() as conn:
try:
sql = f'UPDATE {self.table_name} SET enable = ?, ut = ? WHERE id = ?'
await conn.execute(sql, (0, ut, id))
await conn.commit()
return True
except Exception as e:
logger.error(f'failed to save proxies, error: {e}')
await conn.rollback()
return False