From 267c4a7640c66f602bfba29740a66df75503334a Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Fri, 15 Feb 2019 18:52:25 +0100 Subject: [PATCH] hotfix reddit --- reddit.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/reddit.py b/reddit.py index ad7401d..dd668ec 100644 --- a/reddit.py +++ b/reddit.py @@ -1,4 +1,5 @@ import praw +from prawcore import exceptions import requests @@ -44,16 +45,32 @@ def reddit(bot, update, args): def reddit_img(bot, update, args): - print(args) - if not args: update.message.reply_text("Geile, sag sub") return desired_sub = args[0] reddit_instance = get_reddit_instance() - sub = reddit_instance.subreddit(desired_sub) - post = sub.random() - while "image" not in get_post_type(post.url): + try: + sub = reddit_instance.subreddit(desired_sub) + post = sub.random() + print(post.url) + except exceptions.NotFound: + update.message.reply_text("Geile, gib gültiges Sub") + return + except exceptions.Forbidden: + update.message.reply_text("Geile, gib freies Sub") + return + except Exception as e: + update.message.reply_text(e) + return + + tries = 0 + while "image" not in get_post_type(post.url) and tries < 5: post = sub.random() + tries += 1 + if tries >= 5: + update.message.reply_text("Geile, gib Sub mit Bildern (über 5 Fehlversuche)") + return + bot.send_photo(chat_id=update.message.chat_id, photo=post.url)