|
1
|
|
|
import os |
|
2
|
|
|
from itertools import batched |
|
3
|
|
|
|
|
4
|
|
|
import requests |
|
5
|
|
|
import yaml |
|
6
|
|
|
from bs4 import BeautifulSoup |
|
7
|
|
|
from satcfdi.exceptions import ResponseError |
|
8
|
|
|
|
|
9
|
|
|
from satdigitalinvoice.log_tools import NoAliasDumper |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def format_clavecat(clavecat: str): |
|
13
|
|
|
if "-" in clavecat: |
|
14
|
|
|
return clavecat |
|
15
|
|
|
return "-".join([ |
|
16
|
|
|
"".join(a) for a in batched(clavecat, 3)] |
|
17
|
|
|
) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def request_predial(predial: str): |
|
21
|
|
|
predial = format_clavecat(predial) |
|
22
|
|
|
|
|
23
|
|
|
r = requests.post( |
|
24
|
|
|
url="https://predial.torreon.gob.mx/app/php/get_datos_predio.php", |
|
25
|
|
|
data={ |
|
26
|
|
|
"txt_clavecat": predial |
|
27
|
|
|
} |
|
28
|
|
|
) |
|
29
|
|
|
if r.status_code == 200: |
|
30
|
|
|
return _parse_response(r.content) |
|
31
|
|
|
|
|
32
|
|
|
raise ResponseError(r) |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def process_predial(folder, predial: str): |
|
36
|
|
|
res = request_predial(predial) |
|
37
|
|
|
|
|
38
|
|
|
if pr := res['predial_recibo']: |
|
39
|
|
|
r = requests.get( |
|
40
|
|
|
url=pr |
|
41
|
|
|
) |
|
42
|
|
|
pdf_file = os.path.join(folder, f"{predial}.pdf") |
|
43
|
|
|
with open(pdf_file, "wb") as f: |
|
44
|
|
|
f.write(r.content) |
|
45
|
|
|
|
|
46
|
|
|
if pec := res['predial_estado_cuenta']: |
|
47
|
|
|
r = requests.get( |
|
48
|
|
|
url=pec |
|
49
|
|
|
) |
|
50
|
|
|
pdf_file = os.path.join(folder, f"{predial}_adeudo.pdf") |
|
51
|
|
|
with open(pdf_file, "wb") as f: |
|
52
|
|
|
f.write(r.content) |
|
53
|
|
|
|
|
54
|
|
|
yaml_file = os.path.join(folder, f"{predial}.yaml") |
|
55
|
|
|
with open(yaml_file, "w", encoding="utf-8") as fs: |
|
56
|
|
|
yaml.dump(res, fs, Dumper=NoAliasDumper, allow_unicode=True, width=1280, sort_keys=False) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def _parse_response(data): |
|
60
|
|
|
if b'No se ha encontrado coincidencia con su clave catastral' in data: |
|
61
|
|
|
return None |
|
62
|
|
|
if b'La cuenta se encuentra excenta de pago' in data: |
|
63
|
|
|
return None |
|
64
|
|
|
|
|
65
|
|
|
res = {} |
|
66
|
|
|
html = BeautifulSoup(data, 'html.parser') |
|
67
|
|
|
i = html.find_all(name="td") |
|
68
|
|
|
for k, v in batched(i, 2): # iterate in pairs |
|
69
|
|
|
res[k.text.strip().rstrip(":")] = v.text.strip() |
|
70
|
|
|
|
|
71
|
|
|
i = html.find_all(name="div", attrs={"class": "text-xs font-weight-bold text-primary text-uppercase mb-1"}) |
|
72
|
|
|
j = html.find_all(name="div", attrs={"class": "encabezado-principal mb-0 font-weight-bold text-gray-800"}) |
|
73
|
|
|
|
|
74
|
|
|
for k, v in zip(i, j): |
|
75
|
|
|
res[k.text.strip()] = v.text.split("$", maxsplit=1)[1] |
|
76
|
|
|
|
|
77
|
|
|
i = html.find_all(name="div", attrs={"class": "text-xs font-weight-bold text-primary text-uppercase mb-1 text-center"}) |
|
78
|
|
|
res['TOTAL'] = i[0].text.split("$", maxsplit=1)[1] |
|
79
|
|
|
|
|
80
|
|
|
i = html.find_all(name="a") |
|
81
|
|
|
for k in i: |
|
82
|
|
|
href = k['href'] |
|
83
|
|
|
for v in ["predial_estado_cuenta", "predial_recibo"]: |
|
84
|
|
|
if v in href: |
|
85
|
|
|
res[v] = href |
|
86
|
|
|
else: |
|
87
|
|
|
res[v] = None |
|
88
|
|
|
|
|
89
|
|
|
return res |
|
90
|
|
|
|