def list_task(args): status = "all" if args.completed: status = "completed" elif args.deleted: status = "deleted" elif args.expired: status = "expired" 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=status) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) # 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=status) elif status == "all": tasks = client.read_all_tasks() elif status == "completed": tasks = filter(lambda x: x["status_text"] == "completed", client.read_all_tasks()) # by #139 elif status == "deleted": tasks = client.read_all_deleted() elif status == "expired": tasks = client.read_all_expired() else: raise NotImplementedError(status) columns = [ "n", "id", "name", "status", "size", "progress", "speed", "date", "dcid", "gcid", "original-url", "download-url", ] columns = filter(lambda k: getattr(args, k), columns) output_tasks(tasks, columns, args, not parent_ids)
def list(): rs={'msg':'','log':'','rst':'','data':{}} form = cgi.FieldStorage() if "tasktype" in form: status=form["tasktype"].value else: status = 'all' if "id" in form: id=form["id"].value else: id = None try: sys.path.append('core') from wshconfig import readconf conf=readconf() from lixian import XunleiClient client = XunleiClient(conf['username'],conf['password'],conf['LIXIAN_HOME'] +'/.xunlei.lixian.cookies') except Exception as e: rs['msg'] = "needlogin" rs['log'] = str(e) return rs if status == 'all': tasks = client.read_all_tasks() elif status == 'completed': tasks = filter(lambda x: x['status_text'] == 'completed', client.read_all_tasks()) elif status == 'deleted': tasks = client.read_all_deleted() elif status == 'expired': tasks = client.read_all_expired() outputs=[] for i, t in enumerate(tasks): one={} one['name']=t['name'] one['id']=t['id'] one["status"]=t["status_text"] one["size"]=t["size"] one["progress"]=t["progress"] one["type"]=t["type"] outputs.append(one) rs["data"]=outputs return rs
def get_torrent(args): ''' usage: lx get-torrent [info-hash|task-id]... ''' from lixian_cli import parse_login_command_line args = parse_login_command_line(args) client = XunleiClient(args.username, args.password, args.cookies) for id in args: id = id.lower() import re if re.match(r'[a-fA-F0-9]{40}$', id): torrent = client.get_torrent_file_by_info_hash(id) elif re.match(r'#?\d+$', id): tasks = client.read_all_tasks() from lixian_tasks import find_task_by_id task = find_task_by_id(tasks, id) assert task, id + ' not found' id = task['bt_hash'] id = id.lower() torrent = client.get_torrent_file_by_info_hash(id) else: raise NotImplementedError() path = id + '.torrent' print path with open(path, 'wb') as output: output.write(torrent)
def download_task(args): args = parse_login_command_line(args, ['tool', 'output', 'output-dir', 'input'], ['delete', 'continue', 'overwrite', 'torrent', 'all', 'mini-hash', 'hash'], alias={'o': 'output', 'i': 'input', 'c':'continue', 'bt':'torrent'}, 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) or args.input: assert not args.output tasks = find_tasks_to_download(client, args) 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 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", "bt": "torrent"}, 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", to_utf_8(link)) if found: print found[0]["id"], 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["id"], task["status_text"], link
def list_task(args): args = parse_login_command_line(args, [], ['all', 'completed', 'id', 'name', 'status', 'size', 'dcid', 'gcid', 'original-url', 'download-url', 'speed', 'progress', 'date', 'n'], default={'id': True, 'name': True, 'status': True, 'n': get_config('n')}, 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=(args.completed and 'completed' or 'all')) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) #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')) elif args.completed: tasks = client.read_all_completed() else: tasks = client.read_all_tasks() columns = ['n', 'id', 'name', 'status', 'size', 'progress', 'speed', 'date', 'dcid', 'gcid', 'original-url', 'download-url'] columns = filter(lambda k: getattr(args, k), columns) for i, t in enumerate(tasks): for k in columns: if k == 'n': if not parent_ids: print '#%d' % t['#'], elif 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 == '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 list_task(args): args = parse_login_command_line(args, [], ['all', 'completed', 'task-id', 'name', 'status', 'size', 'dcid', 'original-url', 'download-url', 'id', 'file', 'url',], default={'task-id': True, 'name': True, 'status': True}) client = XunleiClient(args.username, args.password, args.cookies) client.set_page_size(100) if args.id or args.file or args.url or len(args): 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 = ['task-id', 'name', 'status', 'size', 'dcid', 'original-url', 'download-url'] columns = filter(lambda k: getattr(args, k), columns) for t in tasks: for k in columns: if k == 'task-id': print 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 == '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 download_task(args): import lixian_download_tools download = lixian_download_tools.get_tool(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) or args.input: tasks = find_tasks_to_download(client, args) 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_tasks() download_multiple_tasks(client, download, tasks, download_args) else: usage(doc=lixian_help.download, message="Not enough arguments")
def download_task(args): args = parse_login_command_line(args, ['tool', 'output', 'output-dir', 'input'], ['delete', 'continue', 'overwrite', 'torrent', 'all', 'mini-hash', 'hash'], alias={'o': 'output', 'i': 'input', 'c':'continue', 'bt':'torrent'}, 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) import lixian_download_tools download = lixian_download_tools.get_tool(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) or args.input: tasks = find_tasks_to_download(client, args) 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 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(links) 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 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', to_utf_8(link)) if found: print found[0]['id'], 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['id'], task['status_text'], link
def add_task(args): args = parse_login_command_line(args, ['input'], ['torrent'], alias={'i':'input'}) 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(links) 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 list_task(args): status = 'all' if args.completed: status = 'completed' elif args.deleted: status = 'deleted' elif args.expired: status = 'expired' 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=status) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) #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=status) elif args.category: tasks = client.read_all_tasks_by_category(from_native(args.category)) elif status == 'all': tasks = client.read_all_tasks() elif status == 'completed': tasks = filter(lambda x: x['status_text'] == 'completed', client.read_all_tasks()) # by #139 elif status == 'deleted': tasks = client.read_all_deleted() elif status == 'expired': tasks = client.read_all_expired() else: raise NotImplementedError(status) columns = ['n', 'id', 'name', 'status', 'size', 'progress', 'speed', 'date', 'dcid', 'gcid', 'original-url', 'download-url'] columns = filter(lambda k: getattr(args, k), columns) output_tasks(tasks, columns, args, not parent_ids)
def list_task(args): status = 'all' if args.completed: status = 'completed' elif args.deleted: status = 'deleted' elif args.expired: status = 'expired' 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=status) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) #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=status) elif status == 'all': tasks = client.read_all_tasks() elif status == 'completed': tasks = filter(lambda x: x['status_text'] == 'completed', client.read_all_tasks()) # by #139 elif status == 'deleted': tasks = client.read_all_deleted() elif status == 'expired': tasks = client.read_all_expired() else: raise NotImplementedError(status) columns = ['n', 'id', 'name', 'status', 'size', 'progress', 'speed', 'date', 'dcid', 'gcid', 'original-url', 'download-url'] columns = filter(lambda k: getattr(args, k), columns) output_tasks(tasks, columns, args, not parent_ids)
def download_task(args): import lixian_download_tools download = lixian_download_tools.get_tool(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) or args.input: tasks = find_tasks_to_download(client, args) 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_tasks() download_multiple_tasks(client, download, tasks, download_args) else: usage(doc=lixian_help.download, message='Not enough arguments')
def download_task(args): import lixian_download_tools download = lixian_download_tools.get_tool(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, 'no_bt_dir': not args.bt_dir, 'save_torrent_file':args.save_torrent_file} client = XunleiClient(args.username, args.password, args.cookies) links = None if len(args) or args.input: tasks = find_tasks_to_download(client, args) 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_tasks() download_multiple_tasks(client, download, tasks, download_args) else: usage(doc=lixian_help.download, message='Not enough arguments')
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', to_utf_8(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 download_task(args): args = parse_login_command_line( args, ["tool", "output", "output-dir", "input"], ["delete", "continue", "overwrite", "torrent", "all", "mini-hash", "hash"], alias={"o": "output", "i": "input", "c": "continue", "bt": "torrent"}, 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, ) import lixian_download_tools download = lixian_download_tools.get_tool(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) or args.input: tasks = find_tasks_to_download(client, args) 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 delete_task(args): client = XunleiClient(args.username, args.password, args.cookies) if len(args): to_delete = lixian_query.search_tasks(client, args) elif args.all: to_delete = client.read_all_tasks() if not to_delete: print 'Nothing to delete' return with colors(args.colors).red.bold(): 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 delete_task(args): client = XunleiClient(args.username, args.password, args.cookies) if len(args): to_delete = search_tasks(client, args) elif args.all: to_delete = client.read_all_tasks() if not to_delete: print 'Nothing to delete' return from lixian_colors import colors with colors(args.colors).red.bold(): 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 get_torrent(args): ''' usage: lx get-torrent [info-hash|task-id]... ''' client = XunleiClient(args.username, args.password, args.cookies) for id in args: id = id.lower() import re if re.match(r'[a-fA-F0-9]{40}$', id): torrent = client.get_torrent_file_by_info_hash(id) elif re.match(r'\d+$', id): tasks = client.read_all_tasks() import lixian_query base = lixian_query.TaskBase(client, args) task = base.get_task_by_id(id) id = task['bt_hash'] id = id.lower() torrent = client.get_torrent_file_by_info_hash(id) else: raise NotImplementedError() path = id + '.torrent' print path with open(path, 'wb') as output: output.write(torrent)
def delete_task(args): client = XunleiClient(args.username, args.password, args.cookies) if len(args): to_delete = search_tasks(client, args) elif args.all: to_delete = client.read_all_tasks() if not to_delete: print "Nothing to delete" return from lixian_colors import colors with colors(args.colors).red.bold(): 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 get_torrent(args): ''' usage: lx get-torrent [info-hash|task-id]... ''' client = XunleiClient(args.username, args.password, args.cookies) for id in args: id = id.lower() import re if re.match(r'[a-fA-F0-9]{40}$', id): torrent = client.get_torrent_file_by_info_hash(id) elif re.match(r'\d+$', id): tasks = client.read_all_tasks() import lixian_query base = lixian_query.TaskBase(client, client.read_all_tasks) task = base.get_task_by_id(id) id = task['bt_hash'] id = id.lower() torrent = client.get_torrent_file_by_info_hash(id) else: raise NotImplementedError() path = id + '.torrent' print path with open(path, 'wb') as output: output.write(torrent)
def list_task(args): args = parse_login_command_line(args, [], ['all', 'completed', 'deleted', 'expired', 'id', 'name', 'status', 'size', 'dcid', 'gcid', 'original-url', 'download-url', 'speed', 'progress', 'date', 'n', 'format-size', 'colors' ], default={'id': get_config('id', True), 'name': True, 'status': True, 'n': get_config('n'), 'size': get_config('size'), 'format-size': get_config('format-size'), 'colors': get_config('colors', True)}, help=lixian_help.list) status = 'all' if args.completed: status = 'completed' elif args.deleted: status = 'deleted' elif args.expired: status = 'expired' 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=status) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) #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=status) elif status == 'all': tasks = client.read_all_tasks() elif status == 'completed': tasks = client.read_all_completed() elif status == 'deleted': tasks = client.read_all_deleted() elif status == 'expired': tasks = client.read_all_expired() else: raise NotImplementedError(status) columns = ['n', 'id', 'name', 'status', 'size', 'progress', 'speed', 'date', 'dcid', 'gcid', 'original-url', 'download-url'] columns = filter(lambda k: getattr(args, k), columns) from lixian_colors import colors 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 not parent_ids: 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 list_task(args): args = parse_login_command_line( args, [], [ "all", "completed", "id", "name", "status", "size", "dcid", "gcid", "original-url", "download-url", "speed", "progress", "date", "n", "format-size", ], default={ "id": True, "name": True, "status": True, "n": get_config("n"), "format-size": get_config("format-size"), }, 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=(args.completed and "completed" or "all")) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) # 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")) elif args.completed: tasks = client.read_all_completed() else: tasks = client.read_all_tasks() columns = [ "n", "id", "name", "status", "size", "progress", "speed", "date", "dcid", "gcid", "original-url", "download-url", ] columns = filter(lambda k: getattr(args, k), columns) for i, t in enumerate(tasks): for k in columns: if k == "n": if not parent_ids: print "#%d" % t["#"], elif 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": 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 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', to_utf_8(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 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", "bt": "torrent"}, 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", to_utf_8(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 list_task(args): args = parse_login_command_line(args, [], [ 'all', 'completed', 'deleted', 'expired', 'id', 'name', 'status', 'size', 'dcid', 'gcid', 'original-url', 'download-url', 'speed', 'progress', 'date', 'n', 'format-size', 'colors' ], default={ 'id': get_config('id', True), 'name': True, 'status': True, 'n': get_config('n'), 'size': get_config('size'), 'format-size': get_config('format-size'), 'colors': get_config('colors', True) }, help=lixian_help.list) status = 'all' if args.completed: status = 'completed' elif args.deleted: status = 'deleted' elif args.expired: status = 'expired' 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: args[0] = args[0][:-1] tasks = search_tasks(client, args, status=status) assert len(tasks) == 1 tasks = client.list_bt(tasks[0]) #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=status) elif status == 'all': tasks = client.read_all_tasks() elif status == 'completed': tasks = client.read_all_completed() elif status == 'deleted': tasks = client.read_all_deleted() elif status == 'expired': tasks = client.read_all_expired() else: raise NotImplementedError(status) columns = [ 'n', 'id', 'name', 'status', 'size', 'progress', 'speed', 'date', 'dcid', 'gcid', 'original-url', 'download-url' ] columns = filter(lambda k: getattr(args, k), columns) from lixian_colors import colors 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 not parent_ids: 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