getDesByUrl.py 906 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. import requests
  3. import re
  4. import html
  5. import json
  6. import os, sys
  7. url = sys.argv[1]
  8. headers = {
  9. 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0'
  10. }
  11. def get_desmos_html(url):
  12. n = os.path.basename(url)
  13. r = requests.get(url, headers=headers)
  14. if r.status_code is not 200:
  15. return None
  16. return (r.text, n)
  17. def html_2_desmos_file(data):
  18. text, name = data
  19. result = re.findall(r'data\-load\-data="(.*?)"', text)
  20. if not len(result) == 1:
  21. return
  22. res = html.unescape(result[0])
  23. res = json.loads(res)
  24. output = res['graph']['state']
  25. with open(name+'.des', 'w') as fo:
  26. json.dump(output, fo, ensure_ascii=False, indent=4)
  27. # for f_n in os.listdir('.'):
  28. # if f_n.endswith('html'):
  29. # get_desmos_file(f_n)
  30. text = get_desmos_html(sys.argv[1])
  31. html_2_desmos_file(text)