settings.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 random
  22. from PyQt5.QtCore import QObject, pyqtSignal
  23. from searxqt.core.instances import Stats2
  24. class SettingsModel(QObject):
  25. changed = pyqtSignal()
  26. def __init__(self, parent=None):
  27. QObject.__init__(self, parent=parent)
  28. self._requests = RequestSettingsModel()
  29. self._stats2 = None
  30. self._requests.changed.connect(self.changed)
  31. def loadSettings(self, data, stats2=True):
  32. self.requests.data = data.get('requests', {})
  33. if stats2:
  34. self._stats2 = SearxStats2Model()
  35. stats2Data = data.get('stats2', {})
  36. self.stats2.loadSettings(stats2Data)
  37. elif self._stats2:
  38. self._stats2.deleteLater()
  39. self._stats2 = None
  40. def saveSettings(self):
  41. if self._stats2:
  42. return {
  43. 'requests': self.requests.data,
  44. 'stats2': self.stats2.saveSettings()
  45. }
  46. return {
  47. 'requests': self.requests.data,
  48. }
  49. @property
  50. def requests(self): return self._requests
  51. @property
  52. def stats2(self): return self._stats2
  53. class SearxStats2Model(QObject):
  54. changed = pyqtSignal()
  55. def __init__(self, parent=None):
  56. QObject.__init__(self, parent=parent)
  57. self._backupUrl = Stats2.URL
  58. def loadSettings(self, data):
  59. self.url = data.get('url', self._backupUrl)
  60. def saveSettings(self):
  61. return {
  62. 'url': self.url
  63. }
  64. def reset(self):
  65. self.url = self._backupUrl
  66. @property
  67. def url(self):
  68. return Stats2.URL
  69. @url.setter
  70. def url(self, url):
  71. Stats2.URL = url
  72. self.changed.emit()
  73. class RequestSettingsModel(QObject):
  74. changed = pyqtSignal()
  75. def __init__(self, parent=None):
  76. QObject.__init__(self, parent=parent)
  77. self._kwargs = {
  78. 'verify': True,
  79. 'timeout': 10,
  80. }
  81. self._randomUserAgent = True
  82. self._useragents = ['searx-qt']
  83. def __changed(self): self.changed.emit()
  84. @property
  85. def data(self):
  86. return {
  87. 'useragents': self._useragents,
  88. 'randomUserAgent': self._randomUserAgent,
  89. 'kwargs': self._kwargs
  90. }
  91. @data.setter
  92. def data(self, data):
  93. self._useragents = data.get('useragents', ['searx-qt'])
  94. self._randomUserAgent = data.get('randomUserAgent', True)
  95. kwargs = data.get('kwargs', {})
  96. self._kwargs.update({'verify': kwargs.get('verify', True)})
  97. self._kwargs.update({'timeout': kwargs.get('timeout', 10)})
  98. self._kwargs.update({'proxies': kwargs.get('proxies', {})})
  99. @property
  100. def kwargs(self):
  101. """ Returns kwargs to be passed to requests.get
  102. """
  103. kwargs = self._kwargs.copy()
  104. kwargs.update({'headers': self.headers})
  105. return kwargs
  106. @kwargs.setter
  107. def kwargs(self, kwargs):
  108. self._kwargs = kwargs
  109. @property
  110. def verify(self): return self._kwargs['verify']
  111. @verify.setter
  112. def verify(self, state):
  113. self._kwargs['verify'] = state
  114. self.__changed()
  115. @property
  116. def timeout(self): return self._kwargs['timeout']
  117. @timeout.setter
  118. def timeout(self, state):
  119. self._kwargs['timeout'] = state
  120. self.__changed()
  121. @property
  122. def proxies(self): return self._kwargs.get('proxies', {})
  123. @proxies.setter
  124. def proxies(self, data):
  125. self._kwargs.update({'proxies': data})
  126. self.__changed()
  127. @property
  128. def headers(self):
  129. headers = {}
  130. if self.useragent:
  131. headers.update({'user-agent': self.useragent})
  132. return headers
  133. @property
  134. def useragent(self):
  135. if not self._useragents:
  136. return ""
  137. if len(self._useragents) == 1 or not self._randomUserAgent:
  138. return self._useragents[0]
  139. return random.choice(self._useragents)
  140. @property
  141. def useragents(self):
  142. return self._useragents
  143. @useragents.setter
  144. def useragents(self, strList):
  145. """
  146. @param strList: A string with user-agents, separated by a new line.
  147. May be empty.
  148. @type strList: str
  149. """
  150. self._useragents = strList
  151. self.__changed()
  152. @property
  153. def randomUserAgent(self): return self._randomUserAgent
  154. @randomUserAgent.setter
  155. def randomUserAgent(self, state):
  156. """
  157. @param state: True to select random user-agent for each request
  158. If False it will select the first user-agent. When
  159. the user-agents list is empty the send string will
  160. be also empty.
  161. @type state: bool
  162. """
  163. self._randomUserAgent = state
  164. self.__changed()