|
5 | 5 | import os
|
6 | 6 | import re
|
7 | 7 |
|
8 |
| -from lxml.html import fromstring |
9 | 8 | import requests
|
10 | 9 |
|
11 | 10 | logger = logging.getLogger(__name__)
|
|
15 | 14 | class CepTracker(object):
|
16 | 15 | url = os.getenv(
|
17 | 16 | "CORREIOS_CEP_URL",
|
18 |
| - "http://www.buscacep.correios.com.br/sistemas/buscacep/resultadoBuscaCepEndereco.cfm?t" # NOQA |
| 17 | + "https://buscacepinter.correios.com.br/app/endereco/carrega-cep-endereco.php", # NOQA |
19 | 18 | )
|
20 | 19 |
|
21 | 20 | def _request(self, cep):
|
22 | 21 | response = requests.post(self.url, data={
|
23 |
| - "relaxation": cep, |
24 |
| - "Metodo": "listaLogradouro", |
25 |
| - "TipoConsulta": "relaxation", |
26 |
| - "StartRow": 1, |
27 |
| - "EndRow": 10, |
| 22 | + "pagina": "/app/endereco/index.php", |
| 23 | + "cepaux": "", |
| 24 | + "mensagem_alerta": "", |
| 25 | + "endereco": cep, |
| 26 | + "tipoCEP": "ALL", |
28 | 27 | }, timeout=10)
|
29 | 28 | try:
|
30 | 29 | response.raise_for_status()
|
31 | 30 | except requests.exceptions.HTTPError as ex:
|
32 | 31 | logger.exception('Erro no site dos Correios')
|
33 | 32 | raise ex
|
34 |
| - return response.text |
35 |
| - |
36 |
| - def _get_infos_(self, cep): |
37 |
| - response = self._request(cep) |
38 |
| - html = fromstring(response) |
39 |
| - registros = html.cssselect('.tmptabela tr') |
40 |
| - |
41 |
| - if not registros: |
42 |
| - return None, [] |
43 |
| - |
44 |
| - header = [h.text.strip(':') for h in registros[0].cssselect('th')] |
45 |
| - registros = registros[1:] |
46 |
| - resultado = [] |
47 |
| - for item in registros: |
48 |
| - td = item.cssselect('td') |
49 |
| - line = [] |
50 |
| - for a in td: |
51 |
| - link = a.cssselect('a') |
52 |
| - if link: |
53 |
| - text = link[0].text |
54 |
| - else: |
55 |
| - text = a.text |
56 |
| - line.append(text) |
57 |
| - resultado.append(line) |
58 |
| - return header, resultado |
| 33 | + return response.json() |
59 | 34 |
|
60 | 35 | def track(self, cep):
|
61 |
| - header, resultado = self._get_infos_(cep) |
| 36 | + data = self._request(cep) |
62 | 37 | result = []
|
63 | 38 |
|
64 | 39 | found = False
|
65 | 40 | now = datetime.now()
|
66 | 41 |
|
67 |
| - for item in resultado: |
| 42 | + for item in data["dados"]: |
| 43 | + if item['cep'] == cep: |
| 44 | + found = True |
| 45 | + |
68 | 46 | data = {
|
69 | 47 | "_meta": {
|
70 | 48 | "v_date": now,
|
71 |
| - } |
| 49 | + }, |
| 50 | + "cep": item['cep'], |
| 51 | + "bairro": item['bairro'], |
| 52 | + "cidade": item['localidade'], |
| 53 | + "estado": item['uf'], |
72 | 54 | }
|
73 |
| - |
74 |
| - for label, value in zip(header, item): |
75 |
| - |
76 |
| - label = label.lower().strip() |
77 |
| - value = re.sub(r'\s+', ' ', value.strip()) |
78 |
| - |
79 |
| - if 'localidade' in label: |
80 |
| - cidade, estado = value.split('/', 1) |
81 |
| - data['cidade'] = cidade.strip() |
82 |
| - data['estado'] = estado.split('-')[0].strip() |
83 |
| - elif 'logradouro' in label: |
84 |
| - if ' - ' in value: |
85 |
| - logradouro, complemento = value.split(' - ', 1) |
86 |
| - data['complemento'] = complemento.strip(' -') |
87 |
| - else: |
88 |
| - logradouro = value |
89 |
| - logradouro = logradouro.strip() |
90 |
| - if logradouro: |
91 |
| - data['logradouro'] = logradouro |
92 |
| - elif label == u'endereço': |
93 |
| - # Use sempre a key `endereco`. O `endereço` existe para não |
94 |
| - # quebrar clientes existentes. #92 |
95 |
| - data['endereco'] = data[label] = value |
96 |
| - elif 'bairro' in label: |
97 |
| - data['bairro'] = value |
98 |
| - elif 'cep' in label: |
99 |
| - _cep = value.replace('-', '') |
100 |
| - if _cep == cep: |
101 |
| - found = True |
102 |
| - data['cep'] = _cep |
103 |
| - else: |
104 |
| - data[label] = value |
| 55 | + logradouro = item["logradouroDNEC"] |
| 56 | + if ' - ' in logradouro: |
| 57 | + logradouro, complemento = logradouro.split(' - ', 1) |
| 58 | + data['complemento'] = complemento.strip(' -') |
| 59 | + data['logradouro'] = logradouro |
105 | 60 |
|
106 | 61 | result.append(data)
|
107 | 62 |
|
|
0 commit comments