Delay init of db until an argparse runs, use a common cursor.
This commit is contained in:
parent
769240dbb8
commit
6647d3bdab
1 changed files with 29 additions and 9 deletions
38
hnarchive.py
38
hnarchive.py
|
@ -13,6 +13,7 @@ from voussoirkit import betterhelp
|
||||||
from voussoirkit import httperrors
|
from voussoirkit import httperrors
|
||||||
from voussoirkit import mutables
|
from voussoirkit import mutables
|
||||||
from voussoirkit import operatornotify
|
from voussoirkit import operatornotify
|
||||||
|
from voussoirkit import pathclass
|
||||||
from voussoirkit import ratelimiter
|
from voussoirkit import ratelimiter
|
||||||
from voussoirkit import sqlhelpers
|
from voussoirkit import sqlhelpers
|
||||||
from voussoirkit import threadpool
|
from voussoirkit import threadpool
|
||||||
|
@ -60,8 +61,22 @@ COMMIT;
|
||||||
COLUMNS = sqlhelpers.extract_table_column_map(DB_INIT)
|
COLUMNS = sqlhelpers.extract_table_column_map(DB_INIT)
|
||||||
ITEMS_COLUMNS = COLUMNS['items']
|
ITEMS_COLUMNS = COLUMNS['items']
|
||||||
|
|
||||||
sql = sqlite3.connect('hnarchive.db')
|
def init_db():
|
||||||
sqlhelpers.executescript(sql, DB_INIT)
|
global sql
|
||||||
|
global cur
|
||||||
|
|
||||||
|
log.debug('Initializing database.')
|
||||||
|
db_path = pathclass.Path('hnarchive.db')
|
||||||
|
if db_path.is_link and not db_path.is_file:
|
||||||
|
raise RuntimeError(f'{db_path.absolute_path} is a broken link.')
|
||||||
|
|
||||||
|
db_exists = db_path.is_file
|
||||||
|
sql = sqlite3.connect(db_path.absolute_path)
|
||||||
|
cur = sql.cursor()
|
||||||
|
|
||||||
|
if not db_exists:
|
||||||
|
log.debug('Running first-time database setup.')
|
||||||
|
sqlhelpers.executescript(conn=sql, script=DB_INIT)
|
||||||
|
|
||||||
# HELPERS ##########################################################################################
|
# HELPERS ##########################################################################################
|
||||||
|
|
||||||
|
@ -211,7 +226,7 @@ def insert_item(data):
|
||||||
log.info('Inserting item %s.', id)
|
log.info('Inserting item %s.', id)
|
||||||
(qmarks, bindings) = sqlhelpers.insert_filler(ITEMS_COLUMNS, row, require_all=True)
|
(qmarks, bindings) = sqlhelpers.insert_filler(ITEMS_COLUMNS, row, require_all=True)
|
||||||
query = f'INSERT INTO items VALUES({qmarks})'
|
query = f'INSERT INTO items VALUES({qmarks})'
|
||||||
sql.execute(query, bindings)
|
cur.execute(query, bindings)
|
||||||
log.loud('Inserted item %s.', id)
|
log.loud('Inserted item %s.', id)
|
||||||
else:
|
else:
|
||||||
row = {
|
row = {
|
||||||
|
@ -233,7 +248,7 @@ def insert_item(data):
|
||||||
log.info('Updating item %s.', id)
|
log.info('Updating item %s.', id)
|
||||||
(qmarks, bindings) = sqlhelpers.update_filler(row, where_key='id')
|
(qmarks, bindings) = sqlhelpers.update_filler(row, where_key='id')
|
||||||
query = f'UPDATE items {qmarks}'
|
query = f'UPDATE items {qmarks}'
|
||||||
sql.execute(query, bindings)
|
cur.execute(query, bindings)
|
||||||
log.loud('Updated item %s.', id)
|
log.loud('Updated item %s.', id)
|
||||||
|
|
||||||
return {'row': row, 'is_new': existing is None}
|
return {'row': row, 'is_new': existing is None}
|
||||||
|
@ -251,7 +266,7 @@ def select_child_items(id):
|
||||||
'''
|
'''
|
||||||
Return items whose parent is this id.
|
Return items whose parent is this id.
|
||||||
'''
|
'''
|
||||||
cur = sql.execute('SELECT * FROM items WHERE parent == ?', [id])
|
cur.execute('SELECT * FROM items WHERE parent == ?', [id])
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
|
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
|
||||||
|
@ -261,14 +276,14 @@ def select_poll_options(id):
|
||||||
'''
|
'''
|
||||||
Return items that are pollopts under this given poll id.
|
Return items that are pollopts under this given poll id.
|
||||||
'''
|
'''
|
||||||
cur = sql.execute('SELECT * FROM items WHERE poll == ?', [id])
|
cur.execute('SELECT * FROM items WHERE poll == ?', [id])
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
|
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
|
||||||
return items
|
return items
|
||||||
|
|
||||||
def select_item(id):
|
def select_item(id):
|
||||||
cur = sql.execute('SELECT * FROM items WHERE id == ?', [id])
|
cur.execute('SELECT * FROM items WHERE id == ?', [id])
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row is None:
|
if row is None:
|
||||||
|
@ -278,7 +293,7 @@ def select_item(id):
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def select_latest_id():
|
def select_latest_id():
|
||||||
cur = sql.execute('SELECT id FROM items ORDER BY id DESC LIMIT 1')
|
cur.execute('SELECT id FROM items ORDER BY id DESC LIMIT 1')
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
if row is None:
|
if row is None:
|
||||||
return None
|
return None
|
||||||
|
@ -486,6 +501,7 @@ def html_render_page(tree):
|
||||||
|
|
||||||
@ctrlc_commit
|
@ctrlc_commit
|
||||||
def get_argparse(args):
|
def get_argparse(args):
|
||||||
|
init_db()
|
||||||
lower = args.lower
|
lower = args.lower
|
||||||
upper = args.upper or get_latest_id()
|
upper = args.upper or get_latest_id()
|
||||||
|
|
||||||
|
@ -496,6 +512,7 @@ def get_argparse(args):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def html_render_argparse(args):
|
def html_render_argparse(args):
|
||||||
|
init_db()
|
||||||
for id in args.ids:
|
for id in args.ids:
|
||||||
tree = build_item_tree(id=id)
|
tree = build_item_tree(id=id)
|
||||||
soup = html_render_page(tree)
|
soup = html_render_page(tree)
|
||||||
|
@ -509,12 +526,14 @@ def html_render_argparse(args):
|
||||||
|
|
||||||
@ctrlc_commit
|
@ctrlc_commit
|
||||||
def livestream_argparse(args):
|
def livestream_argparse(args):
|
||||||
|
init_db()
|
||||||
NOTIFY_EVERY_LINE.set(True)
|
NOTIFY_EVERY_LINE.set(True)
|
||||||
insert_items(livestream(), commit_period=args.commit_period)
|
insert_items(livestream(), commit_period=args.commit_period)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ctrlc_commit
|
@ctrlc_commit
|
||||||
def update_argparse(args):
|
def update_argparse(args):
|
||||||
|
init_db()
|
||||||
while True:
|
while True:
|
||||||
lower = select_latest_id() or 1
|
lower = select_latest_id() or 1
|
||||||
upper = get_latest_id()
|
upper = get_latest_id()
|
||||||
|
@ -529,6 +548,7 @@ def update_argparse(args):
|
||||||
|
|
||||||
@ctrlc_commit
|
@ctrlc_commit
|
||||||
def update_items_argparse(args):
|
def update_items_argparse(args):
|
||||||
|
init_db()
|
||||||
seconds = args.days * 86400
|
seconds = args.days * 86400
|
||||||
if args.only_mature:
|
if args.only_mature:
|
||||||
then = time.time() - (86400 * 14)
|
then = time.time() - (86400 * 14)
|
||||||
|
@ -537,7 +557,7 @@ def update_items_argparse(args):
|
||||||
else:
|
else:
|
||||||
query = 'SELECT id FROM items WHERE retrieved - time <= ?'
|
query = 'SELECT id FROM items WHERE retrieved - time <= ?'
|
||||||
bindings = [seconds]
|
bindings = [seconds]
|
||||||
cur = sql.execute(query, bindings)
|
cur.execute(query, bindings)
|
||||||
ids = cur.fetchall()
|
ids = cur.fetchall()
|
||||||
|
|
||||||
log.info('Updating %d items.', len(ids))
|
log.info('Updating %d items.', len(ids))
|
||||||
|
|
Loading…
Reference in a new issue