This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.py
99 lines (73 loc) · 2.49 KB
/
process.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
__author__ = 'zephyre'
from datetime import datetime
import alfred
from bson import ObjectId
from bson.errors import InvalidId
def process(query_str):
""" Entry point """
query_str = query_str.strip().lower() if query_str else ''
results = []
if query_str == 'gen':
ret = gen_objectid_result()
results.append(ret)
oid = ret.attributes['arg']
results.extend(get_generation_time(oid))
else:
oid = parse_query_value(query_str)
if oid:
results.extend(get_generation_time(oid))
if results:
xml = alfred.xml(results) # compiles the XML answer
alfred.write(xml) # writes the XML back to Alfred
def get_generation_time(oid):
"""
Get the alfred result for the ObjectId generation time
:param oid:
:return:
"""
import pytz
from tzlocal import get_localzone
gt = oid.generation_time.replace(tzinfo=pytz.UTC)
t0 = pytz.UTC.localize(datetime.utcfromtimestamp(0))
tz_cn = pytz.timezone('Asia/Shanghai')
t1 = tz_cn.normalize(gt)
tz_local = get_localzone()
t2 = tz_local.normalize(gt)
total_seconds = int((gt - t0).total_seconds())
return [
alfred.Item(title=str(gt), subtitle='Generation time in UTC', icon='', attributes={'arg': gt}),
alfred.Item(title=str(t1), subtitle='Generation time in Asia/Shanghai', icon='', attributes={'arg': t1}),
alfred.Item(title=str(t2), subtitle='Generation time in local timezone: %s' % tz_local.zone, icon='',
attributes={'arg': t1}),
alfred.Item(title=str(total_seconds), subtitle='Unix Epoch Timestamp', icon='',
attributes={'arg': total_seconds})
]
def gen_objectid_result():
oid = ObjectId()
return alfred.Item(title=str(oid), subtitle='Generated ObjectId', icon='', attributes={'arg': oid})
def parse_query_value(query_str):
"""
Parse the query string. If the input is invalid or doesn't exist, None will be returned
:param query_str:
:return:
"""
try:
oid = ObjectId(query_str)
except InvalidId:
oid = None
return oid
# def alfred_items_for_value(oid):
# index = 0
# alfred_items = [
# alfred.Item(title=str(oid), subtitle='Original ObjectId', attributes={
# 'uid': alfred.uid(index),
# 'arg': oid,
# }, icon='icon.png')
# ]
# return alfred_items
if __name__ == '__main__':
try:
arg = alfred.args()[0]
except IndexError:
arg = None
process(arg)