|
20 | 20 | You should have received a copy of the GNU General Public License |
21 | 21 | along with OpenWebScraper. If not, see <https://www.gnu.org/licenses/>. |
22 | 22 | """ |
| 23 | +import json |
23 | 24 | import os |
24 | 25 | import subprocess |
25 | 26 | import sys |
| 27 | +import threading |
26 | 28 |
|
27 | 29 | from PyQt5.QtWidgets import QComboBox, QPushButton, QVBoxLayout, QGroupBox, QLineEdit, QHBoxLayout, QMessageBox |
28 | 30 |
|
@@ -114,6 +116,14 @@ def setup_behaviour(self): |
114 | 116 | else: |
115 | 117 | self._view.crawl_name_input.textChanged.connect(self.update_model) |
116 | 118 |
|
| 119 | + # test connection to scrapy wrapper and produce feedback |
| 120 | + def set_info(info): |
| 121 | + if "version" in info: |
| 122 | + self.master_cnt.set_initializer_info(f"Connected to OWS-scrapy-wrapper version {info['version']}") |
| 123 | + else: |
| 124 | + self.master_cnt.set_initializer_info(info['message'], color="red") |
| 125 | + thread = threading.Thread(target=check_scrapy_connection, args=[set_info]) |
| 126 | + thread.start() |
117 | 127 |
|
118 | 128 | def update_model(self): |
119 | 129 | if self.master_cnt: |
@@ -269,3 +279,21 @@ def start_scrapy(settings_path): |
269 | 279 | close_fds=True) |
270 | 280 | except Exception as exc: |
271 | 281 | LOG.exception("{0}: {1}".format(type(exc).__name__, exc)) |
| 282 | + |
| 283 | + |
| 284 | +def check_scrapy_connection(callback): |
| 285 | + scrapy_script = SETTINGS["general"]["scrapy_wrapper_exec"] |
| 286 | + command = scrapy_script + " INFO" |
| 287 | + LOG.info("Running {0}".format(command)) |
| 288 | + try: |
| 289 | + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False) |
| 290 | + |
| 291 | + info_json, _ = proc.communicate() |
| 292 | + |
| 293 | + info = json.loads(info_json.splitlines()[-1]) |
| 294 | + LOG.info("Connected to OWS-scrapy-wrapper version {0}".format(info["version"])) |
| 295 | + except Exception as exc: |
| 296 | + LOG.exception("{0}: {1}".format(type(exc).__name__, exc)) |
| 297 | + info = dict(message="Could not connect to OWS-scrapy-wrapper. Check your config.") |
| 298 | + |
| 299 | + callback(info) |
0 commit comments