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.

59 lines
1.4 KiB

6 years ago
  1. import praw
  2. import requests
  3. def get_post_type(url):
  4. header = requests.head(url).headers
  5. if 'Content-Type' in header:
  6. return header['Content-Type']
  7. else:
  8. return ''
  9. def get_reddit_instance():
  10. reddit_config = {}
  11. with open("reddit_config") as tmp_file:
  12. for line in tmp_file:
  13. (key, val) = line.split(';')
  14. reddit_config[key] = val.replace('\n', '')
  15. return praw.Reddit(
  16. user_agent=reddit_config['user_agent'],
  17. client_id=reddit_config['client_id'],
  18. client_secret=reddit_config['client_secret']
  19. )
  20. def wrong_dog(bot, update):
  21. reddit(bot, update, ['whatswrongwithyourdog'])
  22. def hamster(bot, update):
  23. reddit_img(bot, update, ['hamsters'])
  24. def reddit(bot, update, args):
  25. if not args:
  26. update.message.reply_text("Geile, sag sub")
  27. return
  28. sub = args[0]
  29. reddit_instance = get_reddit_instance()
  30. post = reddit_instance.subreddit(sub).random()
  31. update.message.reply_text(post.url)
  32. def reddit_img(bot, update, args):
  33. print(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. sub = reddit_instance.subreddit(desired_sub)
  40. post = sub.random()
  41. while "image" not in get_post_type(post.url):
  42. post = sub.random()
  43. bot.send_photo(chat_id=update.message.chat_id, photo=post.url)