From 636d2286abdcc9c7fe23ab5890a6fca7f7b004c6 Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Tue, 5 Feb 2019 14:54:08 +0100 Subject: [PATCH] PEP8 formatting added /codinglove feature --- coding_love.py | 22 ++++++++++++++++++++++ db.py | 2 +- schneiderbot.py | 20 +++++++++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 coding_love.py diff --git a/coding_love.py b/coding_love.py new file mode 100644 index 0000000..c8edffa --- /dev/null +++ b/coding_love.py @@ -0,0 +1,22 @@ +import requests +import re + +url = "https://thecodinglove.com/" + + +def send_coding_love_gif(bot, update): + random_url = re.search('href="(.*)".*\n.*random\(\)', requests.get(url).text).group(1) + + random_page_content = requests.get(random_url).text + caption = re.search('blog-post-title">(.*)', random_page_content).group(1) + + gif_match = re.search('data="(.*)" type="image/gif"', random_page_content) + if gif_match is None: + gif_match = re.search('og:image" cont ent="(.*)"', random_page_content) + + if gif_match: + gif_url = gif_match.group(1) + bot.send_animation(chat_id=update.message.chat_id, animation=gif_url, caption=caption) + + else: + update.message.reply_text("Error fetching image: %s ¯\_(ツ)_/¯" % random_url) diff --git a/db.py b/db.py index 196ee93..5cedb13 100644 --- a/db.py +++ b/db.py @@ -8,7 +8,7 @@ class Db(): def __init__(self): self.conn = sqlite3.connect("deaths.db") table_exist = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='death'") - if table_exist.fetchall() == []: + if not table_exist.fetchall(): self.conn.execute("CREATE TABLE death(name text, chat integer )") self.conn.execute("create index chat_idx on death(chat)") diff --git a/schneiderbot.py b/schneiderbot.py index 1670e38..d7286ed 100755 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -5,6 +5,7 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram import ParseMode from db import Db from mensa import mensa +from coding_love import send_coding_love_gif import logging import random import requests @@ -61,13 +62,13 @@ def help(bot, update): /help - this help text""", parse_mode=ParseMode.MARKDOWN) -def sendCat(bot, update): +def send_cat(bot, update): """CUTE""" cat_file = requests.get('http://aws.random.cat/meow').json()['file'] bot.send_photo(chat_id=update.message.chat_id, photo=cat_file) -def sendDog(bot, update): +def send_dog(bot, update): """CUTE 2.0""" dog = pydog.PyDog() bot.send_photo(chat_id=update.message.chat_id, photo=dog.get_random_image()) @@ -105,7 +106,7 @@ def revive(bot, update, sudocall=False): update.message.reply_text("You fool! You cannot revive a dead person!") -SUDOCMDS = { +SUDO_CMDS = { "kill": kill, "revive": revive } @@ -123,13 +124,13 @@ def sudo(bot, update, args): update.message.reply_text("Unknown command") return for arg in args: - if arg in SUDOCMDS: - SUDOCMDS[arg](bot, update, True) + if arg in SUDO_CMDS: + SUDO_CMDS[arg](bot, update, True) else: update.message.reply_text("Unknown command") -def sendCatDog(bot, update): +def send_cat_dog(bot, update): """Best of both worlds!""" catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg" bot.send_photo(chat_id=update.message.chat_id, photo=catdog) @@ -197,14 +198,15 @@ def main(): dp.add_handler(CommandHandler("goodlife", goodlife)) dp.add_handler(CommandHandler("mensa", mensa, pass_args=True)) dp.add_handler(CommandHandler("sudo", sudo, pass_args=True)) - dp.add_handler(CommandHandler("cat", sendCat)) - dp.add_handler(CommandHandler("dog", sendDog)) - dp.add_handler(CommandHandler("catdog", sendCatDog)) + dp.add_handler(CommandHandler("cat", send_cat)) + dp.add_handler(CommandHandler("dog", send_dog)) + dp.add_handler(CommandHandler("catdog", send_cat_dog)) dp.add_handler(CommandHandler("shrug", shrug)) dp.add_handler(CommandHandler("kill", kill)) dp.add_handler(CommandHandler("revive", revive)) dp.add_handler(CommandHandler("simon", simon, pass_args=True)) dp.add_handler(CommandHandler("graveyard", graveyard)) + dp.add_handler(CommandHandler("codinglove", send_coding_love_gif)) # log all errors dp.add_error_handler(error)