forked from qd-today/qd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qiandao.py
75 lines (64 loc) · 1.82 KB
/
qiandao.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<[email protected]>
# http://binux.me
# Created on 2014-08-18 12:17:21
import functools
import json
import sys
from tornado.ioloop import IOLoop
from libs.fetcher import Fetcher
from libs.log import Log
logger_QD = Log('qiandao').getlogger()
def usage():
print("{} tpl.har [--key=value] [env.json]".format(sys.argv[0]))
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) < 3:
usage()
# load tpl
tpl_file = sys.argv[1]
try:
tpl = json.load(open(tpl_file,encoding='utf-8'))
except Exception as e:
logger_QD.error(e)
usage()
# load env
variables = {}
env = {}
env_file = None
for each in sys.argv[2:]:
if each.startswith('--'):
key, value = each.split('=', 1)
key = key.lstrip('--')
variables[key] = value
else:
env_file = each
if env_file:
try:
env = json.load(open(env_file,encoding='utf-8'))
except Exception as e:
logger_QD.error(e)
usage()
if 'variables' not in env or not isinstance(env['variables'], dict)\
or 'session' not in env:
env = {
'variables': env,
'session': [],
}
env['variables'].update(variables)
# do fetch
ioloop = IOLoop.instance()
def ioloop_stop(x):
ioloop.stop()
fetcher = Fetcher()
result = fetcher.do_fetch(tpl, env)
ioloop.add_future(result, ioloop_stop)
ioloop.start()
try:
result = result.result()
except Exception as e:
print('qiandao failed!', e)
else:
print('qiandao success!', result.get('variables', {}).get('__log__', '').replace('\\r\\n','\r\n'))