|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
1 | 3 | import bottle |
2 | 4 | import json |
3 | 5 | from bottle import route, run, response |
|
6 | 8 | from correios import Correios |
7 | 9 | from database import MongoDb as Database |
8 | 10 |
|
| 11 | + |
9 | 12 | app_v1 = bottle.Bottle() |
10 | 13 | jsonp_query_key = 'callback' |
11 | 14 |
|
| 15 | + |
12 | 16 | def expired(record_date): |
13 | 17 | from datetime import datetime, timedelta |
14 | 18 |
|
15 | | - WEEKS = 26 #6 months |
| 19 | + # 6 months |
| 20 | + WEEKS = 26 |
16 | 21 |
|
17 | 22 | now = datetime.now() |
18 | 23 |
|
19 | | - return ( now - record_date['v_date'] >= timedelta(weeks=WEEKS)) |
| 24 | + return (now - record_date['v_date'] >= timedelta(weeks=WEEKS)) |
20 | 25 |
|
21 | 26 |
|
22 | 27 | def _get_info_from_source(cep): |
@@ -44,32 +49,34 @@ def format_result(result): |
44 | 49 | @route('/cep/<cep:re:\d{5}-?\d{3}>') |
45 | 50 | @app_v1.route('/cep/<cep:re:\d{5}-?\d{3}>') |
46 | 51 | def verifica_cep(cep): |
47 | | - cep = cep.replace('-','') |
| 52 | + cep = cep.replace('-', '') |
48 | 53 | db = Database() |
49 | 54 | response.headers['Access-Control-Allow-Origin'] = '*' |
50 | 55 |
|
51 | | - result = db.get_one(cep, fields={ '_id': False }) |
52 | | - if result and result.has_key('v_date') and not expired(result): |
| 56 | + result = db.get_one(cep, fields={'_id': False}) |
| 57 | + if result and 'v_date' in result and not expired(result): |
53 | 58 | result.pop('v_date') |
54 | 59 | else: |
55 | 60 | try: |
56 | 61 | info = _get_info_from_source(cep) |
57 | 62 | except ValueError: |
58 | | - response.status = '404 O CEP %s informado nao pode ser localizado' % cep |
| 63 | + response.status = '404 O CEP %s informado nao pode ' |
| 64 | + 'ser localizado' % cep |
59 | 65 | return |
60 | 66 | except requests.exceptions.RequestException: |
61 | 67 | response.status = '503 Servico Temporariamente Indisponivel' |
62 | 68 | return |
63 | 69 | for item in info: |
64 | 70 | db.insert_or_update(item) |
65 | | - result = db.get_one(cep, fields={ '_id': False, 'v_date': False }) |
| 71 | + result = db.get_one(cep, fields={'_id': False, 'v_date': False}) |
66 | 72 |
|
67 | 73 | if result: |
68 | 74 |
|
69 | 75 | response.headers['Cache-Control'] = 'public, max-age=2592000' |
70 | 76 | return format_result(result) |
71 | 77 | else: |
72 | | - response.status = '404 O CEP %s informado nao pode ser localizado' % cep |
| 78 | + response.status = '404 O CEP %s informado nao pode ser ' |
| 79 | + 'localizado' % cep |
73 | 80 | return |
74 | 81 |
|
75 | 82 |
|
@@ -98,11 +105,14 @@ def track_pack(provider, track): |
98 | 105 | return format_result(resposta) |
99 | 106 |
|
100 | 107 | except AttributeError: |
101 | | - response.status = '404 O pacote %s informado nao pode ser localizado' %track |
| 108 | + response.status = '404 O pacote %s informado nao pode ser ' |
| 109 | + 'localizado' % track |
102 | 110 | else: |
103 | | - response.status = '404 O Servico %s nao pode ser encontrado' %provider |
| 111 | + response.status = '404 O Servico %s nao pode ser encontrado' % provider |
| 112 | + |
104 | 113 |
|
105 | 114 | bottle.mount('/v1', app_v1) |
106 | 115 |
|
| 116 | + |
107 | 117 | def _standalone(port=9876): |
108 | 118 | run(host='localhost', port=port) |
0 commit comments