2017-05-28 13:46:45 +00:00
|
|
|
#!/usr/bin/env python
|
2021-10-03 13:12:09 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-05-28 13:46:45 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import re
|
2021-02-25 16:42:52 +00:00
|
|
|
from os.path import dirname, join
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlparse, urljoin
|
2017-05-28 13:46:45 +00:00
|
|
|
from distutils.version import LooseVersion, StrictVersion
|
|
|
|
from lxml import html
|
2018-08-05 08:55:42 +00:00
|
|
|
from searx import searx_dir
|
2017-05-28 13:46:45 +00:00
|
|
|
|
|
|
|
URL = 'https://ftp.mozilla.org/pub/firefox/releases/'
|
|
|
|
RELEASE_PATH = '/pub/firefox/releases/'
|
|
|
|
|
2018-08-05 08:55:42 +00:00
|
|
|
NORMAL_REGEX = re.compile('^[0-9]+\.[0-9](\.[0-9])?$')
|
2017-05-28 13:46:45 +00:00
|
|
|
# BETA_REGEX = re.compile('.*[0-9]b([0-9\-a-z]+)$')
|
|
|
|
# ESR_REGEX = re.compile('^[0-9]+\.[0-9](\.[0-9])?esr$')
|
|
|
|
|
2021-10-03 13:12:09 +00:00
|
|
|
#
|
2018-08-05 08:55:42 +00:00
|
|
|
useragents = {
|
2021-12-27 08:16:03 +00:00
|
|
|
# fmt: off
|
2017-05-28 13:46:45 +00:00
|
|
|
"versions": (),
|
2021-12-16 22:10:39 +00:00
|
|
|
"os": ('Windows NT 10.0; Win64; x64',
|
2017-05-28 13:46:45 +00:00
|
|
|
'X11; Linux x86_64'),
|
2021-12-27 08:16:03 +00:00
|
|
|
"ua": "Mozilla/5.0 ({os}; rv:{version}) Gecko/20100101 Firefox/{version}",
|
|
|
|
# fmt: on
|
2017-05-28 13:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_firefox_versions():
|
|
|
|
resp = requests.get(URL, timeout=2.0)
|
|
|
|
if resp.status_code != 200:
|
|
|
|
raise Exception("Error fetching firefox versions, HTTP code " + resp.status_code)
|
|
|
|
else:
|
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
versions = []
|
|
|
|
|
|
|
|
for link in dom.xpath('//a/@href'):
|
|
|
|
url = urlparse(urljoin(URL, link))
|
|
|
|
path = url.path
|
|
|
|
if path.startswith(RELEASE_PATH):
|
|
|
|
version = path[len(RELEASE_PATH):-1]
|
|
|
|
if NORMAL_REGEX.match(version):
|
|
|
|
versions.append(LooseVersion(version))
|
|
|
|
|
|
|
|
list.sort(versions, reverse=True)
|
|
|
|
return versions
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_firefox_last_versions():
|
|
|
|
versions = fetch_firefox_versions()
|
|
|
|
|
|
|
|
result = []
|
|
|
|
major_last = versions[0].version[0]
|
|
|
|
major_list = (major_last, major_last - 1)
|
|
|
|
for version in versions:
|
|
|
|
major_current = version.version[0]
|
2018-08-05 08:55:42 +00:00
|
|
|
if major_current in major_list:
|
2017-05-28 13:46:45 +00:00
|
|
|
result.append(version.vstring)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-08-05 08:55:42 +00:00
|
|
|
def get_useragents_filename():
|
|
|
|
return join(join(searx_dir, "data"), "useragents.json")
|
|
|
|
|
|
|
|
|
|
|
|
useragents["versions"] = fetch_firefox_last_versions()
|
|
|
|
with open(get_useragents_filename(), "w") as f:
|
|
|
|
json.dump(useragents, f, indent=4, ensure_ascii=False)
|