|
5 | 5 | import json |
6 | 6 | import re |
7 | 7 | import time |
| 8 | +from typing import Any |
8 | 9 | from datetime import datetime |
9 | 10 |
|
10 | 11 | import requests |
@@ -288,24 +289,44 @@ def set_total(self) -> None: |
288 | 289 |
|
289 | 290 | def retrieve_records(self) -> list: |
290 | 291 | """Retrieve records from DBLP.""" |
291 | | - # try: |
| 292 | + ret: Any |
292 | 293 | while True: |
293 | | - # print(self.url) |
294 | | - ret = self.session.request( |
295 | | - "GET", self.url, headers=self.headers, timeout=self._timeout # type: ignore |
296 | | - ) |
| 294 | + try: |
| 295 | + ret = self.session.request( |
| 296 | + "GET", self.url, headers=self.headers, timeout=self._timeout # type: ignore |
| 297 | + ) |
| 298 | + except ( |
| 299 | + requests.exceptions.ConnectionError, |
| 300 | + requests.exceptions.Timeout, |
| 301 | + requests.exceptions.HTTPError, |
| 302 | + requests.exceptions.RequestException, |
| 303 | + ) as exc: |
| 304 | + raise colrev_exceptions.ServiceNotAvailableException( |
| 305 | + "DBLP API is currently not available or returned an invalid response" |
| 306 | + ) from exc |
297 | 307 |
|
298 | 308 | if ret.status_code == 429: |
299 | 309 | time.sleep(60) |
300 | 310 | print("Waiting for 60 seconds (request limit reached)") |
301 | 311 | continue |
302 | | - ret.raise_for_status() |
303 | | - # 429 - too many requests |
304 | | - if ret.status_code == 500: |
305 | | - return [] |
| 312 | + if ret.status_code >= 500: |
| 313 | + raise colrev_exceptions.ServiceNotAvailableException( |
| 314 | + "DBLP API is currently not available or returned an invalid response" |
| 315 | + ) |
| 316 | + try: |
| 317 | + ret.raise_for_status() |
| 318 | + except requests.exceptions.HTTPError as exc: |
| 319 | + raise colrev_exceptions.ServiceNotAvailableException( |
| 320 | + "DBLP API is currently not available or returned an invalid response" |
| 321 | + ) from exc |
306 | 322 | break |
307 | 323 |
|
308 | | - data = json.loads(ret.text) |
| 324 | + try: |
| 325 | + data = json.loads(ret.text) |
| 326 | + except (TypeError, ValueError, json.JSONDecodeError) as exc: |
| 327 | + raise colrev_exceptions.ServiceNotAvailableException( |
| 328 | + "DBLP API is currently not available or returned an invalid response" |
| 329 | + ) from exc |
309 | 330 | response_ms = float(data["result"]["time"]["text"]) |
310 | 331 | time.sleep(response_ms / 10) |
311 | 332 |
|
|
0 commit comments