Code-Refactor
This commit is contained in:
parent
4d9a498e50
commit
39b9b6a23f
62
bot.py
62
bot.py
@ -4,21 +4,19 @@
|
||||
# RSS Bot
|
||||
# Python 3 required
|
||||
|
||||
import logging
|
||||
import re
|
||||
import redis
|
||||
import feedparser
|
||||
from json import loads
|
||||
|
||||
from configparser import ConfigParser
|
||||
from json import loads
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import feedparser
|
||||
import redis
|
||||
from bs4 import BeautifulSoup
|
||||
from telegram import ChatAction, ParseMode
|
||||
from telegram.error import TelegramError, Unauthorized, BadRequest, TimedOut, NetworkError, ChatMigrated
|
||||
from telegram.ext import Updater, Job, CommandHandler, MessageHandler, Filters
|
||||
from telegram.ext.dispatcher import run_async
|
||||
from telegram.error import (TelegramError, Unauthorized, BadRequest,
|
||||
TimedOut, NetworkError, ChatMigrated)
|
||||
|
||||
import logging
|
||||
from bs4 import BeautifulSoup
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# Bot Configuration
|
||||
config = ConfigParser()
|
||||
@ -51,6 +49,7 @@ if not r.ping():
|
||||
print('Konnte nicht mit Redis verbinden, prüfe deine Einstellungen')
|
||||
quit()
|
||||
|
||||
|
||||
def is_number(s):
|
||||
try:
|
||||
float(s)
|
||||
@ -58,15 +57,18 @@ def is_number(s):
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def remove_tags(html):
|
||||
return ''.join(BeautifulSoup(html, "html.parser").findAll(text=True))
|
||||
|
||||
|
||||
def can_use(update):
|
||||
if update.message.from_user.id in admins:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def cleanRSS(str):
|
||||
str = str.replace('[…]', '')
|
||||
str = str.replace('[bilder]', '')
|
||||
@ -99,12 +101,14 @@ def cleanRSS(str):
|
||||
str = re.sub('http://www\.serienjunkies.de/.*\.html', '', str)
|
||||
return str
|
||||
|
||||
|
||||
def check_chat(bot, username):
|
||||
try:
|
||||
return bot.getChat(username)
|
||||
except:
|
||||
return
|
||||
|
||||
|
||||
# Commands
|
||||
@run_async
|
||||
def start(bot, update):
|
||||
@ -117,6 +121,7 @@ def start(bot, update):
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
|
||||
|
||||
@run_async
|
||||
def help(bot, update):
|
||||
if not can_use(update):
|
||||
@ -128,36 +133,42 @@ def help(bot, update):
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
|
||||
|
||||
def subscribe_to_rss(bot, update, args):
|
||||
if not can_use(update):
|
||||
return
|
||||
if len(args) < 1:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Feed-URL ein.', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Feed-URL ein.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
feed_url = args[0]
|
||||
is_url = re.search("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", feed_url)
|
||||
if not is_url:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dies ist keine URL.', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dies ist keine URL.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
if len(args) > 1:
|
||||
username = args[1]
|
||||
chat_info = check_chat(bot, username)
|
||||
if not chat_info:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
chat_id = str(chat_info.id)
|
||||
else:
|
||||
chat_id = str(update.message.chat_id)
|
||||
|
||||
if r.sismember('pythonbot:rss:' + chat_id, feed_url):
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Feed wurde bereits abonniert.', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Feed wurde bereits abonniert.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
bot.sendChatAction(update.message.chat_id, action=ChatAction.TYPING)
|
||||
feed_data = feedparser.parse(feed_url)
|
||||
if not 'link' in feed_data.feed:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Kein gültiger Feed.',reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Kein gültiger Feed.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
if not 'title' in feed_data.feed:
|
||||
@ -183,32 +194,37 @@ def subscribe_to_rss(bot, update, args):
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
|
||||
|
||||
def unsubscribe_rss(bot, update, args):
|
||||
if not can_use(update):
|
||||
return
|
||||
|
||||
if len(args) < 1:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Nummer ein', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Nummer ein',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
|
||||
if len(args) > 1:
|
||||
username = args[1]
|
||||
chat_info = check_chat(bot, username)
|
||||
if not chat_info:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
chat_id = str(chat_info.id)
|
||||
else:
|
||||
chat_id = str(update.message.chat_id)
|
||||
|
||||
if not is_number(args[0]):
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Nummer ein.', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Bitte gebe eine Nummer ein.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
uhash = 'pythonbot:rss:' + chat_id
|
||||
n = int(args[0])
|
||||
subs = list(r.smembers(uhash))
|
||||
if n < 1 or n > len(subs):
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Abonnement-ID ist zu hoch.', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Abonnement-ID ist zu hoch.',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
sub = subs[n - 1]
|
||||
lhash = 'pythonbot:rss:' + sub + ':subs'
|
||||
@ -235,6 +251,7 @@ def get_rss_list(chat_id, chat_name):
|
||||
text = text + str(n + 1) + ') ' + feed + '\n'
|
||||
return text
|
||||
|
||||
|
||||
@run_async
|
||||
def list_rss(bot, update, args):
|
||||
if not can_use(update):
|
||||
@ -243,7 +260,8 @@ def list_rss(bot, update, args):
|
||||
username = args[0]
|
||||
chat_info = check_chat(bot, username)
|
||||
if not chat_info:
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!', reply_to_message_id=update.message.message_id)
|
||||
bot.sendMessage(chat_id=update.message.chat_id, text='Dieser Kanal existiert nicht!',
|
||||
reply_to_message_id=update.message.message_id)
|
||||
return
|
||||
rss_list = get_rss_list(str(chat_info.id), chat_info.title)
|
||||
else:
|
||||
@ -259,6 +277,7 @@ def list_rss(bot, update, args):
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
|
||||
|
||||
def get_new_entries(last, new_entries):
|
||||
entries = []
|
||||
for k, v in enumerate(new_entries):
|
||||
@ -274,6 +293,7 @@ def get_new_entries(last, new_entries):
|
||||
entries.append(v)
|
||||
return entries
|
||||
|
||||
|
||||
def manually_check_rss(bot, update):
|
||||
if not can_use(update):
|
||||
return
|
||||
@ -284,6 +304,7 @@ def manually_check_rss(bot, update):
|
||||
reply_to_message_id=update.message.message_id
|
||||
)
|
||||
|
||||
|
||||
@run_async
|
||||
def check_rss(bot, job):
|
||||
keys = list(r.keys('pythonbot:rss:*:subs'))
|
||||
@ -357,6 +378,7 @@ def check_rss(bot, job):
|
||||
print('HTTP-Fehler: ' + str(feed_data.status))
|
||||
print('----------')
|
||||
|
||||
|
||||
def error(bot, update, error):
|
||||
logger.warn('Update "%s" verursachte Fehler "%s"' % (update, error))
|
||||
|
||||
|
Reference in New Issue
Block a user