Total Complexity | 8 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 92.31% |
Changes | 0 |
1 | #!/usr/bin/python |
||
2 | # -*- coding: utf-8 -*- |
||
3 | 1 | import time |
|
4 | |||
5 | 1 | import requests |
|
6 | |||
7 | 1 | from saucenao.exceptions import * |
|
8 | |||
9 | 1 | PREVIOUS_STATUS_CODE = None |
|
10 | |||
11 | 1 | STATUS_CODE_OK = 1 |
|
12 | 1 | STATUS_CODE_SKIP = 2 |
|
13 | 1 | STATUS_CODE_REPEAT = 3 |
|
14 | |||
15 | |||
16 | 1 | def verify_status_code(request_response: requests.Response, file_name: str) -> tuple: |
|
17 | """Verify the status code of the post request to the search url and raise exceptions if the code is unexpected |
||
18 | |||
19 | :type request_response: requests.Response |
||
20 | :type file_name: str |
||
21 | :return: |
||
22 | """ |
||
23 | 1 | if request_response.status_code == 200: |
|
24 | 1 | return STATUS_CODE_OK, '' |
|
25 | |||
26 | 1 | elif request_response.status_code == 429: |
|
27 | 1 | if 'Search Rate Too High.' in request_response.text: |
|
28 | msg = "Search rate limit reached" |
||
29 | return STATUS_CODE_REPEAT, msg |
||
30 | 1 | if 'limit of 150 searches' in request_response.text: |
|
31 | 1 | raise DailyLimitReachedException('Daily search limit for unregistered users reached') |
|
32 | 1 | elif 'limit of 300 searches' in request_response.text: |
|
33 | 1 | raise DailyLimitReachedException('Daily search limit for basic users reached') |
|
34 | else: |
||
35 | 1 | raise DailyLimitReachedException('Daily search limit reached') |
|
36 | 1 | elif request_response.status_code == 403: |
|
37 | 1 | raise InvalidOrWrongApiKeyException("Invalid or wrong API key") |
|
38 | 1 | elif request_response.status_code == 413: |
|
39 | 1 | msg = "Payload too large, skipping file: {0:s}".format(file_name) |
|
40 | 1 | return STATUS_CODE_SKIP, msg |
|
41 | else: |
||
42 | 1 | msg = "Unknown status code: {0:d}".format(request_response.status_code) |
|
43 | return STATUS_CODE_REPEAT, msg |
||
44 |