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.

104 lines
2.6 KiB

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