handler.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 urllib.parse
  22. class NetworkTypes:
  23. """ Custom definitions for network types, the ones from stats2 don't
  24. include I2P.
  25. """
  26. Web = 0
  27. Tor = 1
  28. I2P = 2
  29. def netTypeFromUrl(url):
  30. """Returns the network type as a int based on the given
  31. url it's extension.
  32. """
  33. parsedUrl = urllib.parse.urlparse(url)
  34. extSplit = parsedUrl.hostname.split('.')
  35. ext = extSplit[(len(extSplit)-1)]
  36. if ext == 'onion':
  37. return NetworkTypes.Tor
  38. elif ext == 'i2p':
  39. return NetworkTypes.I2P
  40. else:
  41. return NetworkTypes.Web
  42. class HandlerProto:
  43. def __init__(self, requestsHandler):
  44. """ Prototype for handling/updating instances and engines data.
  45. @param requestsHandler: Handler for doing requests
  46. @type requestsHandler: searxqt.core.requests.RequestsHandler
  47. """
  48. self.requestsHandler = requestsHandler
  49. self._instances = {}
  50. self._engines = {}
  51. self._lastUpdated = 0
  52. def lastUpdated(self): return self._lastUpdated
  53. @property
  54. def instances(self): return self._instances
  55. @property
  56. def engines(self): return self._engines
  57. def data(self):
  58. return {
  59. 'instances': self._instances,
  60. 'engines': self._engines,
  61. 'lastUpdate': self._lastUpdated
  62. }
  63. def setData(self, data):
  64. """
  65. @param data: json object (instances.json)
  66. @type data: dict/json
  67. """
  68. self.clear()
  69. self._instances.update(data.get('instances', {}))
  70. self._engines.update(data.get('engines', {}))
  71. self._lastUpdated = data.get('lastUpdate', 0)
  72. def clear(self):
  73. self._instances.clear()
  74. self._engines.clear()