You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.6 KiB
106 lines
2.6 KiB
import praw
|
|
import requests
|
|
from prawcore import exceptions
|
|
from telegram import Update
|
|
from telegram.ext import CallbackContext
|
|
|
|
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 "Ungültiges Sub :("
|
|
except exceptions.Forbidden:
|
|
return "Privates/Gesperrtes 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 "Keine Bilder gefunden (über 5 Fehlversuche)"
|
|
|
|
return post
|
|
|
|
|
|
def wrong_dog(update: Update, context: CallbackContext):
|
|
context.args = ['whatswrongwithyourdog']
|
|
reddit_img(update, context)
|
|
|
|
|
|
def hamster(update: Update, context: CallbackContext):
|
|
context.args = ['hamsters']
|
|
reddit_img(update, context)
|
|
|
|
|
|
def drillcat(update: Update, context: CallbackContext):
|
|
context.args = ['drillcats']
|
|
reddit_img(update, context)
|
|
|
|
|
|
def cat(update: Update, context: CallbackContext):
|
|
context.args = ['catpictures']
|
|
reddit_img(update, context)
|
|
|
|
|
|
def dog(update: Update, context: CallbackContext):
|
|
context.args = ['dogpictures']
|
|
reddit_img(update, context)
|
|
|
|
|
|
def reddit(update: Update, context: CallbackContext):
|
|
if not context.args:
|
|
update.message.reply_text("Bitte Sub angeben")
|
|
return
|
|
|
|
sub = context.args[0]
|
|
update.message.reply_text(get_reddit_post(sub).url)
|
|
|
|
|
|
def reddit_img(update: Update, context: CallbackContext):
|
|
if not context.args:
|
|
update.message.reply_text("Bitte Sub angeben")
|
|
return
|
|
sub = context.args[0]
|
|
post = get_reddit_post(sub, True)
|
|
|
|
try:
|
|
context.bot.send_photo(chat_id=update.message.chat_id, photo=post.url)
|
|
return
|
|
except AttributeError:
|
|
update.message.reply_text(post)
|
|
print("ERROR")
|