Passed
Push — main ( 44031f...fd93eb )
by Sat CFDI
05:07
created

satcfdi.csf   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 69
ccs 42
cts 44
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 14

5 Functions

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 3 1
A url() 0 2 1
A _request_constancia() 0 11 2
B _parse_response() 0 25 7
A _find_regimen() 0 5 3
1 1
from datetime import datetime
2 1
import requests
3 1
import urllib3
4 1
from bs4 import BeautifulSoup
5
# noinspection PyUnresolvedReferences
6 1
from ..transform.catalog import CATALOGS
7 1
from .. import ResponseError, __version__, Code
8
9 1
try:
10 1
    urllib3.util.ssl_.DEFAULT_CIPHERS += ':HIGH:!DH'
11 1
except:
12 1
    pass
13
14
15 1
def retrieve(rfc: str, id_cif: str):
16 1
    data = _request_constancia(rfc, id_cif)
17 1
    return _parse_response(data)
18
19
20 1
def url(rfc: str, id_cif: str):
21 1
    return f"https://siat.sat.gob.mx/app/qr/faces/pages/mobile/validadorqr.jsf?D1=10&D2=1&D3={id_cif}_{rfc}"
22
23
24 1
def _request_constancia(rfc: str, id_cif: str):
25 1
    res = requests.get(
26
        url=url(rfc, id_cif),
27
        headers={
28
            "User-Agent": __version__.__user_agent__,
29
        }
30
    )
31 1
    if res.ok:
32 1
        return res.content
33
    else:
34
        raise ResponseError(res)
35
36
37 1
def _find_regimen(regimen):
38 1
    for k, v in CATALOGS['{http://www.sat.gob.mx/sitio_internet/cfd/catalogos}c_RegimenFiscal'].items():
39 1
        if regimen.endswith(v):
40 1
            return Code(k, v)
41
    return Code(None, regimen)
42
43
44 1
def _parse_response(data):
45 1
    _REGIMENES = "Regimenes"
46 1
    html = BeautifulSoup(data, 'html.parser')
47 1
    gc_v = html.find_all(name="td", attrs={"role": "gridcell", "style": "text-align:left;"})
48 1
    if not gc_v:
49 1
        raise ValueError("'rfc' or 'id_cif' is invalid")
50 1
    gc_k = html.find_all(name="span", attrs={"style": "font-weight: bold;"})
51 1
    result = {_REGIMENES: []}
52
53 1
    for k, v in zip(gc_k, gc_v):
54 1
        k = k.text.rstrip(":")
55 1
        v = v.text
56 1
        if k.startswith("Fecha ") and v:
57 1
            v = datetime.strptime(v, "%d-%m-%Y").date()
58
59 1
        if k == 'Régimen':
60 1
            result[_REGIMENES].append({
61
                'RegimenFiscal': _find_regimen(v),
62
            })
63 1
        elif k == 'Fecha de alta':
64 1
            result[_REGIMENES][-1][k] = v
65
        else:
66 1
            result[k] = v
67
68
    return result
69