From 09cf4e51e78ad7fdb13f181308f8c7b423a0d06c Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Mon, 11 Feb 2019 12:04:04 +0100 Subject: [PATCH] first version of /hamster and /reddit --- reddit.py | 33 +++++++++++++++++++++++++++++++-- schneiderbot.py | 29 ++++++++++++++++++----------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/reddit.py b/reddit.py index 504a387..411c5bd 100644 --- a/reddit.py +++ b/reddit.py @@ -1,4 +1,13 @@ import praw +import requests + + +def get_post_type(url): + header = requests.head(url).headers + if 'Content-Type' in header: + return header['Content-Type'] + else: + return '' def get_reddit_instance(): @@ -16,9 +25,29 @@ def get_reddit_instance(): def wrong_dog(bot, update): - reddit = get_reddit_instance() + reddit_instance = get_reddit_instance() - sub = reddit.subreddit('whatswrongwithyourdog') + sub = reddit_instance.subreddit('whatswrongwithyourdog') post = sub.random() update.message.reply_text("This dog has serious issues: %s" % post.url) + +def hamster(bot, update): + reddit_instance = get_reddit_instance() + + sub = reddit_instance.subreddit('hamsters') + post = sub.random() + while "image" not in get_post_type(post.url): + post = sub.random() + bot.send_photo(chat_id=update.message.chat_id, photo=post.url) + + +def reddit(bot, update, args): + if not args: + update.message.reply_text("Geile, sag sub") + return + sub = args[0] + reddit_instance = get_reddit_instance() + + post = reddit_instance.subreddit(sub).random() + update.message.reply_text(post.url) diff --git a/schneiderbot.py b/schneiderbot.py index a457957..77007e5 100755 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -10,7 +10,7 @@ from telegram import ParseMode from telegram.ext import Updater, CommandHandler from coding_love import send_coding_love_gif -from reddit import wrong_dog +from reddit import wrong_dog, hamster, reddit from db import Db from mensa import mensa @@ -185,16 +185,7 @@ def error(bot, update, error): logger.warning('Update "%s" caused error "%s"', update, error) -def main(): - """Start the bot.""" - # Create the EventHandler and pass it your bot's token. - token = open("token").read() - updater = Updater(token.strip()) - - # Get the dispatcher to register handlers - dp = updater.dispatcher - - # on different commands - answer in Telegram +def init_commands(dp): dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("magie", magie)) @@ -212,6 +203,22 @@ def main(): dp.add_handler(CommandHandler("graveyard", graveyard)) dp.add_handler(CommandHandler("codinglove", send_coding_love_gif)) dp.add_handler(CommandHandler("wrongdog", wrong_dog)) + dp.add_handler(CommandHandler("hamster", hamster)) + dp.add_handler(CommandHandler("reddit", reddit, pass_args=True)) + + +def main(): + """Start the bot.""" + # Create the EventHandler and pass it your bot's token. + token = open("token").read() + # token = open("test_token").read() + updater = Updater(token.strip()) + + # Get the dispatcher to register handlers + dp = updater.dispatcher + init_commands(dp) + + # on different commands - answer in Telegram # log all errors dp.add_error_handler(error)