1- from bottle import route , run , error , response
1+ from bottle import route , run , response
22from CepTracker import CepTracker
33from requests import ConnectionError
44
5- import pymongo , json , re
5+ from database import MongoDb as Database
66
77def expired (record_date ):
88 from datetime import datetime , timedelta
@@ -23,27 +23,26 @@ def _get_info_from_correios(cep):
2323
2424 return info
2525
26+
2627@route ('/cep/<cep:re:\d{5}-?\d{3}>' )
2728def verifica_cep (cep ):
2829 cep = cep .replace ('-' ,'' )
30+ db = Database ()
2931
3032 response .headers ['Access-Control-Allow-Origin' ] = '*'
31-
33+
3234 try :
33- con = pymongo .MongoClient ('localhost' )
34- db = con .postmon
35- ceps = db .ceps
36- result = ceps .find_one ({'cep' :cep }, fields = {'_id' :False })
35+ result = db .get_one (cep , fields = { '_id' : False })
3736
3837 if not result or not result .has_key ('v_date' ) or expired (result ):
3938 try :
4039 for item in _get_info_from_correios (cep ):
41- ceps . update ({ 'cep' : item [ 'cep' ]}, { '$set' : item }, upsert = True )
40+ db . insert_or_update ( item )
4241
4342 except ConnectionError :
4443 response .status = '503 Servico Temporariamente Indisponivel'
4544
46- result = ceps . find_one ({ ' cep' : cep } , fields = {'_id' :False ,'v_date' :False })
45+ result = db . get_one ( cep , fields = { '_id' : False , 'v_date' : False })
4746
4847 response .headers ['Cache-Control' ] = 'public, max-age=2592000'
4948
0 commit comments