instances.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Searx-qt.
  6. #
  7. # Searx-qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import time
  22. from searxqt.core.handler import HandlerProto
  23. class SearchEngine:
  24. def __init__(self, name, data):
  25. """Model for a search engine data.
  26. @param name: Name of the search engine
  27. @type name: str
  28. @param data: Data of the search engine
  29. @type data: dict
  30. """
  31. self._name = name
  32. self._data = data
  33. def __repr__(self): return self._name
  34. @property
  35. def name(self):
  36. """
  37. @return: returns the name of this search engine.
  38. @rtype: str
  39. """
  40. return self._name
  41. @property
  42. def enabled(self):
  43. """If this instance has this search engine enabled or not.
  44. @return:
  45. @rtype: bool
  46. """
  47. return self._data.get('enabled', False)
  48. @property
  49. def stats(self):
  50. """
  51. @return:
  52. @rtype: bool
  53. """
  54. return self._data.get('stats', False)
  55. class TLS:
  56. def __init__(self, data):
  57. """Model for a instance it's TLS data.
  58. @param data: dict with the instance it's TLS data.
  59. @type data: dict
  60. """
  61. self._data = data
  62. @property
  63. def data(self): return self._data
  64. @property
  65. def version(self):
  66. """Returns the TLS version used.
  67. @return:
  68. @rtype: str
  69. """
  70. return self.data.get('version', "")
  71. @property
  72. def certificate(self):
  73. """Returns a TLSCertificate object.
  74. @return:
  75. @rtype: TLSCertificate
  76. """
  77. return TLSCertificate(self.data.get('certificate', {}))
  78. class TLSCertificate:
  79. def __init__(self, data):
  80. """Model for a instance it's TLS certificate-data.
  81. @param data: dict with the instance it's TLS certificate-data.
  82. @type data: dict
  83. """
  84. self._data = data
  85. @property
  86. def data(self): return self._data
  87. @property
  88. def version(self):
  89. """
  90. @return:
  91. @rtype: int
  92. """
  93. return self.data.get('version', 0)
  94. @property
  95. def issuer(self):
  96. """
  97. @return:
  98. @rtype: TLSCertificateIssuer
  99. """
  100. return TLSCertificateIssuer(self.data.get('issuer', {}))
  101. class TLSCertificateIssuer:
  102. def __init__(self, data):
  103. """Model for a instance it's TLS certificate-issuer-data.
  104. @param data: dict with the instance it's
  105. TLS certificate-issuer-data.
  106. @type data: dict
  107. """
  108. self._data = data
  109. @property
  110. def data(self): return self._data
  111. @property
  112. def commonName(self):
  113. """
  114. @rtype: str
  115. """
  116. return self.data.get('commonName', "")
  117. @property
  118. def countryName(self):
  119. """
  120. @rtype: str
  121. """
  122. return self.data.get('countryName', "")
  123. @property
  124. def organizationName(self):
  125. """
  126. @rtype: str
  127. """
  128. return self.data.get('organizationName', "")
  129. class Network:
  130. def __init__(self, data):
  131. """Model for a instance it's network data.
  132. @param data: dict with the instance it's network data.
  133. @type data: dict
  134. """
  135. self._data = data
  136. @property
  137. def data(self): return self._data
  138. @property
  139. def ipv6(self):
  140. """
  141. @return: If ipv6 is enabled on this instance.
  142. @rtype: bool
  143. """
  144. return self.data.get('ipv6', False)
  145. @property
  146. def ips(self):
  147. """
  148. @return: A list with NetworkIP objects
  149. @rtype: list
  150. """
  151. return [NetworkIP(ip, data)
  152. for ip, data in self.data.get('ips', {}).items()]
  153. @property
  154. def asnPrivacy(self):
  155. """
  156. @return: -1 no asn privacy, 0 asn privacy, -2 unknown.
  157. @rtype: int
  158. """
  159. return self.data.get('asn_privacy', -2)
  160. class NetworkIP:
  161. def __init__(self, ip, data):
  162. """Model for a network ip data.
  163. @param ip: ip address
  164. @type ip: str
  165. @param data: dict with a network ip data.
  166. @type data: dict
  167. """
  168. self._ip = ip
  169. self._data = data
  170. def __repr__(self):
  171. return (
  172. "IP: {0} Reverse: {1} FieldType: {2} asnCidr: {3}"
  173. .format(self.ip, self.reverse, self.fieldType, self.asnCidr)
  174. )
  175. def __str__(self): return repr(self)
  176. @property
  177. def ip(self):
  178. """
  179. @return: ip address
  180. @rtype: str
  181. """
  182. return self._ip
  183. @property
  184. def data(self): return self._data
  185. @property
  186. def reverse(self):
  187. """
  188. @return:
  189. @rtype: str
  190. """
  191. return self.data.get('reverse', None)
  192. @property
  193. def fieldType(self):
  194. """
  195. @return: Record type
  196. @rtype: str
  197. """
  198. return self.data.get('field_type', None)
  199. @property
  200. def asnCidr(self):
  201. """
  202. @return:
  203. @rtype: str
  204. """
  205. return self.data.get('asn_cidr', None)
  206. @property
  207. def httpsPort(self):
  208. """
  209. @return:
  210. @rtype: bool
  211. """
  212. return self.data.get('https_port', None)
  213. class Instance:
  214. def __init__(self, url, data):
  215. """Model for a SearX instance.
  216. @param url: Url of the instance
  217. @type url: str
  218. @param data: Data of the instance
  219. @type data: dict
  220. """
  221. self._url = url
  222. self._data = data
  223. def __str__(self): return self.url
  224. def __repr__(self): return str(self)
  225. @property
  226. def data(self): return self._data
  227. @property
  228. def url(self):
  229. """
  230. @return: returns the url of this instance.
  231. @rtype: str
  232. """
  233. return self._url
  234. @property
  235. def main(self):
  236. """Returns False when not set.
  237. @return: ?
  238. @rtype: bool
  239. """
  240. return self._data.get('main', False)
  241. @property
  242. def networkType(self):
  243. """Returns a empty string when none found.
  244. Note: http, https and i2p return 'normal'; tor returns 'tor'.
  245. @return: Network protocol used (normal or tor)
  246. @rtype: str
  247. """
  248. return self._data.get('network_type', '')
  249. @property
  250. def version(self):
  251. """Returns a empty string when none found.
  252. @return: Returns the instance it's version.
  253. @rtype: str
  254. """
  255. return self._data.get('version', '')
  256. @property
  257. def engines(self):
  258. """
  259. Returns a empty string when none found.
  260. @return: Returns a list with SearchEngine objects
  261. @rtype: list
  262. """
  263. return [SearchEngine(name, data) for name, data in self._data.get(
  264. 'engines', {}).items()]
  265. @property
  266. def tls(self):
  267. """
  268. @rtype: TLS
  269. """
  270. return TLS(self._data.get('tls', {}))
  271. @property
  272. def network(self):
  273. """
  274. @rtype: Network
  275. """
  276. return Network(self._data.get('network', {}))
  277. class Stats2(HandlerProto):
  278. """ This class holds the instances.json data and will be passed
  279. to other classes (Instances, Engines)
  280. """
  281. URL = "https://searx.space/"
  282. def __init__(self, requestsHandler):
  283. HandlerProto.__init__(self, requestsHandler)
  284. def updateInstances(self):
  285. """Fetches instances.json from a searx-stats2 instance.
  286. @param requestKwargs: Kwargs to pass to requests.get
  287. @type requestKwargs: dict
  288. @return: A tuple (bool Success/Failed, str Message)
  289. @rtype: tuple
  290. """
  291. url = Stats2.URL.rstrip('/') + '/data/instances.json'
  292. result = self.requestsHandler.get(url)
  293. if result:
  294. self.setData(result.json())
  295. self._lastUpdated = time.time()
  296. return True
  297. return False