refactor bookmarks code - separate from main rafael loop
This commit is contained in:
parent
9e1d731cb0
commit
03d2f04095
|
@ -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()
|
||||
|
||||
|
|
|
@ -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}")
|
Loading…
Reference in New Issue