diff --git a/slixfeed/datetime.py b/slixfeed/datetime.py index eb7cea2..47a2012 100644 --- a/slixfeed/datetime.py +++ b/slixfeed/datetime.py @@ -22,6 +22,20 @@ def now(): return date +def current_date(): + """ + Print MM DD, YYYY (Weekday Time) timestamp. + + Returns + ------- + date : str + MM DD, YYYY (Weekday Time) timestamp. + """ + now = datetime.now() + time = now.strftime("%B %d, %Y (%A %T)") + return time + + def current_time(): """ Print HH:MM:SS timestamp. diff --git a/slixfeed/export.py b/slixfeed/export.py new file mode 100644 index 0000000..33c7aba --- /dev/null +++ b/slixfeed/export.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from slixfeed.datetime import current_date + +def markdown(jid, filename, results): + with open(filename, 'w') as file: + file.write( + '# Subscriptions for {}\n'.format(jid)) + file.write( + '## Set of feeds exported with Slixfeed\n') + for result in results: + file.write( + '- [{}]({})\n'.format(result[0], result[1])) + file.write( + '\n\n* * *\n\nThis list was saved on {} from xmpp:{} using ' + '[Slixfeed](https://gitgud.io/sjehuda/slixfeed)\n'.format( + current_date(), jid)) diff --git a/slixfeed/opml.py b/slixfeed/opml.py index 527ceb1..64ff1f9 100644 --- a/slixfeed/opml.py +++ b/slixfeed/opml.py @@ -33,8 +33,6 @@ 'version': 'opml2' } -""" - import listparser import lxml @@ -48,17 +46,19 @@ async def import_from_file(db_file, opml_doc): # tags = feed['tags'] # await datahandler.add_feed_no_check(db_file, [url, title]) +""" + from slixfeed.datetime import current_time import xml.etree.ElementTree as ET # NOTE Use OPyML or LXML -async def export_to_file(jid, filename, results): +def export_to_file(jid, filename, results): root = ET.Element("opml") root.set("version", "1.0") head = ET.SubElement(root, "head") ET.SubElement(head, "title").text = "Subscriptions for {}".format(jid) ET.SubElement(head, "description").text = ( - "Set of feeds exported with Slixfeed.") + "Set of feeds exported with Slixfeed") ET.SubElement(head, "generator").text = "Slixfeed" ET.SubElement(head, "urlPublic").text = ( "https://gitgud.io/sjehuda/slixfeed") diff --git a/slixfeed/xmpp/process.py b/slixfeed/xmpp/process.py index fc08dbd..31e375a 100644 --- a/slixfeed/xmpp/process.py +++ b/slixfeed/xmpp/process.py @@ -26,6 +26,7 @@ from slixfeed.config import ( get_pathname_to_database, remove_from_list) from slixfeed.datetime import current_time, timestamp +import slixfeed.export as export import slixfeed.fetch as fetcher import slixfeed.opml as opml import slixfeed.sqlite as sqlite @@ -38,6 +39,7 @@ import slixfeed.xmpp.muc as groupchat import slixfeed.xmpp.status as status import slixfeed.xmpp.text as text import slixfeed.xmpp.upload as upload +from slixfeed.xmpp.utility import jid_type async def event(self, event): @@ -326,38 +328,43 @@ async def message(self, message): response = "Missing keywords." send_reply_message(self, message, response) case _ if message_lowercase.startswith("export "): - valid = 1 key = message_text[7:] - data_dir = get_default_dbdir() - if not os.path.isdir(data_dir): - os.mkdir(data_dir) - filename = os.path.join( - data_dir, "opml", "slixfeed_" + timestamp() + "." + key) - db_file = get_pathname_to_database(jid) - results = await sqlite.get_feeds(db_file) - match key: - case "opml": - status_type = "dnd" - status_message = ( - "📂️ Procesing request to export feeds into OPML ...") - send_status_message(self, jid, status_type, status_message) - await opml.export_to_file( - jid, filename, results) - url = await upload.start(self, jid, filename) - response = ( - "Feeds exported successfully to an OPML " - "Outline Syndication.\n{}").format(url) - await task.start_tasks_xmpp(self, jid, ["status"]) - case "html": - response = "Not yet implemented." - case "markdown": - response = "Not yet implemented" - case _: - response = "Unsupported filetype." - valid = 0 - if valid: + if key in ("opml", "html", "md", "xbel"): + status_type = "dnd" + status_message = ( + "📂️ Procesing request to export feeds into {} ..." + ).format(key) + send_status_message( + self, jid, status_type, status_message) + data_dir = get_default_dbdir() + if not os.path.isdir(data_dir): + os.mkdir(data_dir) + if not os.path.isdir(data_dir + '/' + key): + os.mkdir(data_dir + '/' + key) + filename = os.path.join( + data_dir, key, "slixfeed_" + timestamp() + "." + key) + db_file = get_pathname_to_database(jid) + results = await sqlite.get_feeds(db_file) + match key: + case "html": + response = "Not yet implemented." + case "md": + export.markdown( + jid, filename, results) + case "opml": + opml.export_to_file( + jid, filename, results) + case "xbel": + response = "Not yet implemented." + url = await upload.start(self, jid, filename) + response = ( + "Feeds exported successfully to {}.\n{}" + ).format(key, url) # send_oob_reply_message(message, url, response) - send_oob_message(self, jid, url) + await send_oob_message(self, jid, url) + await task.start_tasks_xmpp(self, jid, ["status"]) + else: + response = "Unsupported filetype." send_reply_message(self, message, response) case _ if (message_lowercase.startswith("gemini") or message_lowercase.startswith("gopher")): @@ -775,11 +782,12 @@ def send_oob_reply_message(message, url, response): reply.send() -def send_oob_message(self, jid, url): +async def send_oob_message(self, jid, url): + chat_type = await jid_type(self, jid) html = ( f'
' f'{url}') - message = self.make_message(mto=jid, mbody=url, mhtml=html) + message = self.make_message(mto=jid, mbody=url, mhtml=html, mtype=chat_type) message['oob']['url'] = url message.send()