Completed
Branch master (9117d2)
by Steffen
03:33 queued 12s
created

saucenao.http.verify_status_code()   C

Complexity

Conditions 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 5.5
c 0
b 0
f 0
cc 7
nop 2
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