123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- import time
- from searxqt.core import log
- from searxqt.core.handler import HandlerProto, NetworkTypes
- from searxqt.core.instanceVersions import InstanceVersion
- from searxqt.core.requests import JsonResult, ErrorType, Schemas
- class SearchEngine:
- def __init__(self, name, data):
- """Model for a search engine data.
- @param name: Name of the search engine
- @type name: str
- @param data: Data of the search engine
- @type data: dict
- """
- self._name = name
- self._data = data
- def __repr__(self): return self._name
- @property
- def name(self):
- """
- @return: returns the name of this search engine.
- @rtype: str
- """
- return self._name
- class TLS:
- def __init__(self, data):
- """Model for a instance it's TLS data.
- @param data: dict with the instance it's TLS data.
- @type data: dict
- """
- self._data = data
- @property
- def data(self): return self._data
- @property
- def version(self):
- """Returns the TLS version used.
- @return:
- @rtype: str
- """
- return self.data.get('version', "")
- @property
- def certificate(self):
- """Returns a TLSCertificate object.
- @return:
- @rtype: TLSCertificate
- """
- return TLSCertificate(self.data.get('certificate', {}))
- class TLSCertificate:
- def __init__(self, data):
- """Model for a instance it's TLS certificate-data.
- @param data: dict with the instance it's TLS certificate-data.
- @type data: dict
- """
- self._data = data
- @property
- def data(self): return self._data
- @property
- def version(self):
- """
- @return:
- @rtype: int
- """
- return self.data.get('version', 0)
- @property
- def issuer(self):
- """
- @return:
- @rtype: TLSCertificateIssuer
- """
- return TLSCertificateIssuer(self.data.get('issuer', {}))
- class TLSCertificateIssuer:
- def __init__(self, data):
- """Model for a instance it's TLS certificate-issuer-data.
- @param data: dict with the instance it's
- TLS certificate-issuer-data.
- @type data: dict
- """
- self._data = data
- @property
- def data(self): return self._data
- @property
- def commonName(self):
- """
- @rtype: str
- """
- return self.data.get('commonName', "")
- @property
- def countryName(self):
- """
- @rtype: str
- """
- return self.data.get('countryName', "")
- @property
- def organizationName(self):
- """
- @rtype: str
- """
- return self.data.get('organizationName', "")
- class Network:
- def __init__(self, data):
- """Model for a instance it's network data.
- @param data: dict with the instance it's network data.
- @type data: dict
- """
- self._data = data
- @property
- def data(self): return self._data
- @property
- def ipv6(self):
- """
- @return: If ipv6 is enabled on this instance.
- @rtype: bool
- """
- return self.data.get('ipv6', False)
- @property
- def ips(self):
- """
- @return: A list with NetworkIP objects
- @rtype: list
- """
- return [NetworkIP(ip, data)
- for ip, data in self.data.get('ips', {}).items()]
- @property
- def asnPrivacy(self):
- """
- @return: -1 no asn privacy, 0 asn privacy, -2 unknown.
- @rtype: int
- """
- return self.data.get('asn_privacy', -2)
- class NetworkIP:
- def __init__(self, ip, data):
- """Model for a network ip data.
- @param ip: ip address
- @type ip: str
- @param data: dict with a network ip data.
- @type data: dict
- """
- self._ip = ip
- self._data = data
- def __repr__(self):
- return (
- f"IP: {self.ip} Reverse: {self.reverse} " \
- f"FieldType: {self.fieldType} asnCidr: {self.asnCidr}"
- )
- def __str__(self): return repr(self)
- @property
- def ip(self):
- """
- @return: ip address
- @rtype: str
- """
- return self._ip
- @property
- def data(self): return self._data
- @property
- def reverse(self):
- """
- @return:
- @rtype: str
- """
- return self.data.get('reverse', None)
- @property
- def fieldType(self):
- """
- @return: Record type
- @rtype: str
- """
- return self.data.get('field_type', None)
- @property
- def asnCidr(self):
- """
- @return:
- @rtype: str
- """
- return self.data.get('asn_cidr', None)
- @property
- def httpsPort(self):
- """
- @return:
- @rtype: bool
- """
- return self.data.get('https_port', None)
- class Instance:
- def __init__(self, url, data):
- """Model for a SearX instance.
- @param url: Url of the instance
- @type url: str
- @param data: Data of the instance
- @type data: dict
- """
- self._url = url
- self._data = data
- self._version = InstanceVersion(self._data.get('version', ''))
- if not self._version.isValid():
- log.debug(f"Invalid version {self._version}: {self._version.error()}", cls=Instance)
- def __str__(self): return self.url
- def __repr__(self): return str(self)
- @property
- def data(self): return self._data
- @property
- def url(self):
- """
- @return: returns the url of this instance.
- @rtype: str
- """
- return self._url
- @property
- def main(self):
- """Returns False when not set.
- @return: ?
- @rtype: bool
- """
- return self._data.get('main', False)
- @property
- def networkType(self):
- """ Network type; see core/handler.py:NetworkTypes
- @return: Network protocol used (Web, Tor, I2P from NetworkTypes)
- @rtype: int
- """
- return self._data.get('network_type', 0)
- @property
- def version(self):
- """Returns a empty string when none found.
- @return: Returns the instance it's version.
- @rtype: InstanceVersion
- """
- return self._version
- @property
- def engines(self):
- """
- Returns a empty string when none found.
- @return: Returns a list with SearchEngine objects
- @rtype: list
- """
- return [SearchEngine(name, data) for name, data in self._data.get(
- 'engines', {}).items()]
- @property
- def tls(self):
- """
- @rtype: TLS
- """
- return TLS(self._data.get('tls', {}))
- @property
- def network(self):
- """
- @rtype: Network
- """
- return Network(self._data.get('network', {}))
- class Stats2Result(JsonResult):
- Schema = Schemas['searx_space_instances']
- def __init__(self, url, response, err="", errType=ErrorType.Success):
- JsonResult.__init__(self, url, response, err=err, errType=errType)
- class Stats2(HandlerProto):
- """ This class holds the instances.json data and will be passed
- to other classes (Instances, Engines)
- """
- URL = "https://searx.space/"
- def __init__(self, requestsHandler):
- HandlerProto.__init__(self, requestsHandler)
- def updateInstances(self):
- """Fetches instances.json from a searx-stats2 instance.
- @param requestKwargs: Kwargs to pass to requests.get
- @type requestKwargs: dict
- @return: A tuple (bool Success/Failed, str Message)
- @rtype: tuple
- """
- url = Stats2.URL.rstrip('/') + '/data/instances.json'
- result = self.requestsHandler.get(url, ResultType=Stats2Result)
- if result:
- self.setData(result.json())
- self._lastUpdated = time.time()
-
- for url, data in self.instances.items():
- data.update({"network_type": NetworkTypes.netTypeFromUrl(url)})
- return result
|