Telegram version of schneiderbot. I'll totally do something w/ this, this is never going to be effectively a mirror.
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.

84 lines
2.0 KiB

5 years ago
5 years ago
  1. import praw
  2. from prawcore import exceptions
  3. import requests
  4. def get_post_type(url):
  5. header = requests.head(url).headers
  6. if 'Content-Type' in header:
  7. return header['Content-Type']
  8. else:
  9. return ''
  10. def get_reddit_instance():
  11. reddit_config = {}
  12. with open("reddit_config") as tmp_file:
  13. for line in tmp_file:
  14. (key, val) = line.split(';')
  15. reddit_config[key] = val.replace('\n', '')
  16. return praw.Reddit(
  17. user_agent=reddit_config['user_agent'],
  18. client_id=reddit_config['client_id'],
  19. client_secret=reddit_config['client_secret']
  20. )
  21. def get_reddit_post(sub, is_image_post=False):
  22. reddit_instance = get_reddit_instance()
  23. try:
  24. sub = reddit_instance.subreddit(sub)
  25. post = sub.random()
  26. except exceptions.NotFound:
  27. return "Geile, gib gültiges Sub"
  28. except exceptions.Forbidden:
  29. return "Geile, gib freies Sub"
  30. except Exception as e:
  31. return e
  32. if post == None:
  33. return "Random geht in diesem Sub leider nicht :("
  34. if is_image_post:
  35. tries = 0
  36. while "image" not in get_post_type(post.url) and tries < 5:
  37. post = sub.random()
  38. tries += 1
  39. if tries >= 5:
  40. return "Geile, gib Sub mit Bildern (über 5 Fehlversuche)"
  41. return post.url
  42. def wrong_dog(bot, update):
  43. reddit(bot, update, ['whatswrongwithyourdog'])
  44. def hamster(bot, update):
  45. reddit_img(bot, update, ['hamsters'])
  46. def reddit(bot, update, args):
  47. if not args:
  48. update.message.reply_text("Geile, sag sub")
  49. return
  50. sub = args[0]
  51. update.message.reply_text(get_reddit_post(sub))
  52. def reddit_img(bot, update, args):
  53. if not args:
  54. update.message.reply_text("Geile, sag sub")
  55. return
  56. sub = args[0]
  57. post = get_reddit_post(sub, True)
  58. try:
  59. bot.send_photo(chat_id=update.message.chat_id, photo=post.url)
  60. return
  61. except AttributeError:
  62. update.message.reply_text(post)