Telegram version of schneiderbot
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

6 years ago
6 years ago
6 years ago
  1. import praw
  2. import requests
  3. from prawcore import exceptions
  4. from telegram import Update
  5. from telegram.ext import CallbackContext
  6. four_twenty_groups = []
  7. def get_post_type(url):
  8. header = requests.head(url).headers
  9. if 'Content-Type' in header:
  10. return header['Content-Type']
  11. else:
  12. return ''
  13. def get_reddit_instance():
  14. reddit_config = {}
  15. with open("config/reddit_config") as tmp_file:
  16. for line in tmp_file:
  17. (key, val) = line.split('=')
  18. reddit_config[key] = val.replace('\n', '')
  19. return praw.Reddit(
  20. user_agent=reddit_config['user_agent'],
  21. client_id=reddit_config['client_id'],
  22. client_secret=reddit_config['client_secret']
  23. )
  24. def get_reddit_post(sub, is_image_post=False):
  25. reddit_instance = get_reddit_instance()
  26. try:
  27. sub = reddit_instance.subreddit(sub)
  28. post = sub.random()
  29. except exceptions.NotFound:
  30. return "Ungültiges Sub :("
  31. except exceptions.Forbidden:
  32. return "Privates/Gesperrtes Sub :("
  33. except Exception as e:
  34. return e
  35. if post is None:
  36. return "Random geht in diesem Sub leider nicht :("
  37. if is_image_post:
  38. tries = 0
  39. while "image" not in get_post_type(post.url) and tries < 5:
  40. post = sub.random()
  41. tries += 1
  42. if tries >= 5:
  43. return "Keine Bilder gefunden (über 5 Fehlversuche)"
  44. return post
  45. def wrong_dog(update: Update, context: CallbackContext):
  46. context.args = ['whatswrongwithyourdog']
  47. reddit_img(update, context)
  48. def hamster(update: Update, context: CallbackContext):
  49. context.args = ['hamsters']
  50. reddit_img(update, context)
  51. def drillcat(update: Update, context: CallbackContext):
  52. context.args = ['drillcats']
  53. reddit_img(update, context)
  54. def cat(update: Update, context: CallbackContext):
  55. context.args = ['catpictures']
  56. reddit_img(update, context)
  57. def dog(update: Update, context: CallbackContext):
  58. context.args = ['dogpictures']
  59. reddit_img(update, context)
  60. def reddit(update: Update, context: CallbackContext):
  61. if not context.args:
  62. update.message.reply_text("Bitte Sub angeben")
  63. return
  64. sub = context.args[0]
  65. update.message.reply_text(get_reddit_post(sub).url)
  66. def reddit_img(update: Update, context: CallbackContext):
  67. if not context.args:
  68. update.message.reply_text("Bitte Sub angeben")
  69. return
  70. sub = context.args[0]
  71. post = get_reddit_post(sub, True)
  72. try:
  73. context.bot.send_photo(chat_id=update.message.chat_id, photo=post.url)
  74. return
  75. except AttributeError:
  76. update.message.reply_text(post)
  77. print("ERROR")