Fix a couple of errors and split functions

This commit is contained in:
Schimon Jehudah 2024-01-03 15:04:01 +00:00
parent 94af40cf40
commit 96f3369539
6 changed files with 114 additions and 148 deletions

View file

@ -69,16 +69,16 @@ def get_value(filename, section, keys):
key = keys key = keys
try: try:
result = section_res[key] result = section_res[key]
logging.debug("Found value {} for key {}".format( logging.debug(
value, key)) "Found value {} for key {}".format(result, key))
except: except:
result = '' result = ''
# logging.error("Missing key:", key) # logging.error("Missing key:", key)
if result == None: if result == None:
logging.error( logging.error(
"Check configuration file {} for " "Check configuration file {}.ini for "
"missing key {} under section {}.".format( "missing key(s) \"{}\" under section [{}].".format(
filename, section, keys)) filename, keys, section))
else: else:
return result return result

View file

@ -376,7 +376,7 @@ async def add_feed_no_check(db_file, data):
url = data[0] url = data[0]
title = data[1] title = data[1]
url = trim_url(url) url = trim_url(url)
exist = await sqlite.check_feed_exist(db_file, url) exist = await sqlite.is_feed_exist(db_file, url)
if not exist: if not exist:
msg = await sqlite.insert_feed(db_file, url, title) msg = await sqlite.insert_feed(db_file, url, title)
await download_updates(db_file, [url]) await download_updates(db_file, [url])
@ -409,7 +409,7 @@ async def add_feed(db_file, url):
""" """
msg = None msg = None
url = trim_url(url) url = trim_url(url)
exist = await sqlite.check_feed_exist(db_file, url) exist = await sqlite.is_feed_exist(db_file, url)
if not exist: if not exist:
res = await download_feed(url) res = await download_feed(url)
if res[0]: if res[0]:
@ -417,9 +417,14 @@ async def add_feed(db_file, url):
title = utility.get_title(url, feed) title = utility.get_title(url, feed)
if utility.is_feed(url, feed): if utility.is_feed(url, feed):
status = res[1] status = res[1]
msg = await sqlite.insert_feed( await sqlite.insert_feed(
db_file, url, title, status) db_file, url, title, status)
await download_updates(db_file, [url]) await download_updates(db_file, [url])
title = title if title else url
msg = (
"> {}\nNews source \"{}\" has been added "
"to subscription list."
).format(url, title)
else: else:
msg = await probe_page( msg = await probe_page(
add_feed, url, res[0], db_file=db_file) add_feed, url, res[0], db_file=db_file)
@ -497,7 +502,7 @@ async def download_feed(url):
user_agent = "Slixfeed/0.1" user_agent = "Slixfeed/0.1"
if not len(user_agent): if not len(user_agent):
user_agent = "Slixfeed/0.1" user_agent = "Slixfeed/0.1"
proxy = config.get_value("settings", "Network", "http_proxy") proxy = (config.get_value("settings", "Network", "http_proxy")) or ''
timeout = ClientTimeout(total=10) timeout = ClientTimeout(total=10)
headers = {'User-Agent': user_agent} headers = {'User-Agent': user_agent}
async with ClientSession(headers=headers) as session: async with ClientSession(headers=headers) as session:

View file

@ -177,24 +177,18 @@ async def insert_feed(db_file, url, title=None, status=None):
Feed Title. The default is None. Feed Title. The default is None.
status : str, optional status : str, optional
HTTP status code. The default is None. HTTP status code. The default is None.
Returns
-------
msg : str
Message.
""" """
#TODO consider async with DBLOCK #TODO consider async with DBLOCK
#conn = create_connection(db_file) #conn = create_connection(db_file)
# with create_connection(db_file) as conn: # with create_connection(db_file) as conn:
# #exist = await check_feed_exist(conn, url) # #exist = await is_feed_exist(conn, url)
# exist = await check_feed_exist(db_file, url) # exist = await is_feed_exist(db_file, url)
# if not exist: # if not exist:
# status = await main.download_feed(url) # status = await main.download_feed(url)
# else: # else:
# return "News source is already listed in the subscription list" # return "News source is already listed in the subscription list"
async with DBLOCK: async with DBLOCK:
with create_connection(db_file) as conn: with create_connection(db_file) as conn:
cur = conn.cursor() cur = conn.cursor()
@ -208,13 +202,6 @@ async def insert_feed(db_file, url, title=None, status=None):
) )
cur.execute(sql, feed) cur.execute(sql, feed)
source = title if title else '<' + url + '>'
msg = (
"> {}\nNews source \"{}\" has been added "
"to subscription list."
).format(url, source)
return msg
async def remove_feed(db_file, ix): async def remove_feed(db_file, ix):
""" """
@ -226,16 +213,10 @@ async def remove_feed(db_file, ix):
Path to database file. Path to database file.
ix : str ix : str
Index of feed. Index of feed.
Returns
-------
msg : str
Message.
""" """
with create_connection(db_file) as conn: with create_connection(db_file) as conn:
async with DBLOCK: async with DBLOCK:
cur = conn.cursor() cur = conn.cursor()
try:
sql = ( sql = (
"SELECT address " "SELECT address "
"FROM feeds " "FROM feeds "
@ -245,12 +226,6 @@ async def remove_feed(db_file, ix):
# for i in url: # for i in url:
# url = i[0] # url = i[0]
url = cur.execute(sql, (ix,)).fetchone()[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 # NOTE Should we move DBLOCK to this line? 2022-12-23
sql = ( sql = (
"DELETE " "DELETE "
@ -269,18 +244,9 @@ async def remove_feed(db_file, ix):
"WHERE id = ?" "WHERE id = ?"
) )
cur.execute(sql, (ix,)) cur.execute(sql, (ix,))
msg = (
"> {}\nNews source \"{}\" has been removed "
"from subscription list."
).format(url, name)
except:
msg = (
"No news source with ID {}."
).format(ix)
return msg
async def check_feed_exist(db_file, url): async def is_feed_exist(db_file, url):
""" """
Check whether a feed exists. Check whether a feed exists.
Query for feeds by given url. Query for feeds by given url.
@ -304,7 +270,8 @@ async def check_feed_exist(db_file, url):
"WHERE address = ?" "WHERE address = ?"
) )
result = cur.execute(sql, (url,)).fetchone() result = cur.execute(sql, (url,)).fetchone()
return result if result:
return True
async def get_number_of_items(db_file, table): async def get_number_of_items(db_file, table):
@ -406,8 +373,8 @@ async def get_unread_entries(db_file, num):
Returns Returns
------- -------
entry : str results : ???
News item message. News items.
""" """
with create_connection(db_file) as conn: with create_connection(db_file) as conn:
cur = conn.cursor() cur = conn.cursor()
@ -641,9 +608,9 @@ async def update_statistics(cur):
cur.execute(sql, (ix, i, stat_dict[i])) cur.execute(sql, (ix, i, stat_dict[i]))
async def toggle_status(db_file, ix): async def set_enabled_status(db_file, ix, status):
""" """
Toggle status of feed. Set status of feed to enabled or not enabled (i.e. disabled).
Parameters Parameters
---------- ----------
@ -651,39 +618,12 @@ async def toggle_status(db_file, ix):
Path to database file. Path to database file.
ix : str ix : str
Index of entry. Index of entry.
status : int
Returns 0 or 1.
-------
msg : str
Message.
""" """
async with DBLOCK: async with DBLOCK:
with create_connection(db_file) as conn: with create_connection(db_file) as conn:
cur = conn.cursor() cur = conn.cursor()
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 = ( sql = (
"UPDATE feeds " "UPDATE feeds "
"SET enabled = :status " "SET enabled = :status "
@ -693,14 +633,6 @@ async def toggle_status(db_file, ix):
"status": status, "status": status,
"id": ix "id": ix
}) })
msg = (
"Updates from '{}' are now {}."
).format(title, state)
except:
msg = (
"No news source with ID {}."
).format(ix)
return msg
async def set_date(cur, url): async def set_date(cur, url):
@ -836,9 +768,10 @@ async def add_entry(cur, entry):
try: try:
cur.execute(sql, entry) cur.execute(sql, entry)
except: except:
# None None
print("Unknown error for sqlite.add_entry") # print("Unknown error for sqlite.add_entry")
print(entry) # print(entry)
#
# print(current_time(), "COROUTINE OBJECT NOW") # print(current_time(), "COROUTINE OBJECT NOW")
# for i in entry: # for i in entry:
# print(type(i)) # print(type(i))
@ -1085,7 +1018,7 @@ async def get_feeds(db_file):
Returns Returns
------- -------
msg : ??? results : ???
URLs of feeds. URLs of feeds.
""" """
cur = get_cursor(db_file) cur = get_cursor(db_file)

View file

@ -340,7 +340,7 @@ async def refresh_task(self, jid, callback, key, val=None):
except: except:
logging.debug( logging.debug(
"No task of type {} to cancel for " "No task of type {} to cancel for "
"JID {} (clean_tasks)").format(key, jid) "JID {} (clean_tasks)".format(key, jid))
# task_manager[jid][key] = loop.call_at( # task_manager[jid][key] = loop.call_at(
# loop.time() + 60 * float(val), # loop.time() + 60 * float(val),
# loop.create_task, # loop.create_task,

View file

@ -17,9 +17,7 @@ async def recover_connection(self, event, message):
# print(current_time(),"Maximum connection attempts exceeded.") # print(current_time(),"Maximum connection attempts exceeded.")
# logging.error("Maximum connection attempts exceeded.") # logging.error("Maximum connection attempts exceeded.")
print(current_time(), "Attempt number", self.connection_attempts) print(current_time(), "Attempt number", self.connection_attempts)
seconds = get_value("accounts", "XMPP Connect", "reconnect_timeout") seconds = (get_value("accounts", "XMPP Connect", "reconnect_timeout")) or 30
if not seconds:
seconds = 30
seconds = int(seconds) seconds = int(seconds)
print(current_time(), "Next attempt within", seconds, "seconds") print(current_time(), "Next attempt within", seconds, "seconds")
# NOTE asyncio.sleep doesn't interval as expected # NOTE asyncio.sleep doesn't interval as expected

View file

@ -327,12 +327,19 @@ async def message(self, message):
else: else:
response = "Missing keywords." response = "Missing keywords."
send_reply_message(self, message, response) send_reply_message(self, message, response)
case _ if message_lowercase.startswith("import "):
status_type = "dnd"
status_message = (
"📥️ Procesing request to import feeds ..."
)
send_status_message(
self, jid, status_type, status_message)
case _ if message_lowercase.startswith("export "): case _ if message_lowercase.startswith("export "):
key = message_text[7:] key = message_text[7:]
if key in ("opml", "html", "md", "xbel"): if key in ("opml", "html", "md", "xbel"):
status_type = "dnd" status_type = "dnd"
status_message = ( status_message = (
"📂️ Procesing request to export feeds into {} ..." "📤 Procesing request to export feeds into {} ..."
).format(key) ).format(key)
send_status_message( send_status_message(
self, jid, status_type, status_message) self, jid, status_type, status_message)
@ -366,12 +373,12 @@ async def message(self, message):
else: else:
response = "Unsupported filetype." response = "Unsupported filetype."
send_reply_message(self, message, response) send_reply_message(self, message, response)
case _ if (message_lowercase.startswith("gemini") or case _ if (message_lowercase.startswith("gemini:") or
message_lowercase.startswith("gopher")): message_lowercase.startswith("gopher:")):
response = "Gemini and Gopher are not supported yet." response = "Gemini and Gopher are not supported yet."
send_reply_message(self, message, response) send_reply_message(self, message, response)
case _ if (message_lowercase.startswith("http") or case _ if (message_lowercase.startswith("http") or
message_lowercase.startswith("feed")): message_lowercase.startswith("feed:")):
url = message_text url = message_text
await task.clean_tasks_xmpp(jid, ["status"]) await task.clean_tasks_xmpp(jid, ["status"])
status_type = "dnd" status_type = "dnd"
@ -624,7 +631,11 @@ async def message(self, message):
ix = message_text[7:] ix = message_text[7:]
if ix: if ix:
db_file = get_pathname_to_database(jid) db_file = get_pathname_to_database(jid)
response = await sqlite.remove_feed(db_file, ix) try:
await sqlite.remove_feed(db_file, ix)
response = (
"> {}\nNews source has been removed "
"from subscription list.").format(url)
# await refresh_task( # await refresh_task(
# self, # self,
# jid, # jid,
@ -634,6 +645,8 @@ async def message(self, message):
# ) # )
await task.clean_tasks_xmpp(jid, ["status"]) await task.clean_tasks_xmpp(jid, ["status"])
await task.start_tasks_xmpp(self, jid, ["status"]) await task.start_tasks_xmpp(self, jid, ["status"])
except:
response = "No news source with ID {}.".format(ix)
else: else:
response = "Missing feed ID." response = "Missing feed ID."
send_reply_message(self, message, response) send_reply_message(self, message, response)
@ -687,10 +700,27 @@ async def message(self, message):
result = await sqlite.statistics(db_file) result = await sqlite.statistics(db_file)
response = compose.list_statistics(result) response = compose.list_statistics(result)
send_reply_message(self, message, response) send_reply_message(self, message, response)
case _ if message_lowercase.startswith("status "): case _ if message_lowercase.startswith("disable "):
ix = message_text[8:]
db_file = get_pathname_to_database(jid)
try:
await sqlite.set_enabled_status(db_file, ix, 0)
response = (
"Updates are now disabled for news source {}."
).format(ix)
except:
response = "No news source with ID {}.".format(ix)
send_reply_message(self, message, response)
case _ if message_lowercase.startswith("enable "):
ix = message_text[7:] ix = message_text[7:]
db_file = get_pathname_to_database(jid) db_file = get_pathname_to_database(jid)
response = await sqlite.toggle_status(db_file, ix) try:
await sqlite.set_enabled_status(db_file, ix, 1)
response = (
"Updates are now disabled for news source {}."
).format(ix)
except:
response = "No news source with ID {}.".format(ix)
send_reply_message(self, message, response) send_reply_message(self, message, response)
case "stop": case "stop":
# FIXME # FIXME