forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_cli.py
executable file
·739 lines (687 loc) · 25.5 KB
/
lixian_cli.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/usr/bin/env python
from lixian import XunleiClient, encypt_password
from lixian_cli_parser import parse_command_line
from lixian_config import *
import lixian_help
import lixian_hash
import lixian_hash_bt
import lixian_hash_ed2k
import subprocess
import sys
import os
import os.path
import re
import urllib2
from getpass import getpass
from lixian_encoding import default_encoding
def from_native(s):
return s.decode(default_encoding)
def to_utf_8(url):
try:
return url.decode(default_encoding).encode('utf-8')
except:
return url
def to_str(s):
assert type(s) in (str, unicode)
return s.encode(default_encoding) if type(s) == unicode else s
def parse_login_command_line(args, keys=[], bools=[], alias={}, default={}, help=None):
common_keys = ['username', 'password', 'cookies']
common_default = {'cookies': LIXIAN_DEFAULT_COOKIES, 'username': get_config('username'), 'password': get_config('password')}
common_keys.extend(keys)
common_default.update(default)
args = parse_command_line(args, common_keys, bools, alias, common_default, help=help)
if args.password == '-':
args.password = getpass('Password: ')
if args.cookies == '-':
args._args['cookies'] = None
return args
def login(args):
args = parse_login_command_line(args, help=lixian_help.login)
if args.cookies == '-':
args._args['cookies'] = None
if len(args) < 1:
args.username = args.username or XunleiClient(cookie_path=args.cookies, login=False).get_username() or get_config('username') or raw_input('ID: ')
args.password = args.password or get_config('password') or getpass('Password: ')
elif len(args) == 1:
args.username = args.username or XunleiClient(cookie_path=args.cookies, login=False).get_username() or get_config('username')
args.password = args[0]
if args.password == '-':
args.password = getpass('Password: ')
elif len(args) == 2:
args.username, args.password = list(args)
if args.password == '-':
args.password = getpass('Password: ')
elif len(args) == 3:
args.username, args.password, args.cookies = list(args)
if args.password == '-':
args.password = getpass('Password: ')
elif len(args) > 3:
raise RuntimeError('Too many arguments')
if not args.username:
raise RuntimeError("What's your name?")
if args.cookies:
print 'Saving login session to', args.cookies
else:
print 'Testing login without saving session'
client = XunleiClient(args.username, args.password, args.cookies)
def logout(args):
args = parse_command_line(args, ['cookies'], default={'cookies': LIXIAN_DEFAULT_COOKIES}, help=lixian_help.logout)
if len(args):
raise RuntimeError('Too many arguments')
print 'logging out from', args.cookies
assert args.cookies
client = XunleiClient(cookie_path=args.cookies, login=False)
client.logout()
def urllib2_download(client, download_url, filename, resuming=False):
'''In the case you don't even have wget...'''
assert not resuming
print 'Downloading', download_url, 'to', filename, '...'
request = urllib2.Request(download_url, headers={'Cookie': 'gdriveid='+client.get_gdriveid()})
response = urllib2.urlopen(request)
import shutil
with open(filename, 'wb') as output:
shutil.copyfileobj(response, output)
def asyn_download(client, download_url, filename, resuming=False):
import lixian_download
lixian_download.download(download_url, filename, headers={'Cookie': 'gdriveid='+str(client.get_gdriveid())}, resuming=resuming)
def wget_download(client, download_url, filename, resuming=False):
gdriveid = str(client.get_gdriveid())
wget_opts = ['wget', '--header=Cookie: gdriveid='+gdriveid, download_url, '-O', filename]
if resuming:
wget_opts.append('-c')
wget_opts.extend(get_config('wget-opts', '').split())
exit_code = subprocess.call(wget_opts)
if exit_code != 0:
raise Exception('wget exited abnormaly')
def curl_download(client, download_url, filename, resuming=False):
gdriveid = str(client.get_gdriveid())
curl_opts = ['curl', '-L', download_url, '--cookie', 'gdriveid='+gdriveid, '--output', filename]
if resuming:
curl_opts.append('--continue')
exit_code = subprocess.call(curl_opts)
if exit_code != 0:
raise Exception('curl exited abnormaly')
def aria2_download(client, download_url, path, resuming=False):
gdriveid = str(client.get_gdriveid())
dir = os.path.dirname(path)
filename = os.path.basename(path)
aria2_opts = ['aria2c', '--header=Cookie: gdriveid='+gdriveid, download_url, '--out', filename, '--file-allocation=none']
if dir:
aria2_opts.extend(('--dir', dir))
if resuming:
aria2_opts.append('-c')
aria2_opts.extend(get_config('aria2-opts', '').split())
exit_code = subprocess.call(aria2_opts)
if exit_code != 0:
raise Exception('aria2c exited abnormaly')
def axel_download(client, download_url, path, resuming=False):
gdriveid = str(client.get_gdriveid())
axel_opts = ['axel', '--header=Cookie: gdriveid='+gdriveid, download_url, '--output', path]
axel_opts.extend(get_config('axel-opts', '').split())
exit_code = subprocess.call(axel_opts)
if exit_code != 0:
raise Exception('axel exited abnormaly')
# TODO: support axel, ProZilla
def escape_filename(name):
amp = re.compile(r'&(amp;)+', flags=re.I)
name = re.sub(amp, '&', name)
name = re.sub(r'[\\/:*?"<>|]', '-', name)
return name
def verify_basic_hash(path, task):
if os.path.getsize(path) != task['size']:
print 'hash error: incorrect file size'
return False
return lixian_hash.verify_dcid(path, task['dcid'])
def verify_hash(path, task):
if verify_basic_hash(path, task):
if task['type'] == 'ed2k':
return lixian_hash_ed2k.verify_ed2k_link(path, task['original_url'])
else:
return True
def verify_mini_hash(path, task):
return os.path.exists(path) and os.path.getsize(path) == task['size'] and lixian_hash.verify_dcid(path, task['dcid'])
def verify_mini_bt_hash(dirname, files):
for f in files:
name = f['name'].encode(default_encoding)
path = os.path.join(dirname, *name.split('\\'))
if not verify_mini_hash(path, f):
return False
return True
def download_single_task(client, download, task, options):
output = options.get('output')
output_dir = options.get('output_dir')
delete = options.get('delete')
resuming = options.get('resuming')
overwrite = options.get('overwrite')
mini_hash = options.get('mini_hash')
no_hash = options.get('no_hash')
assert client.get_gdriveid()
if task['status_text'] != 'completed':
if 'files' not in task:
print 'skip task %s as the status is %s' % (task['name'].encode(default_encoding), task['status_text'])
return
def download1(client, url, path, size):
if not os.path.exists(path):
download(client, url, path)
elif not resuming:
if overwrite:
download(client, url, path)
else:
raise Exception('%s already exists. Please try --continue or --overwrite' % path)
else:
assert os.path.getsize(path) <= size, 'existing file bigger than expected, unsafe to continue nor overwrite'
if os.path.getsize(path) < size:
download(client, url, path, resuming)
elif os.path.getsize(path) == size:
pass
else:
raise NotImplementedError()
def download2(client, url, path, task):
size = task['size']
if mini_hash and resuming and verify_mini_hash(path, task):
return
download1(client, url, path, size)
verify = verify_basic_hash if no_hash else verify_hash
if not verify(path, task):
print 'hash error, redownloading...'
os.remove(path)
download1(client, url, path, size)
if not verify(path, task):
raise Exception('hash check failed')
download_url = str(task['xunlei_url'])
if output:
filename = output
else:
filename = escape_filename(task['name']).encode(default_encoding)
if output_dir:
filename = os.path.join(output_dir, filename)
referer = str(client.get_referer())
gdriveid = str(client.get_gdriveid())
if task['type'] == 'bt':
files = client.list_bt(task)
if len(files) == 1 and files[0]['name'] == task['name']:
dirname = os.path.dirname(filename)
else:
dirname = filename
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
if 'files' in task:
ordered_files = []
indexed_files = dict((f['index'], f) for f in files)
for index in task['files']:
t = indexed_files[int(index)]
if t not in ordered_files:
if t['status_text'] != 'completed':
print 'skip task %s/%s (%s) as the status is %s' % (t['id'], index, t['name'].encode(default_encoding), t['status_text'])
else:
ordered_files.append(t)
files = ordered_files
if mini_hash and resuming and verify_mini_bt_hash(dirname, files):
print task['name'].encode(default_encoding), 'is already done'
if delete and 'files' not in task:
client.delete_task(task)
return
for f in files:
name = f['name']
print 'Downloading', name.encode(default_encoding), '...'
# XXX: if file name is escaped, hashing bt won't get correct file
path = os.path.join(dirname, *[p.encode(default_encoding) for p in map(escape_filename, name.split('\\'))])
subdir = os.path.dirname(path)
if subdir and not os.path.exists(subdir):
os.makedirs(subdir)
download_url = str(f['xunlei_url'])
download2(client, download_url, path, f)
if not no_hash:
torrent_file = client.get_torrent_file(task)
print 'Hashing bt ...'
from lixian_progress import SimpleProgressBar
bar = SimpleProgressBar()
file_set = [f['name'].encode('utf-8').split('\\') for f in files] if 'files' in task else None
verified = lixian_hash_bt.verify_bt(filename, lixian_hash_bt.bdecode(torrent_file)['info'], file_set=file_set, progress_callback=bar.update)
bar.done()
if not verified:
# note that we don't delete bt download folder if hash failed
raise Exception('bt hash check failed')
else:
dirname = os.path.dirname(filename)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
print 'Downloading', os.path.basename(filename), '...'
download2(client, download_url, filename, task)
if delete and 'files' not in task:
client.delete_task(task)
def download_multiple_tasks(client, download, tasks, options):
for task in tasks:
download_single_task(client, download, task, options)
skipped = filter(lambda t: t['status_text'] != 'completed', tasks)
if skipped:
print "Below tasks were skipped as they were not ready:"
for task in skipped:
print task['id'], task['status_text'], task['name'].encode(default_encoding)
def find_torrents_task_to_download(client, links):
tasks = client.read_all_tasks()
hashes = set(t['bt_hash'].lower() for t in tasks if t['type'] == 'bt')
link_hashes = []
for link in links:
if re.match(r'^(?:bt://)?([a-fA-F0-9]{40})$', link):
info_hash = link[-40:].lower()
if info_hash not in hashes:
print 'Adding bt task', link
client.add_torrent_task_by_info_hash(info_hash)
link_hashes.append(info_hash)
elif re.match(r'http://', link):
print 'Downloading torrent file from', link
torrent = urllib2.urlopen(link, timeout=60).read()
assert torrent.startswith('d8:announce'), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11])
info_hash = lixian_hash_bt.info_hash_from_content(torrent)
if info_hash not in hashes:
print 'Adding bt task', link
client.add_torrent_task_by_content(torrent, os.path.basename(link))
link_hashes.append(info_hash)
elif os.path.exists(link):
with open(link, 'rb') as stream:
torrent = stream.read()
assert torrent.startswith('d8:announce'), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11])
info_hash = lixian_hash_bt.info_hash_from_content(torrent)
if info_hash not in hashes:
print 'Adding bt task', link
client.add_torrent_task_by_content(torrent, os.path.basename(link))
link_hashes.append(info_hash)
else:
raise NotImplementedError('Unknown torrent '+link)
all_tasks = client.read_all_tasks()
tasks = []
for h in link_hashes:
for t in all_tasks:
if t['bt_hash'].lower() == h.lower():
tasks.append(t)
break
else:
raise NotImplementedError('not task found')
return tasks
def find_tasks_to_download(client, args):
links = []
links.extend(args)
if args.input:
with open(args.input) as x:
links.extend(line.strip() for line in x.readlines() if line.strip())
if args.torrent:
return find_torrents_task_to_download(client, links)
if args.search or any(re.match(r'^\d+(/[-\d\[\],\s]+)?$', x) for x in args):
return search_tasks(client, args, check='check_none')
all_tasks = client.read_all_tasks()
to_add = set(links)
for t in all_tasks:
for x in to_add:
if link_equals(t['original_url'], x):
to_add.remove(x)
break
if to_add:
print 'Adding below tasks:'
for link in links:
if link in to_add:
print link
client.add_batch_tasks(map(to_utf_8, to_add))
for link in to_add:
# add_batch_tasks doesn't work for bt task, add bt task one by one...
if link.startswith('bt://') or link.startswith('magnet:'):
client.add_task(link)
all_tasks = client.read_all_tasks()
tasks = []
for link in links:
for task in all_tasks:
if link_equals(link, task['original_url']):
tasks.append(task)
break
else:
raise NotImplementedError('task not found, wired: '+link)
return tasks
def merge_bt_sub_tasks(tasks):
result_tasks = []
task_mapping = {}
for task in tasks:
id = task['id']
if id in task_mapping:
if 'index' in task and 'files' in task_mapping[id]:
task_mapping[id]['files'].append(task['index'])
else:
if 'index' in task:
t = dict(task)
t['files'] = [t['index']]
del t['index']
result_tasks.append(t)
task_mapping[id] = t
else:
result_tasks.append(task)
task_mapping[id] = task
return result_tasks
def download_task(args):
args = parse_login_command_line(args,
['tool', 'output', 'output-dir', 'input'],
['delete', 'continue', 'overwrite', 'torrent', 'all', 'search', 'mini-hash', 'hash'],
alias={'o': 'output', 'i': 'input', 'c':'continue'},
default={'tool':get_config('tool', 'wget'),'delete':get_config('delete'),'continue':get_config('continue'),'output-dir':get_config('output-dir'), 'mini-hash':get_config('mini-hash'), 'hash':get_config('hash', True)},
help=lixian_help.download)
download = {'wget':wget_download, 'curl': curl_download, 'aria2':aria2_download, 'aria2c':aria2_download, 'axel':axel_download, 'asyn':asyn_download, 'urllib2':urllib2_download}[args.tool]
download_args = {'output':args.output, 'output_dir':args.output_dir, 'delete':args.delete, 'resuming':args._args['continue'], 'overwrite':args.overwrite, 'mini_hash':args.mini_hash, 'no_hash': not args.hash}
client = XunleiClient(args.username, args.password, args.cookies)
links = None
if len(args) > 1 or args.input:
assert not args.output
tasks = find_tasks_to_download(client, args)
tasks = merge_bt_sub_tasks(tasks)
download_multiple_tasks(client, download, tasks, download_args)
elif args.torrent:
assert not args.search
assert len(args) == 1
tasks = find_torrents_task_to_download(client, [args[0]])
assert len(tasks) == 1
download_single_task(client, download, tasks[0], download_args)
elif len(args):
assert len(args) == 1
tasks = search_tasks(client, args, status='all', check=False)
if not tasks:
url = args[0]
print 'Adding new task %s ...' % url
client.add_task(to_utf_8(url))
tasks = client.read_all_completed()
tasks = filter_tasks(tasks, 'original_url', url)
assert tasks, 'task not found, wired'
tasks = merge_bt_sub_tasks(tasks)
if args.output:
assert len(tasks) == 1
download_single_task(client, download, tasks[0], download_args)
else:
download_multiple_tasks(client, download, tasks, download_args)
elif args.all:
#tasks = client.read_all_completed()
tasks = client.read_all_tasks()
download_multiple_tasks(client, download, tasks, download_args)
else:
usage(doc=lixian_help.download, message='Not enough arguments')
def link_equals(x1, x2):
from lixian_url import url_unmask, normalize_unicode_link
x1 = url_unmask(x1)
x2 = url_unmask(x2)
if x1.startswith('magnet:'):
x1 = 'bt://'+lixian_hash_bt.magnet_to_infohash(x1).encode('hex')
if x2.startswith('magnet:'):
x2 = 'bt://'+lixian_hash_bt.magnet_to_infohash(x2).encode('hex')
if x1.startswith('ed2k://') and x2.startswith('ed2k://'):
return lixian_hash_ed2k.parse_ed2k_link(x1) == lixian_hash_ed2k.parse_ed2k_link(x2)
#import urllib
#if type(x1) == unicode:
# x1 = x1.encode('utf-8')
#if type(x2) == unicode:
# x2 = x2.encode('utf-8')
#x1 = urllib.unquote(x1)
#x2 = urllib.unquote(x2)
#x1 = x1.replace('&', '&')
#x2 = x2.replace('&', '&')
elif x1.startswith('bt://') and x2.startswith('bt://'):
x1 = x1.lower()
x2 = x2.lower()
elif x1.startswith('http://') and x2.startswith('http://'):
x1 = normalize_unicode_link(x1)
x2 = normalize_unicode_link(x2)
return x1 == x2
def link_in(url, links):
for link in links:
if link_equals(url, link):
return True
def filter_tasks(tasks, k, v):
if k == 'id':
task_id, sub_id = re.match(r'^(\d+)(?:/([-\d\[\],\s]+))?$', v).groups()
matched = filter(lambda t: t['id'] == task_id, tasks)
if matched:
assert len(matched) == 1
task = matched[0]
if sub_id:
assert task['type'] == 'bt', 'task %s is not a bt task' % task['name'].encode(default_encoding)
matched = []
if re.match(r'\[.*\]', sub_id):
for sub_id in re.split(r'\s*,\s*', sub_id[1:-1]):
assert re.match(r'^\d+(-\d+)?$', sub_id), sub_id
if '-' in sub_id:
start, end = sub_id.split('-')
for i in range(int(start), int(end)+1):
t = dict(task)
t['index'] = str(i)
matched.append(t)
else:
assert re.match(r'^\d+$', sub_id), sub_id
t = dict(task)
t['index'] = sub_id
matched.append(t)
else:
assert re.match(r'^\d+$', sub_id), sub_id
t = dict(task)
t['index'] = sub_id
matched.append(t)
else:
matched = [task]
elif k == 'name':
matched = filter(lambda t: t[k].find(v) != -1, tasks)
elif k == 'original_url':
matched = filter(lambda t: link_equals(t[k], v), tasks)
else:
matched = filter(lambda t: t[k] == v, tasks)
return matched
def search_tasks(client, args, status='all', check=True):
if status == 'all':
tasks = client.read_all_tasks()
elif status == 'completed':
tasks = client.read_all_tasks()
else:
raise NotImplementedError()
found = []
for x in args:
if args.search:
matched = filter_tasks(tasks, 'name', x)
else:
if re.match(r'^\d+(/[-\d\[\],\s]+)?$', x):
matched = filter_tasks(tasks, 'id', x)
elif re.match(r'\w+://', x) or x.startswith('magnet:'):
matched = filter_tasks(tasks, 'original_url', x)
else:
matched = filter_tasks(tasks, 'name', x)
if check:
if not matched:
raise RuntimeError('Not task found for '+x)
if check != 'check_none' and (not args.all) and len(matched) > 1:
raise RuntimeError('Too many tasks found for '+x)
found.extend(matched)
return found
def list_task(args):
args = parse_login_command_line(args, [],
['all', 'completed',
'id', 'name', 'status', 'size', 'dcid', 'original-url', 'download-url', 'speed', 'progress',
'search'],
default={'id': True, 'name': True, 'status': True},
help=lixian_help.list)
parent_ids = [a[:-1] for a in args if re.match(r'^\d+/$', a)]
if parent_ids and not all(re.match(r'^\d+/$', a) for a in args):
raise NotImplementedError("Can't mix 'id/' with others")
assert len(parent_ids) <= 1, "sub-tasks listing only supports single task id"
ids = [a[:-1] if re.match(r'^\d+/$', a) else a for a in args]
client = XunleiClient(args.username, args.password, args.cookies)
if parent_ids:
tasks = client.list_bt(client.get_task_by_id(parent_ids[0]))
tasks.sort(key=lambda x: int(x['index']))
elif len(ids):
tasks = search_tasks(client, args, status=(args.completed and 'completed' or 'all'), check=False)
elif args.completed:
tasks = client.read_all_completed()
else:
tasks = client.read_all_tasks()
columns = ['id', 'name', 'status', 'size', 'progress', 'speed', 'dcid', 'original-url', 'download-url']
columns = filter(lambda k: getattr(args, k), columns)
for t in tasks:
for k in columns:
if k == 'id':
print t.get('index', t['id']),
elif k == 'name':
print t['name'].encode(default_encoding),
elif k == 'status':
print t['status_text'],
elif k == 'size':
print t['size'],
elif k == 'progress':
print t['progress'],
elif k == 'speed':
print t['speed'],
elif k == 'dcid':
print t['dcid'],
elif k == 'original-url':
print t['original_url'],
elif k == 'download-url':
print t['xunlei_url'],
else:
raise NotImplementedError()
print
def add_task(args):
args = parse_login_command_line(args, ['input'], ['torrent'], alias={'i':'input'}, help=lixian_help.add)
assert len(args) or args.input
client = XunleiClient(args.username, args.password, args.cookies)
links = []
links.extend(args)
if args.input:
with open(args.input) as x:
links.extend(line.strip() for line in x.readlines() if line.strip())
if not args.torrent:
print 'Adding below tasks:'
for link in links:
print link
client.add_batch_tasks(map(to_utf_8, links))
for link in links:
# add_batch_tasks doesn't work for bt task, add bt task one by one...
if link.startswith('bt://') or link.startswith('magnet:'):
client.add_task(link)
print 'All tasks added. Checking status...'
tasks = client.read_all_tasks()
for link in links:
found = filter_tasks(tasks, 'original_url', link)
if found:
print found[0]['status_text'], link
else:
print 'unknown', link
else:
tasks = find_torrents_task_to_download(client, links)
assert len(tasks) == len(links)
print 'All tasks added:'
for link, task in zip(links, tasks):
print task['status_text'], link
def delete_task(args):
args = parse_login_command_line(args, [], ['search', 'i', 'all'], help=lixian_help.delete)
client = XunleiClient(args.username, args.password, args.cookies)
to_delete = search_tasks(client, args)
print "Below files are going to be deleted:"
for x in to_delete:
print x['name'].encode(default_encoding)
if args.i:
yes_or_no = raw_input('Are your sure to delete below files from Xunlei cloud? ')
while yes_or_no.lower() not in ('y', 'yes', 'n', 'no'):
yes_or_no = raw_input('yes or no? ')
if yes_or_no.lower() in ('y', 'yes'):
pass
elif yes_or_no.lower() in ('n', 'no'):
raise RuntimeError('Deletion abort per user request.')
client.delete_tasks(to_delete)
def pause_task(args):
args = parse_login_command_line(args, [], ['search', 'i', 'all'], help=lixian_help.pause)
client = XunleiClient(args.username, args.password, args.cookies)
to_pause = search_tasks(client, args)
print "Below files are going to be paused:"
for x in to_pause:
print x['name'].encode(default_encoding)
client.pause_tasks(to_pause)
def restart_task(args):
args = parse_login_command_line(args, [], ['search', 'i', 'all'], help=lixian_help.restart)
client = XunleiClient(args.username, args.password, args.cookies)
to_restart = search_tasks(client, args)
print "Below files are going to be restarted:"
for x in to_restart:
print x['name'].encode(default_encoding)
client.restart_tasks(to_restart)
def rename_task(args):
args = parse_login_command_line(args, [], [], help=lixian_help.rename)
if len(args) != 2 or not re.match(r'\d+$', args[0]):
usage(lixian_help.rename, 'Incorrect arguments')
sys.exit(1)
client = XunleiClient(args.username, args.password, args.cookies)
taskid, new_name = args
task = client.get_task_by_id(taskid)
client.rename_task(task, from_native(new_name))
def lixian_info(args):
args = parse_login_command_line(args, help=lixian_help.info)
client = XunleiClient(args.username, args.password, args.cookies, login=False)
print 'id:', client.get_username()
print 'internalid:', client.get_userid()
print 'gdriveid:', client.get_gdriveid() or ''
def lx_config(args):
args = parse_command_line(args, [], ['print', 'delete'], help=lixian_help.config)
if args.delete:
assert len(args) == 1
delete_config(args[0])
elif args['print'] or not len(args):
if len(args):
assert len(args) == 1
print get_config(args[0])
else:
print 'Loading', global_config.path, '...\n'
print source_config()
print global_config
else:
assert len(args) in (1, 2)
if args[0] == 'password':
if len(args) == 1 or args[1] == '-':
password = getpass('Password: ')
else:
password = args[1]
print 'Saving password (encrypted) to', global_config.path
put_config('password', encypt_password(password))
else:
print 'Saving configuration to', global_config.path
put_config(*args)
def print_hash(args):
assert len(args) == 1
print 'ed2k:', lixian_hash_ed2k.hash_file(args[0])
print 'dcid:', lixian_hash.dcid_hash_file(args[0])
def usage(doc=lixian_help.usage, message=None):
if hasattr(doc, '__call__'):
doc = doc()
if message:
print to_str(message)
print to_str(doc).strip()
def lx_help(args):
if len(args) == 1:
helper = getattr(lixian_help, args[0].lower(), lixian_help.help)
usage(helper)
elif len(args) == 0:
print lixian_help.welcome
else:
print lixian_help.help
def execute_command(args=sys.argv[1:]):
if not args:
usage()
sys.exit(1)
command = args[0]
if command.startswith('-'):
if command in ('-h', '--help'):
usage()
elif command in ('-v', '--version'):
print '0.0.x'
else:
usage()
sys.exit(1)
sys.exit(0)
commands = {'login': login, 'logout': logout, 'download': download_task, 'list': list_task, 'add': add_task, 'delete': delete_task, 'pause': pause_task, 'restart': restart_task, 'rename': rename_task, 'info': lixian_info, 'config': lx_config, 'hash': print_hash, 'help': lx_help}
if command not in commands:
usage()
sys.exit(1)
if '-h' in args or '--help' in args:
lx_help([command])
else:
commands[command](args[1:])
if __name__ == '__main__':
execute_command()