-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaftership.py
115 lines (89 loc) · 3.18 KB
/
aftership.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
## read plugin api:
## https://kupferlauncher.github.io/Documentation/PluginAPI.html
__kupfer_name__ = _('AfterShip')
__version__ = '0.1.0'
__author__ = 'Hugo Sena Ribeiro <[email protected]>'
__description__ = '''Uses AfterShip for package tracking'''
__kupfer_actions__ = ('PackageStatus', 'ViewPage')
__kupfer_sources__ = ('Curriers',)
from json import loads
from urllib import request, error
from kupfer.plugin_support import PluginSettings
from kupfer.objects import Action, Source, TextLeaf
from kupfer.utils import show_url
__kupfer_settings__ = PluginSettings(
{
'key' : 'api_key',
'label': _('AfterShip Key'),
'type': str,
'value': '92139263-a1d7-40a4-847b-d82f536b51ea',
},
)
CURRIERS_ENDPOINT = 'https://api.aftership.com/v4/couriers/all'
ENDPOINT = 'https://api.aftership.com/v4/last_checkpoint/{}/{}'
LINK = 'https://track.aftership.com/{}/{}'
def api_request(url):
req = request.Request(
url,
headers={
'Content-Type': 'application/json',
'aftership-api-key': __kupfer_settings__['api_key']
}
)
with request.urlopen(req) as service:
return loads(service.read())
class Currier(TextLeaf):
def __init__(self, obj):
TextLeaf.__init__(self, obj['slug'], obj['name'])
class PackageStatusLeaf(TextLeaf):
def __init__(self, obj, code, currier):
self.obj = obj
self.code = code
self.currier = currier
txt = self.obj.get('meta', {}).get('message', '')
checkpoint = self.obj.get('data', {}).get('checkpoint', {})
if checkpoint:
if checkpoint.get('message'):
fmt = '{message} {country_name} {checkpoint_time}'
txt = fmt.format(**checkpoint)
else:
txt = 'Empty status info'
TextLeaf.__init__(self, txt)
class Curriers(Source):
def __init__(self):
Source.__init__(self, 'AfterShip Curriers')
def produces(self):
yield Currier
def get_items(self):
json = api_request(CURRIERS_ENDPOINT)
curriers = json.get('data', {}).get('couriers', ())
for currier in curriers:
if not currier['required_fields']:
yield Currier(currier)
class PackageStatus(Action):
def __init__(self):
Action.__init__(self, name=_('Package Status'))
def activate(self, leaf, currier_leaf):
url = ENDPOINT.format(currier_leaf.object, leaf.object)
json = {}
try:
json = api_request(url)
except error.HTTPError as err:
json = {'meta':{'message': err.reason, 'code': err.code}}
return PackageStatusLeaf(json, leaf.object, currier_leaf.object)
def has_result(self):
return True
def item_types(self):
yield TextLeaf
def requires_object(self):
return True
def object_types(self, for_item=None):
yield Currier
class ViewPage(Action):
def __init__(self):
Action.__init__(self, name=_('View at AfterShip'))
def activate(self, leaf):
url = LINK.format(leaf.currier, leaf.code)
show_url(url)
def item_types(self):
yield PackageStatusLeaf