-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathPackTracker.py
73 lines (56 loc) · 1.76 KB
/
PackTracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# coding: utf-8
import json
import os
import packtrack
import requests
from database import MongoDb as Database
def correios(track, backend=None, auth=None):
if backend is None:
backend = os.getenv('ECT_BACKEND')
encomenda = packtrack.Correios.track(track, backend=backend, auth=auth)
if not encomenda:
raise ValueError(u"Encomenda nao encontrada.")
if not encomenda.status:
raise ValueError(u"A encomenda ainda nao tem historico.")
result = []
for status in encomenda.status:
historico = {
'data': status.data,
'local': status.local,
'situacao': status.situacao,
'detalhes': status.detalhes,
}
result.append(historico)
return result
def register(provider, track, callback):
"""
Registra o pacote para acompanhamento.
"""
db = Database()
return db.packtrack.register(provider, track, callback)
def run(provider, track):
db = Database()
obj = db.packtrack.get_one(provider, track)
if provider != 'ect':
raise ValueError(u"Unexpected provider: %s" % provider)
try:
data = correios(track)
except ValueError:
return False
changed = obj.get('historico') != data
db.packtrack.update(provider, track, data, changed=changed)
return changed
def report(provider, track):
db = Database()
obj = db.packtrack.get_one(provider, track)
_meta = obj.pop('_meta')
callbacks = _meta.pop('callbacks')
for callback in callbacks:
data = obj.copy()
url = callback['callback']
data['input'] = callback
headers = {'Content-Type': 'application/json'}
requests.post(
url,
headers=headers,
data=json.dumps(data))