coin-counter.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 urban dictionary api'
  8. def api():
  9. data = requests.get("https://www.pokemon.com/api/1/us/news/get-news.json")
  10. if data.status_code == 200:
  11. return data.json()
  12. else:
  13. return error_message
  14. # for article in data:
  15. # print(f"""<item>
  16. # <title>{article['title']}</title>
  17. # <link>{"https://pokemon.com"+article['url']}</link>
  18. # <pubdate>{datetime.datetime.strptime(article['date'], '%B %d, %Y').strftime('%a, %d %b %Y')}</pubdate>
  19. # <description><![CDATA[<img src="https://pokemon.com{article['image']}" alt="{article['alt']}">
  20. #{article['shortDescription']}]]></description>
  21. #</item>""")
  22. app = Flask(__name__)
  23. @app.route('/')
  24. def home():
  25. data = api()
  26. if data == error_message:
  27. return error_message
  28. text = ""
  29. for article in data:
  30. text = text + f"""
  31. <div class='{article['id']}'>
  32. <p>{article['date']}</p>
  33. <h1><a href='{article['url']}'>{article['title']}</a></h1>
  34. <img src='https://pokemon.com{article['image']}' alt='{article['alt']}'>
  35. <p>{article['shortDescription']}</p>
  36. </div>
  37. <hr>
  38. """
  39. return text
  40. @app.route('/us/pokemon-news/<url>')
  41. def article(url):
  42. data = requests.get("https://www.pokemon.com/us/pokemon-news/"+url).text
  43. with open("index.html", "w") as f:
  44. f.write(data)
  45. find = json.loads(re.findall('<script type="application/ld\+json">((.|\n)*?)</script>', data)[0][0])["articleBody"]
  46. return find
  47. if __name__ == '__main__':
  48. app.run(port=80)