2017-05-28 15:46:45 +02:00
|
|
|
#!/usr/bin/env python
|
2022-01-03 12:58:48 +01:00
|
|
|
# lint: pylint
|
2021-10-03 15:12:09 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2022-01-03 12:40:06 +01:00
|
|
|
"""Fetch firefox useragent signatures
|
|
|
|
|
|
|
|
Output file: :origin:`searx/data/useragents.json` (:origin:`CI Update data ...
|
|
|
|
<.github/workflows/data-update.yml>`).
|
|
|
|
|
|
|
|
"""
|
2023-02-10 13:40:12 +01:00
|
|
|
# pylint: disable=use-dict-literal
|
2022-01-03 12:40:06 +01:00
|
|
|
|
2017-05-28 15:46:45 +02:00
|
|
|
import json
|
|
|
|
import re
|
2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import urlparse, urljoin
|
2023-02-09 18:42:59 +01:00
|
|
|
from packaging.version import parse
|
2022-01-03 12:58:48 +01:00
|
|
|
|
|
|
|
import requests
|
2017-05-28 15:46:45 +02:00
|
|
|
from lxml import html
|
2024-03-10 15:33:23 +01:00
|
|
|
from searx.data import data_dir
|
|
|
|
|
|
|
|
DATA_FILE = data_dir / 'useragents.json'
|
2017-05-28 15:46:45 +02:00
|
|
|
|
|
|
|
URL = 'https://ftp.mozilla.org/pub/firefox/releases/'
|
|
|
|
RELEASE_PATH = '/pub/firefox/releases/'
|
|
|
|
|
2022-01-03 12:58:48 +01:00
|
|
|
NORMAL_REGEX = re.compile(r'^[0-9]+\.[0-9](\.[0-9])?$')
|
|
|
|
# BETA_REGEX = re.compile(r'.*[0-9]b([0-9\-a-z]+)$')
|
|
|
|
# ESR_REGEX = re.compile(r'^[0-9]+\.[0-9](\.[0-9])?esr$')
|
2017-05-28 15:46:45 +02:00
|
|
|
|
2021-10-03 15:12:09 +02:00
|
|
|
#
|
2018-08-05 10:55:42 +02:00
|
|
|
useragents = {
|
2021-12-27 09:16:03 +01:00
|
|
|
# fmt: off
|
2017-05-28 15:46:45 +02:00
|
|
|
"versions": (),
|
2021-12-16 23:10:39 +01:00
|
|
|
"os": ('Windows NT 10.0; Win64; x64',
|
2017-05-28 15:46:45 +02:00
|
|
|
'X11; Linux x86_64'),
|
2023-12-04 08:18:10 +01:00
|
|
|
"ua": "Mozilla/5.0 ({os}; rv:{version}) Gecko/20100101 Firefox/{version}",
|
2021-12-27 09:16:03 +01:00
|
|
|
# fmt: on
|
2017-05-28 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_firefox_versions():
|
|
|
|
resp = requests.get(URL, timeout=2.0)
|
|
|
|
if resp.status_code != 200:
|
2023-02-10 13:40:12 +01:00
|
|
|
# pylint: disable=broad-exception-raised
|
2024-03-10 15:33:23 +01:00
|
|
|
raise Exception("Error fetching firefox versions, HTTP code " + resp.status_code) # type: ignore
|
2022-01-03 12:58:48 +01:00
|
|
|
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):
|
2023-02-09 18:42:59 +01:00
|
|
|
versions.append(parse(version))
|
2022-01-03 12:58:48 +01:00
|
|
|
|
|
|
|
list.sort(versions, reverse=True)
|
|
|
|
return versions
|
2017-05-28 15:46:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def fetch_firefox_last_versions():
|
|
|
|
versions = fetch_firefox_versions()
|
|
|
|
|
|
|
|
result = []
|
2023-02-09 18:42:59 +01:00
|
|
|
major_last = versions[0].major
|
2017-05-28 15:46:45 +02:00
|
|
|
major_list = (major_last, major_last - 1)
|
|
|
|
for version in versions:
|
2023-02-09 18:42:59 +01:00
|
|
|
major_current = version.major
|
|
|
|
minor_current = version.minor
|
2018-08-05 10:55:42 +02:00
|
|
|
if major_current in major_list:
|
2022-11-05 22:04:37 +01:00
|
|
|
user_agent_version = f'{major_current}.{minor_current}'
|
|
|
|
if user_agent_version not in result:
|
|
|
|
result.append(user_agent_version)
|
2017-05-28 15:46:45 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-01-03 12:40:06 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
useragents["versions"] = fetch_firefox_last_versions()
|
2024-03-10 15:33:23 +01:00
|
|
|
with DATA_FILE.open('w', encoding='utf-8') as f:
|
|
|
|
json.dump(useragents, f, indent=4, sort_keys=True, ensure_ascii=False)
|