Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
postmon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Matheus Horstmann
postmon
Commits
318fd259
Commit
318fd259
authored
12 years ago
by
Alê Borba
Browse files
Options
Downloads
Plain Diff
Merge pull request #22 from iurisilvio/issue_20
Bugfix
parents
3849982d
a999f7f6
No related branches found
Branches containing commit
Tags
v1.3.1
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CepTracker.py
+25
-25
25 additions, 25 deletions
CepTracker.py
test/postmon_test.py
+33
-1
33 additions, 1 deletion
test/postmon_test.py
with
58 additions
and
26 deletions
CepTracker.py
+
25
−
25
View file @
318fd259
...
...
@@ -5,48 +5,48 @@ import requests
import
re
class
CepTracker
():
def
__init__
(
self
):
self
.
url
=
'
http://m.correios.com.br/movel/buscaCepConfirma.do
'
self
.
result
=
[]
self
.
fields
=
[
'
logradouro
'
,
'
bairro
'
,
[
'
cidade
'
,
'
estado
'
],
'
cep
'
]
def
_get_infos_
(
self
,
cep
,
csspattern
=
'
div .respostadestaque
'
):
def
_get_infos_
(
self
,
cep
):
from
lxml.html
import
fromstring
response
=
requests
.
post
(
self
.
url
,
data
=
{
'
cepEntrada
'
:
cep
,
'
tipoCep
'
:
''
,
'
cepTemp
'
:
''
,
'
metodo
'
:
'
buscarCep
'
})
html
=
fromstring
(
response
.
text
)
return
html
.
cssselect
(
csspattern
)
registro_csspattern
=
'
.caixacampobranco, .caixacampoazul
'
registros
=
html
.
cssselect
(
registro_csspattern
)
resultado
=
[]
for
item
in
registros
:
item_csspattern
=
'
.resposta, .respostadestaque
'
resultado
.
append
([
a
.
text
for
a
in
item
.
cssselect
(
item_csspattern
)])
return
resultado
def
track
(
self
,
cep
):
itens
=
self
.
_get_infos_
(
cep
)
result
=
[]
index
=
4
-
len
(
itens
)
data
=
dict
()
for
item
in
itens
:
if
index
%
4
==
0
:
if
index
>
0
:
index
=
0
self
.
result
.
append
(
data
)
data
=
dict
()
# TODO: definir v_date apenas uma vez
data
=
dict
()
data
[
"
v_date
"
]
=
datetime
.
now
()
text
=
re
.
sub
(
'
\s+
'
,
'
'
,
item
.
text
.
strip
())
for
label
,
value
in
zip
(
item
[
0
::
2
],
item
[
1
::
2
]):
if
(
index
==
2
):
cidade
,
estado
=
text
.
split
(
'
/
'
,
1
)
data
[
'
cidade
'
]
=
cidade
.
strip
()
data
[
'
estado
'
]
=
estado
.
split
(
'
-
'
)[
0
].
strip
()
else
:
data
[
self
.
fields
[
index
]]
=
text
label
=
label
.
lower
().
strip
(
'
:
'
)
value
=
re
.
sub
(
'
\s+
'
,
'
'
,
value
.
strip
())
index
+=
1
if
'
localidade
'
in
label
:
cidade
,
estado
=
value
.
split
(
'
/
'
,
1
)
data
[
'
cidade
'
]
=
cidade
.
strip
()
data
[
'
estado
'
]
=
estado
.
split
(
'
-
'
)[
0
].
strip
()
else
:
data
[
label
]
=
value
if
len
(
data
)
>
0
:
self
.
result
.
append
(
data
)
result
.
append
(
data
)
return
self
.
result
return
result
This diff is collapsed.
Click to expand it.
test/postmon_test.py
+
33
−
1
View file @
318fd259
...
...
@@ -52,4 +52,36 @@ class PostmonTest(unittest.TestCase):
self
.
assertTrue
(
len
(
result
)
==
0
)
# TODO: existe CEP com mais de um resultado?
def
test_cep_com_mais_de_um_resultado
(
self
):
'''
A busca pelo CEP 75064590 retorna dois resultados
Logradouro: Rua A
Bairro: Vila Jaiara
Localidade / UF: Anápolis /GO
CEP: 75064590
Logradouro: Rua A
Bairro: Vila Jaiara Setor Leste
Localidade / UF: Anápolis /GO
CEP: 75064379
'''
tracker
=
CepTracker
.
CepTracker
()
result
=
tracker
.
track
(
'
75064590
'
)
self
.
assertTrue
(
len
(
result
)
==
2
)
self
.
assertEqual
(
result
[
0
][
'
cep
'
],
'
75064590
'
)
self
.
assertEqual
(
result
[
0
][
'
logradouro
'
],
'
Rua A
'
)
self
.
assertEqual
(
result
[
0
][
'
bairro
'
],
'
Vila Jaiara
'
)
self
.
assertEqual
(
result
[
0
][
'
cidade
'
],
u
'
Anápolis
'
)
self
.
assertEqual
(
result
[
0
][
'
estado
'
],
'
GO
'
)
self
.
assertIsNotNone
(
result
[
0
][
'
v_date
'
])
self
.
assertEqual
(
result
[
1
][
'
cep
'
],
'
75064379
'
)
self
.
assertEqual
(
result
[
1
][
'
logradouro
'
],
'
Rua A
'
)
self
.
assertEqual
(
result
[
1
][
'
bairro
'
],
'
Vila Jaiara Setor Leste
'
)
self
.
assertEqual
(
result
[
1
][
'
cidade
'
],
u
'
Anápolis
'
)
self
.
assertEqual
(
result
[
1
][
'
estado
'
],
'
GO
'
)
self
.
assertIsNotNone
(
result
[
1
][
'
v_date
'
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment