engines.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. class EnginesModel:
  22. def __init__(self, handler=None):
  23. self._handler = handler
  24. self._data = {}
  25. if handler:
  26. self._data = handler.engines
  27. @property
  28. def handler(self): return self._handler
  29. def __contains__(self, name):
  30. return bool(name in self._data)
  31. def __getitem__(self, engineName):
  32. return Engine(engineName, self._data[engineName])
  33. def __str__(self): return str([engineName for engineName in self.keys()])
  34. def __repr__(self): return str(self)
  35. def __len__(self): return len(self._data)
  36. def data(self): return self._data
  37. def items(self):
  38. return [
  39. (name, Engine(name, data))
  40. for name, data in self._data.items()
  41. ]
  42. def keys(self): return self._data.keys()
  43. def values(self):
  44. return [
  45. Engine(name, data)
  46. for name, data in self._data.items()
  47. ]
  48. def categories(self):
  49. """ Returns a list with all categories, collected from instances.
  50. """
  51. categories = []
  52. for engine in self.values():
  53. for cat in engine.categories:
  54. if cat not in categories:
  55. categories.append(cat)
  56. return categories
  57. def getByCategory(self, category):
  58. """ Returns a list with Engine's that are part of the given
  59. category.
  60. @param category: category name
  61. @type category: str
  62. @rtype: list
  63. """
  64. result = []
  65. for engine in self.values():
  66. if category in engine.categories:
  67. result.append(engine)
  68. return result
  69. class Stats2Engines(EnginesModel):
  70. def __init__(self, handler):
  71. """
  72. @param handler: Object that holds the instances.json data.
  73. @type handler: core.instances.Stats2
  74. """
  75. EnginesModel.__init__(self, handler)
  76. class Engine:
  77. def __init__(self, engineName, engineData):
  78. self._data = engineData
  79. self._name = engineName
  80. @property
  81. def data(self):
  82. return self._data
  83. @property
  84. def name(self):
  85. """
  86. @return: returns the name of this engine
  87. @rtype: str
  88. """
  89. return self._name
  90. @property
  91. def categories(self):
  92. """
  93. @return: list with search categories
  94. @rtype: list
  95. """
  96. return self._data.get('categories', [])
  97. @property
  98. def languageSupport(self):
  99. """
  100. @return: If this engine has language support
  101. @rtype: bool
  102. """
  103. return self._data.get('language_support', False)
  104. @property
  105. def paging(self):
  106. """
  107. @return: If this engine has paging support
  108. @rtype: bool
  109. """
  110. return self._data.get('paging', False)
  111. @property
  112. def safesearch(self):
  113. """
  114. @return: If this engine has safe-search support
  115. @rtype: bool
  116. """
  117. return self._data.get('safesearch', False)
  118. @property
  119. def shortcut(self):
  120. """
  121. @return: Shortcut name for this engine
  122. @rtype: str
  123. """
  124. return self._data.get('shortcut', "")
  125. @property
  126. def timeRangeSupport(self):
  127. """
  128. @return: If this engine has time-range support
  129. @rtype: bool
  130. """
  131. return self._data.get('time_range_support', False)