|
|
import praw from prawcore import exceptions import requests
four_twenty_groups = []
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(): reddit_config = {} with open("config/reddit_config") as tmp_file: for line in tmp_file: (key, val) = line.split('=') reddit_config[key] = val.replace('\n', '')
return praw.Reddit( user_agent=reddit_config['user_agent'], client_id=reddit_config['client_id'], client_secret=reddit_config['client_secret'] )
def get_reddit_post(sub, is_image_post=False): reddit_instance = get_reddit_instance()
try: sub = reddit_instance.subreddit(sub) post = sub.random()
except exceptions.NotFound: return "Geile, gib gültiges Sub" except exceptions.Forbidden: return "Geile, gib freies Sub" except Exception as e: return e
if post is None: return "Random geht in diesem Sub leider nicht :("
if is_image_post: tries = 0 while "image" not in get_post_type(post.url) and tries < 5: post = sub.random() tries += 1 if tries >= 5: return "Geile, gib Sub mit Bildern (über 5 Fehlversuche)"
return post
def toggle_four_twenty(bot, update): chat_id = update.message.chat_id
if chat_id in four_twenty_groups: four_twenty_groups.remove(chat_id) update.message.reply_text("You killed the fire!") else: four_twenty_groups.append(update.message.chat_id) update.message.reply_text("420 blaze it!")
def four_twenty(bot, update): for group_id in four_twenty_groups: post = get_reddit_post('trees', True) bot.send_photo(chat_id=group_id, photo=post.url, caption=post.title)
def wrong_dog(bot, update): reddit_img(bot, update, ['whatswrongwithyourdog'])
def hamster(bot, update): reddit_img(bot, update, ['hamsters'])
def cat(bot, update): reddit_img(bot, update, ['catpictures'])
def dog(bot, update): reddit_img(bot, update, ['dogpictures'])
def reddit(bot, update, args): if not args: update.message.reply_text("Geile, sag sub") return
sub = args[0] update.message.reply_text(get_reddit_post(sub).url)
def reddit_img(bot, update, args): if not args: update.message.reply_text("Geile, sag sub") return sub = args[0] post = get_reddit_post(sub, True)
try: bot.send_photo(chat_id=update.message.chat_id, photo=post.url) return except AttributeError: update.message.reply_text(post) print("ERROR")
|