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.

76 lines
1.9 KiB

6 years ago
6 years ago
6 years ago
6 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 wrong_dog(bot, update):
  22. reddit(bot, update, ['whatswrongwithyourdog'])
  23. def hamster(bot, update):
  24. reddit_img(bot, update, ['hamsters'])
  25. def reddit(bot, update, args):
  26. if not args:
  27. update.message.reply_text("Geile, sag sub")
  28. return
  29. sub = args[0]
  30. reddit_instance = get_reddit_instance()
  31. post = reddit_instance.subreddit(sub).random()
  32. update.message.reply_text(post.url)
  33. def reddit_img(bot, update, args):
  34. if not args:
  35. update.message.reply_text("Geile, sag sub")
  36. return
  37. desired_sub = args[0]
  38. reddit_instance = get_reddit_instance()
  39. try:
  40. sub = reddit_instance.subreddit(desired_sub)
  41. post = sub.random()
  42. print(post.url)
  43. except exceptions.NotFound:
  44. update.message.reply_text("Geile, gib gültiges Sub")
  45. return
  46. except exceptions.Forbidden:
  47. update.message.reply_text("Geile, gib freies Sub")
  48. return
  49. except Exception as e:
  50. update.message.reply_text(e)
  51. return
  52. tries = 0
  53. while "image" not in get_post_type(post.url) and tries < 5:
  54. post = sub.random()
  55. tries += 1
  56. if tries >= 5:
  57. update.message.reply_text("Geile, gib Sub mit Bildern (über 5 Fehlversuche)")
  58. return
  59. bot.send_photo(chat_id=update.message.chat_id, photo=post.url)