update_firefox_version.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. import json
  3. import requests
  4. import re
  5. from os.path import dirname, join
  6. from urllib.parse import urlparse, urljoin
  7. from packaging.version import Version, parse
  8. from lxml import html
  9. from searx import searx_dir
  10. URL = 'https://ftp.mozilla.org/pub/firefox/releases/'
  11. RELEASE_PATH = '/pub/firefox/releases/'
  12. NORMAL_REGEX = re.compile('^[0-9]+\.[0-9](\.[0-9])?$')
  13. # BETA_REGEX = re.compile('.*[0-9]b([0-9\-a-z]+)$')
  14. # ESR_REGEX = re.compile('^[0-9]+\.[0-9](\.[0-9])?esr$')
  15. #
  16. useragents = {
  17. "versions": (),
  18. "os": ('Windows NT 10.0; WOW64',
  19. 'X11; Linux x86_64'),
  20. "ua": "Mozilla/5.0 ({os}; rv:{version}) Gecko/20100101 Firefox/{version}"
  21. }
  22. def fetch_firefox_versions():
  23. resp = requests.get(URL, timeout=2.0)
  24. if resp.status_code != 200:
  25. raise Exception("Error fetching firefox versions, HTTP code " + resp.status_code)
  26. else:
  27. dom = html.fromstring(resp.text)
  28. versions = []
  29. for link in dom.xpath('//a/@href'):
  30. url = urlparse(urljoin(URL, link))
  31. path = url.path
  32. if path.startswith(RELEASE_PATH):
  33. version = path[len(RELEASE_PATH):-1]
  34. if NORMAL_REGEX.match(version):
  35. versions.append(Version(version))
  36. list.sort(versions, reverse=True)
  37. return versions
  38. def fetch_firefox_last_versions():
  39. versions = fetch_firefox_versions()
  40. result = []
  41. major_last = versions[0].major
  42. major_list = (major_last, major_last - 1)
  43. for version in versions:
  44. major_current = version.major
  45. if major_current in major_list:
  46. result.append(str(version))
  47. return result
  48. def get_useragents_filename():
  49. return join(join(searx_dir, "data"), "useragents.json")
  50. useragents["versions"] = fetch_firefox_last_versions()
  51. with open(get_useragents_filename(), "w") as f:
  52. json.dump(useragents, f, indent=4, ensure_ascii=False)