pokemon-tv.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. from flask import Flask
  3. import requests
  4. import datetime
  5. import re
  6. import json
  7. error_message = 'Uh oh, could not connect to pokemon tv api'
  8. def api():
  9. data = requests.get("https://www.pokemon.com/api/pokemontv/v2/channels/us/")
  10. if data.status_code == 200:
  11. return data.json()
  12. else:
  13. return error_message
  14. app = Flask(__name__)
  15. @app.route('/')
  16. def home():
  17. data = api()
  18. if data == error_message:
  19. return error_message
  20. text = "<div class='row' style='display:flex; flex-wrap: wrap; padding: 0 4px;'>\n"
  21. index = 0
  22. for i, channel in enumerate(data):
  23. stuff = [0, 5, 10, 15, 20, 25, 30]
  24. if (i in stuff) and (i != 30):
  25. index = index + 1
  26. print(i)
  27. text += "<div class='column' style='flex: 50%; padding: 0 4px;'>"
  28. for i in range(stuff[index]):
  29. text += f"""
  30. <img style='margin-top: 8px; vertical-align: middle;' src='{channel['channel_images']['dashboard_image_1125_1500']}'>
  31. """
  32. text += "</div>"
  33. text += "</div>"
  34. return text
  35. @app.route('/us/pokemon-news/<url>')
  36. def article(url):
  37. data = requests.get("https://www.pokemon.com/us/pokemon-news/"+url).text
  38. with open("index.html", "w") as f:
  39. f.write(data)
  40. find = json.loads(re.findall('<script type="application/ld\+json">((.|\n)*?)</script>', data)[0][0])["articleBody"]
  41. return find
  42. if __name__ == '__main__':
  43. app.run(port=80)