From 03d2f0409562dd7751456da2d810bef3805739e3 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Sun, 3 Apr 2022 10:51:59 +0000 Subject: [PATCH] refactor bookmarks code - separate from main rafael loop --- src/rafael/__init__.py | 45 ++++------------------------ src/rafael/bookmarks.py | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 src/rafael/bookmarks.py diff --git a/src/rafael/__init__.py b/src/rafael/__init__.py index 2503787..9d3c817 100644 --- a/src/rafael/__init__.py +++ b/src/rafael/__init__.py @@ -1,69 +1,36 @@ -import time import dotenv import os -import click import dokuwiki import requests -from bs4 import BeautifulSoup from flask import Flask from telegram import Update from telegram.ext import Updater, MessageHandler, CommandHandler, Filters, CallbackContext -from datetime import datetime app = Flask("rafael") - RAFAEL_UA = "RAFAEL/0.1" + +from rafael.bookmarks import RafaelBookmarkPlugin + + class RafaelBot: def __init__(self): self.updater = Updater(os.getenv("TELEGRAM_API_KEY")) - self.dispatcher = self.updater.dispatcher - self.dispatcher.add_handler(MessageHandler(Filters.entity('url'), self.handle_url)) + bh = RafaelBookmarkPlugin() + bh.register(self.dispatcher) try: self.wiki = dokuwiki.DokuWiki(os.getenv('DOKU_URL'), os.getenv('DOKU_USER'), os.getenv('DOKU_PASS'), cookieAuth=True) except (dokuwiki.DokuWikiError, Exception) as err: print('unable to connect: %s' % err) - - def get_title(self, url): - - r = requests.get(url, headers={"User-Agent": RAFAEL_UA}) - bs = BeautifulSoup(r.content) - return bs.title.string - - def handle_url(self, update: Update, context: CallbackContext) -> None: - """Echo the user message.""" - update.message.reply_text("Storing URL...") - - update_body = "" - - for ent in update.message.entities: - if ent.type == "url": - url = update.message.text[ent.offset:ent.length] - try: - title = self.get_title(url) - link = f"[[{url}|{title}]]" - except: - link = "{url}" - - added_ds = f"(retrieved at: {datetime.now().strftime('%d/%m/%Y, %H:%M:%S')})" - - update_body += f" * {link} {added_ds}" - - try: - self.wiki.pages.append("bookmarks/inbox", f"\n\n{update_body}") - update.message.reply_text(f"Updated bookmarks page: {os.getenv('DOKU_URL')}bookmarks/inbox") - except (dokuwiki.DokuWikiError) as err: - update.message.reply_text(f"Failed to store url: {err}") - def run(self): self.updater.start_polling() diff --git a/src/rafael/bookmarks.py b/src/rafael/bookmarks.py new file mode 100644 index 0000000..9c1c8ce --- /dev/null +++ b/src/rafael/bookmarks.py @@ -0,0 +1,65 @@ +import time +import dotenv +import os +import click +import dokuwiki +import requests + +from bs4 import BeautifulSoup +from flask import Flask + +from telegram import Update +import telegram +from telegram.ext import Updater, MessageHandler, CommandHandler, Filters, CallbackContext, Dispatcher + +from datetime import datetime + +from rafael import RAFAEL_UA + + +class RafaelBookmarkPlugin: + + def __init__(self): + self.updater = Updater(os.getenv("TELEGRAM_API_KEY")) + + try: + self.wiki = dokuwiki.DokuWiki(os.getenv('DOKU_URL'), os.getenv('DOKU_USER'), os.getenv('DOKU_PASS'), cookieAuth=True) + except (dokuwiki.DokuWikiError, Exception) as err: + print('unable to connect: %s' % err) + + + def register(self, dispatcher: Dispatcher): + dispatcher.add_handler(MessageHandler(Filters.entity('url'), self.handle_url)) + + def get_title(self, url): + + r = requests.get(url, headers={"User-Agent": RAFAEL_UA}) + bs = BeautifulSoup(r.content, 'html5lib') + return bs.title.string + + + def handle_url(self, update: Update, context: CallbackContext) -> None: + """Echo the user message.""" + update.message.reply_text("Storing URL...") + + update_body = "" + + for ent in update.message.entities: + if ent.type == "url": + url = update.message.text[ent.offset:ent.length] + try: + title = self.get_title(url) + link = f"[[{url}|{title}]]" + except Exception as err: + update.message.reply_text(f"Failed to retrieve title for page {url}: {err}. Storing url only") + link = f"{url}" + + added_ds = f"(retrieved at: {datetime.now().strftime('%d/%m/%Y, %H:%M:%S')})" + + update_body += f" * {link} {added_ds}" + + try: + self.wiki.pages.append("bookmarks/inbox", f"\n\n{update_body}") + update.message.reply_text(f"Updated bookmarks page: {os.getenv('DOKU_URL')}bookmarks/inbox") + except (dokuwiki.DokuWikiError) as err: + update.message.reply_text(f"Failed to store url: {err}") \ No newline at end of file