fetch_firefox_version.py 2.0KB

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