forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
109 lines (100 loc) · 3.04 KB
/
util.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
__all__ = ['parse_login', 'parse_colors', 'parse_logging', 'parse_size', 'create_client', 'output_tasks', 'usage']
from lixian_cli_parser import *
from lixian_config import get_config
from lixian_config import LIXIAN_DEFAULT_COOKIES
from lixian_encoding import default_encoding, to_native
from lixian_colors import colors
from getpass import getpass
import lixian_help
@command_line_value('username', default=get_config('username'))
@command_line_value('password', default=get_config('password'))
@command_line_value('cookies', default=LIXIAN_DEFAULT_COOKIES)
def parse_login(args):
if args.password == '-':
args.password = getpass('Password: ')
if args.cookies == '-':
args._args['cookies'] = None
return args
@command_line_option('colors', default=get_config('colors', True))
def parse_colors(args):
pass
@command_line_value('log-level', default=get_config('log-level'))
@command_line_value('log-path', default=get_config('log-path'))
@command_line_option('debug')
@command_line_option('trace')
def parse_logging(args):
path = args.log_path
level = args.log_level
if args.trace:
level = 'trace'
elif args.debug:
level = 'debug'
if path or level:
import lixian_logging
level = level or 'info'
lixian_logging.init_logger(use_colors=args.colors, level=level, path=path)
logger = lixian_logging.get_logger()
import lixian
# inject logger to lixian (this makes lixian.py zero-dependency)
lixian.logger = logger
@command_line_option('size', default=get_config('size'))
@command_line_option('format-size', default=get_config('format-size'))
def parse_size(args):
pass
def create_client(args):
from lixian import XunleiClient
client = XunleiClient(args.username, args.password, args.cookies)
if args.page_size:
client.page_size = int(args.page_size)
return client
def output_tasks(tasks, columns, args, top=True):
for i, t in enumerate(tasks):
status_colors = {
'waiting': 'yellow',
'downloading': 'magenta',
'completed':'green',
'pending':'cyan',
'failed':'red',
}
c = status_colors[t['status_text']]
with colors(args.colors).ansi(c)():
for k in columns:
if k == 'n':
if top:
print '#%d' % t['#'],
elif k == 'id':
print t.get('index', t['id']),
elif k == 'name':
print t['name'].encode(default_encoding),
elif k == 'status':
with colors(args.colors).bold():
print t['status_text'],
elif k == 'size':
if args.format_size:
from lixian_util import format_size
print format_size(t['size']),
else:
print t['size'],
elif k == 'progress':
print t['progress'],
elif k == 'speed':
print t['speed'],
elif k == 'date':
print t['date'],
elif k == 'dcid':
print t['dcid'],
elif k == 'gcid':
print t['gcid'],
elif k == 'original-url':
print t['original_url'],
elif k == 'download-url':
print t['xunlei_url'],
else:
raise NotImplementedError(k)
print
def usage(doc=lixian_help.usage, message=None):
if hasattr(doc, '__call__'):
doc = doc()
if message:
print to_native(message)
print to_native(doc).strip()