| Total Complexity | 7 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | #!/usr/bin/python |
||
|
|
|||
| 2 | # -*- coding: utf-8 -*- |
||
| 3 | import requests |
||
| 4 | |||
| 5 | from saucenao.exceptions import * |
||
| 6 | |||
| 7 | PREVIOUS_STATUS_CODE = None |
||
| 8 | |||
| 9 | STATUS_CODE_OK = 1 |
||
| 10 | STATUS_CODE_SKIP = 2 |
||
| 11 | STATUS_CODE_REPEAT = 3 |
||
| 12 | |||
| 13 | |||
| 14 | def verify_status_code(request_response: requests.Response, file_name: str) -> tuple: |
||
| 15 | """Verify the status code of the post request to the search url and raise exceptions if the code is unexpected |
||
| 16 | |||
| 17 | :type request_response: requests.Response |
||
| 18 | :type file_name: str |
||
| 19 | :return: |
||
| 20 | """ |
||
| 21 | if request_response.status_code == 200: |
||
| 22 | return STATUS_CODE_OK, '' |
||
| 23 | |||
| 24 | elif request_response.status_code == 429: |
||
| 25 | if 'limit of 150 searches' in request_response.text: |
||
| 26 | raise DailyLimitReachedException('Daily search limit for unregistered users reached') |
||
| 27 | elif 'limit of 300 searches' in request_response.text: |
||
| 28 | raise DailyLimitReachedException('Daily search limit for basic users reached') |
||
| 29 | else: |
||
| 30 | raise DailyLimitReachedException('Daily search limit reached') |
||
| 31 | elif request_response.status_code == 403: |
||
| 32 | raise InvalidOrWrongApiKeyException("Invalid or wrong API key") |
||
| 33 | elif request_response.status_code == 413: |
||
| 34 | msg = "Payload too large, skipping file: {0:s}".format(file_name) |
||
| 35 | return STATUS_CODE_SKIP, msg |
||
| 36 | else: |
||
| 37 | msg = "Unknown status code: {0:d}".format(request_response.status_code) |
||
| 38 | return STATUS_CODE_REPEAT, msg |
||
| 39 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.