forked from sch/Slixfeed
Update sqlitehandler.py
This commit is contained in:
parent
1f3aa9459e
commit
85d98609c5
1 changed files with 102 additions and 83 deletions
|
@ -134,7 +134,8 @@ async def add_feed(db_file, title, url, res):
|
|||
cur.execute(sql, feed)
|
||||
|
||||
source = title if title else '<' + url + '>'
|
||||
msg = '> {}\nNews source "{}" has been added to subscription list'.format(url, source)
|
||||
msg = """> {}\nNews source \"{}\" has been added to subscription list.
|
||||
""".format(url, source)
|
||||
return msg
|
||||
|
||||
|
||||
|
@ -151,18 +152,20 @@ async def remove_feed(db_file, ix):
|
|||
cur = conn.cursor()
|
||||
try:
|
||||
sql = "SELECT address FROM feeds WHERE id = ?"
|
||||
url = cur.execute(sql, (ix,))
|
||||
for i in url:
|
||||
url = i[0]
|
||||
# cur
|
||||
# for i in url:
|
||||
# url = i[0]
|
||||
url = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
sql = "SELECT name FROM feeds WHERE id = ?"
|
||||
name = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
# NOTE Should we move DBLOCK to this line? 2022-12-23
|
||||
sql = "DELETE FROM entries WHERE source = ?"
|
||||
cur.execute(sql, (url,))
|
||||
sql = "DELETE FROM feeds WHERE id = ?"
|
||||
cur.execute(sql, (ix,))
|
||||
msg = """News source <{}> has been removed from subscription list
|
||||
""".format(url)
|
||||
msg = "> {}\nNews source \"{}\" has been removed from subscription list.".format(url, name)
|
||||
except:
|
||||
msg = """No news source with ID {}""".format(ix)
|
||||
msg = "No news source with ID {}.".format(ix)
|
||||
return msg
|
||||
|
||||
|
||||
|
@ -177,8 +180,7 @@ async def check_feed_exist(db_file, url):
|
|||
"""
|
||||
cur = get_cursor(db_file)
|
||||
sql = "SELECT id, name FROM feeds WHERE address = ?"
|
||||
cur.execute(sql, (url,))
|
||||
result = cur.fetchone()
|
||||
result = cur.execute(sql, (url,)).fetchone()
|
||||
return result
|
||||
|
||||
|
||||
|
@ -193,8 +195,22 @@ async def get_number_of_items(db_file, str):
|
|||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT count(id) FROM {}".format(str)
|
||||
count = cur.execute(sql)
|
||||
count = cur.fetchone()[0]
|
||||
count = cur.execute(sql).fetchone()[0]
|
||||
return count
|
||||
|
||||
|
||||
async def get_number_of_feeds_active(db_file):
|
||||
"""
|
||||
Return number of active feeds.
|
||||
|
||||
:param db_file: Database filename.
|
||||
:param cur: Cursor object.
|
||||
:return: Number of rows.
|
||||
"""
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT count(id) FROM feeds WHERE enabled = 1"
|
||||
count = cur.execute(sql).fetchone()[0]
|
||||
return count
|
||||
|
||||
|
||||
|
@ -209,8 +225,7 @@ async def get_number_of_entries_unread(db_file):
|
|||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT count(id) FROM entries WHERE read = 0"
|
||||
count = cur.execute(sql)
|
||||
count = cur.fetchone()[0]
|
||||
count = cur.execute(sql).fetchone()[0]
|
||||
return count
|
||||
|
||||
|
||||
|
@ -223,26 +238,18 @@ async def get_entry_unread(db_file):
|
|||
"""
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
entry = []
|
||||
sql = "SELECT id FROM entries WHERE read = 0"
|
||||
ix = cur.execute(sql).fetchone()
|
||||
if ix is None:
|
||||
return False
|
||||
ix = ix[0]
|
||||
sql = "SELECT title FROM entries WHERE id = :id"
|
||||
cur.execute(sql, (ix,))
|
||||
title = cur.fetchone()[0]
|
||||
entry.append(title)
|
||||
title = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
sql = "SELECT summary FROM entries WHERE id = :id"
|
||||
cur.execute(sql, (ix,))
|
||||
summary = cur.fetchone()[0]
|
||||
entry.append(summary)
|
||||
summary = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
sql = "SELECT link FROM entries WHERE id = :id"
|
||||
cur.execute(sql, (ix,))
|
||||
link = cur.fetchone()[0]
|
||||
entry.append(link)
|
||||
entry = "{}\n\n{}\n\n{}".format(entry[0], entry[1], entry[2])
|
||||
# print(entry)
|
||||
link = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
entry = "{}\n\n{}\n\n{}".format(title, summary, link)
|
||||
async with DBLOCK:
|
||||
await mark_as_read(cur, ix)
|
||||
# async with DBLOCK:
|
||||
|
@ -269,12 +276,23 @@ async def statistics(db_file):
|
|||
:return: News item as message.
|
||||
"""
|
||||
feeds = await get_number_of_items(db_file, 'feeds')
|
||||
active_feeds = await get_number_of_feeds_active(db_file)
|
||||
entries = await get_number_of_items(db_file, 'entries')
|
||||
unread_entries = await get_number_of_entries_unread(db_file)
|
||||
msg = "You have {} unread news items out of {} from {} news sources.".format(unread_entries, entries, feeds)
|
||||
# msg = """You have {} unread news items out of {} from {} news sources.
|
||||
# """.format(unread_entries, entries, feeds)
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT value FROM settings WHERE key = \"enabled\""
|
||||
status = cur.execute(sql).fetchone()[0]
|
||||
sql = "SELECT value FROM settings WHERE key = \"interval\""
|
||||
interval = cur.execute(sql).fetchone()[0]
|
||||
msg = """News items: {} ({})\nNews sources: {} ({})\nUpdate interval: {}\nOperation status: {}
|
||||
""".format(unread_entries, entries, active_feeds, feeds, interval, status)
|
||||
return msg
|
||||
|
||||
|
||||
#TODO statistics
|
||||
async def update_statistics(cur):
|
||||
"""
|
||||
Update table statistics.
|
||||
|
@ -293,8 +311,7 @@ async def update_statistics(cur):
|
|||
cur.execute(sql, {"title": i, "num": stat_dict[i]})
|
||||
else:
|
||||
sql = "SELECT count(id) FROM statistics"
|
||||
count = cur.execute(sql)
|
||||
count = cur.fetchone()[0]
|
||||
count = cur.execute(sql).fetchone()[0]
|
||||
ix = count + 1
|
||||
sql = "INSERT INTO statistics VALUES(?,?,?)"
|
||||
cur.execute(sql, (ix, i, stat_dict[i]))
|
||||
|
@ -312,26 +329,27 @@ async def toggle_status(db_file, ix):
|
|||
async with DBLOCK:
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
#cur = get_cursor(db_file)
|
||||
sql = "SELECT name FROM feeds WHERE id = :id"
|
||||
cur.execute(sql, (ix,))
|
||||
title = cur.fetchone()[0]
|
||||
sql = "SELECT enabled FROM feeds WHERE id = ?"
|
||||
# NOTE [0][1][2]
|
||||
cur.execute(sql, (ix,))
|
||||
status = cur.fetchone()[0]
|
||||
# FIXME always set to 1
|
||||
# NOTE Maybe because is not integer
|
||||
# TODO Reset feed table before further testing
|
||||
if status == 1:
|
||||
status = 0
|
||||
state = "disabled"
|
||||
else:
|
||||
status = 1
|
||||
state = "enabled"
|
||||
sql = "UPDATE feeds SET enabled = :status WHERE id = :id"
|
||||
cur.execute(sql, {"status": status, "id": ix})
|
||||
msg = "Updates for '{}' are now {}".format(title, state)
|
||||
try:
|
||||
#cur = get_cursor(db_file)
|
||||
sql = "SELECT name FROM feeds WHERE id = :id"
|
||||
title = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
sql = "SELECT enabled FROM feeds WHERE id = ?"
|
||||
# NOTE [0][1][2]
|
||||
status = cur.execute(sql, (ix,)).fetchone()[0]
|
||||
# FIXME always set to 1
|
||||
# NOTE Maybe because is not integer
|
||||
# TODO Reset feed table before further testing
|
||||
if status == 1:
|
||||
status = 0
|
||||
state = "disabled"
|
||||
else:
|
||||
status = 1
|
||||
state = "enabled"
|
||||
sql = "UPDATE feeds SET enabled = :status WHERE id = :id"
|
||||
cur.execute(sql, {"status": status, "id": ix})
|
||||
msg = "Updates for '{}' are now {}.".format(title, state)
|
||||
except:
|
||||
msg = "No news source with ID {}.".format(ix)
|
||||
return msg
|
||||
|
||||
|
||||
|
@ -414,8 +432,7 @@ async def remove_entry(db_file, source, length):
|
|||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT count(id) FROM entries WHERE source = ?"
|
||||
count = cur.execute(sql, (source,))
|
||||
count = cur.fetchone()[0]
|
||||
count = cur.execute(sql, (source,)).fetchone()[0]
|
||||
limit = count - length
|
||||
if limit:
|
||||
limit = limit;
|
||||
|
@ -441,8 +458,7 @@ async def remove_nonexistent_entries(db_file, feed, source):
|
|||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT id, title, link FROM entries WHERE source = ?"
|
||||
cur.execute(sql, (source,))
|
||||
entries_db = cur.fetchall()
|
||||
entries_db = cur.execute(sql, (source,)).fetchall()
|
||||
for entry_db in entries_db:
|
||||
exist = False
|
||||
for entry_feed in feed.entries:
|
||||
|
@ -477,8 +493,7 @@ async def get_subscriptions(db_file):
|
|||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT address FROM feeds WHERE enabled = 1"
|
||||
cur.execute(sql)
|
||||
result = cur.fetchall()
|
||||
result = cur.execute(sql).fetchall()
|
||||
return result
|
||||
|
||||
|
||||
|
@ -574,8 +589,7 @@ async def check_entry_exist(db_file, title, link):
|
|||
"""
|
||||
cur = get_cursor(db_file)
|
||||
sql = "SELECT id FROM entries WHERE title = :title and link = :link"
|
||||
cur.execute(sql, {"title": title, "link": link})
|
||||
result = cur.fetchone()
|
||||
result = cur.execute(sql, {"title": title, "link": link}).fetchone()
|
||||
return result
|
||||
|
||||
# TODO dictionary
|
||||
|
@ -602,15 +616,17 @@ async def set_settings_value(db_file, key_value):
|
|||
:param key_value: List of key ("enabled", "interval", "quantum") and value (Integer).
|
||||
:return: Message.
|
||||
"""
|
||||
if isinstance(key_value, list):
|
||||
key = key_value[0]
|
||||
val = key_value[1]
|
||||
elif key_value == "enable":
|
||||
key = "enabled"
|
||||
val = 1
|
||||
else:
|
||||
key = "enabled"
|
||||
val = 0
|
||||
# if isinstance(key_value, list):
|
||||
# key = key_value[0]
|
||||
# val = key_value[1]
|
||||
# elif key_value == "enable":
|
||||
# key = "enabled"
|
||||
# val = 1
|
||||
# else:
|
||||
# key = "enabled"
|
||||
# val = 0
|
||||
key = key_value[0]
|
||||
val = key_value[1]
|
||||
async with DBLOCK:
|
||||
with create_connection(db_file) as conn:
|
||||
cur = conn.cursor()
|
||||
|
@ -618,11 +634,15 @@ async def set_settings_value(db_file, key_value):
|
|||
sql = "UPDATE settings SET value = :value WHERE key = :key"
|
||||
cur.execute(sql, {"key": key, "value": val})
|
||||
if key == 'quantum':
|
||||
msg = "Every update will contain {} news items.".format(val)
|
||||
msg = "Each update will contain {} news items.".format(val)
|
||||
elif key == 'interval':
|
||||
msg = "Updates will be sent every {} minutes.".format(val)
|
||||
else:
|
||||
msg = "Updates are {}d.".format(key_value)
|
||||
if val:
|
||||
status = "disabled"
|
||||
else:
|
||||
status = "enabled"
|
||||
msg = "Updates are {}.".format(status)
|
||||
return msg
|
||||
|
||||
|
||||
|
@ -667,8 +687,7 @@ async def get_settings_value(db_file, key):
|
|||
try:
|
||||
cur = conn.cursor()
|
||||
sql = "SELECT value FROM settings WHERE key = ?"
|
||||
cur.execute(sql, (key,))
|
||||
result = cur.fetchone()[0]
|
||||
result = cur.execute(sql, (key,)).fetchone()[0]
|
||||
except:
|
||||
result = await set_settings_value_default(cur, key)
|
||||
if not result:
|
||||
|
|
Loading…
Reference in a new issue