forked from OxOOo/ProxyPoolWithUI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.py
33 lines (27 loc) · 792 Bytes
/
init.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
# encoding: utf-8
from config import DATABASE_PATH
from .Proxy import Proxy
from .Fetcher import Fetcher
from fetchers import fetchers
import sqlite3
def init():
"""
初始化数据库
"""
conn = sqlite3.connect(DATABASE_PATH)
create_tables = Proxy.ddls + Fetcher.ddls
for sql in create_tables:
conn.execute(sql)
conn.commit()
# 注册所有的爬取器
c = conn.cursor()
c.execute('BEGIN EXCLUSIVE TRANSACTION;')
for item in fetchers:
c.execute('SELECT * FROM fetchers WHERE name=?', (item.name,))
if c.fetchone() is None:
f = Fetcher()
f.name = item.name
c.execute('INSERT INTO fetchers VALUES(?,?,?,?,?)', f.params())
c.close()
conn.commit()
conn.close()