1
|
|
|
import os |
2
|
|
|
from itertools import batched |
3
|
|
|
|
4
|
|
|
import requests |
5
|
|
|
import yaml |
6
|
|
|
from bs4 import BeautifulSoup |
7
|
|
|
|
8
|
|
|
from satdigitalinvoice.log_tools import NoAliasDumper |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def format_clavecat(clavecat: str): |
12
|
|
|
if "-" in clavecat: |
13
|
|
|
return clavecat |
14
|
|
|
return "-".join([ |
15
|
|
|
"".join(a) for a in batched(clavecat, 3)] |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def request_predial(predial: str): |
20
|
|
|
predial = format_clavecat(predial) |
21
|
|
|
|
22
|
|
|
r = requests.post( |
23
|
|
|
url="https://predial.torreon.gob.mx/app/php/get_datos_predio.php", |
24
|
|
|
data={ |
25
|
|
|
"txt_clavecat": predial |
26
|
|
|
} |
27
|
|
|
) |
28
|
|
|
assert r.status_code == 200 |
29
|
|
|
res = _parse_response(r.content) |
30
|
|
|
return res |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def process_predial(folder, predial: str): |
34
|
|
|
res = request_predial(predial) |
35
|
|
|
if not res: |
36
|
|
|
print("No encontrado", predial) |
37
|
|
|
return |
38
|
|
|
|
39
|
|
|
if res['predial_recibo'] is not None: |
40
|
|
|
r = requests.get( |
41
|
|
|
url=res['predial_recibo'] |
42
|
|
|
) |
43
|
|
|
pdf_file = os.path.join(folder, f"{predial}.pdf") |
44
|
|
|
with open(pdf_file, "wb") as f: |
45
|
|
|
f.write(r.content) |
46
|
|
|
else: |
47
|
|
|
r = requests.get( |
48
|
|
|
url=res['predial_estado_cuenta'] |
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
|
|
|
|