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.

39 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. import requests
  2. import re
  3. import html
  4. from telegram import Update
  5. from telegram.ext import CallbackContext
  6. urls = {
  7. "codinglove": "https://thecodinglove.com/",
  8. "wikihow": "https://de.wikihow.com/Spezial:Randomizer"
  9. }
  10. def send_coding_love_gif(update: Update, context: CallbackContext):
  11. random_url = re.search('href="(.*)".*\n.*\n.*random\( \)', requests.get(urls["codinglove"]).text).group(1)
  12. random_page_content = requests.get(random_url)
  13. random_page_content.encoding = 'utf-8'
  14. random_page_content = html.unescape(random_page_content.text)
  15. caption = re.search('blog-post-title">(.*)</h1>', random_page_content).group(1)
  16. gif_match = re.search('data="(.*)" type="image/gif"', random_page_content)
  17. if gif_match is None:
  18. gif_match = re.search('og:image" content="(.*)"', random_page_content)
  19. if gif_match:
  20. gif_url = gif_match.group(1)
  21. context.bot.send_animation(chat_id=update.message.chat_id, animation=gif_url, caption=caption)
  22. else:
  23. update.message.reply_text("Error fetching image: {} ¯\_(ツ)_/¯".format(random_url))
  24. def send_wiki_how_article(update: Update, context: CallbackContext):
  25. random_howto = requests.get(urls["wikihow"])
  26. howto_title = re.search('<title>(.*)</title>', random_howto.text).group(1)
  27. howto_url = re.search('<link rel="canonical" href="(.*)".*/>', random_howto.text).group(1)
  28. update.message.reply_text(howto_title + "\n" + howto_url)