logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

fetch_firefox_version.py (2074B)


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