Esempio n. 1
0
 def execute(self, name=None):
     if type(name) is list:
         name = name[0]
     cache_dirs = context.get_dir_from_cache(context.get_rwd())
     # 判断目录是否已存在
     if name in map(lambda x:x['server_filename'], cache_dirs):
         _logger.error('target dir existed!')
     else:
         dir_path = '%s%s' % (context.get_rwd(), name)
         _logger.debug('create remote dir: %s' % dir_path)
         context.client.create_dir(dir_path)
Esempio n. 2
0
 def _download_torrent(self, torrent_file, mask=False):
     save_path = context.get_rwd()
     source_path = None
     # 查找种子文件
     torrent_file = os.path.join(context.get_lwd(), torrent_file)
     if os.path.exists(torrent_file):
         if not mask:
             # 上传种子文件
             print 'upload torrent file: %s ...' % torrent_file
             result = context.client.upload(context.get_rwd(), torrent_file)
             source_path = result['path']
         else:
             print 'mask torrent file ...'
             # 混淆bt文件
             torrent_data, name_dict = bt.mask_file(torrent_file)
             # 创建一层目录,作为下载存储目录
             save_path = '%s%s/' % (save_path, name_dict['prefix'])
             _logger.debug('save path: %s' % save_path)
             context.client.create_dir(save_path)
             # 上传混淆后的种子文件
             print 'upload masked torrent ...'
             result = context.client.upload_data(save_path, 'tmp.torrent', torrent_data)
             source_path = result['path']
             # 上传文件名字典
             print 'upload name dictionary ...'
             dict_data = json.dumps(name_dict)
             context.client.upload_data(save_path, 'dictionary.txt', dict_data)
     else:
         print 'no such torrent file!'
         return None
     # 获取种子文件信息
     try:
         print 'query torrent info ...'
         result = context.client.cloud_dl_query_bt_info(source_path)
         bt_info = result.get('torrent_info')
         # 创建bt任务
         task_id = None
         if bt_info is None:
             print 'can\'t get torrent info!'
         else:
             print 'create download for bt ...'
             # 选择种子中的全部文件
             selected_idx = map(lambda x:str(x+1), xrange(0, bt_info['file_count']))
             selected_idx = ','.join(selected_idx)
             # 创建离线下载任务
             result = context.client.cloud_dl_add_bt_task(source_path, selected_idx, bt_info['sha1'], save_path)
             task_id = result['task_id'] if result.has_key('task_id') else None
     except:
         print 'create download task failed ...'
     # 删除种子文件
     print 'delete torrent file ...'
     context.client.delete(json.dumps([source_path]))
     return task_id
Esempio n. 3
0
 def execute(self, arg=None):
     if arg is None: raise InvalidArgumentException()
     upload_file = arg if arg.startswith('/') else '%s%s' % (context.get_lwd(), arg)
     if os.path.exists(upload_file) and os.path.isfile(upload_file):
         context.client.upload_curl(context.get_rwd(), upload_file)
     else:
         raise InvalidArgumentException()
Esempio n. 4
0
 def get_completer_words(self, prefix):
     '''
     TODO 自动完成存在问题,推测与unicode有关
     '''
     # 获取要寻址的路径
     if prefix.startswith('/'):
         rwd = prefix
     else:
         if prefix is None: prefix = ''
         rwd = '%s%s' % (context.get_rwd(), prefix)
     # 处理路径中的相对路径
     if rwd.endswith('/'):
         lwd = os.path.abspath(rwd) + '/'
     else:
         lwd = os.path.abspath(rwd)
     # 拆分出父目录和子目录前缀
     parent_dir, sub_prefix = os.path.split(lwd)
     # 从缓存中获取父目录下的所有子目录
     if not context.has_dir_cache(parent_dir):
         result = context.client.list(parent_dir)
         context.cache_file_list(parent_dir, result['list'])
     sub_dirs = context.get_dir_from_cache(parent_dir)
     # 只保留名称
     sub_dirs = map(lambda x:x['server_filename'], sub_dirs)
     # 列出所有符合前缀的子目录
     return filter(lambda x:unicode(x).startswith(unicode(sub_prefix)), sub_dirs)
Esempio n. 5
0
 def upload_one_file(self, fn):
     upload_file = fn if fn.startswith('/') else '%s%s' % (context.get_lwd(), fn)
     if os.path.exists(upload_file) and os.path.isfile(upload_file):
         print 'upload file: %s ...' % upload_file
         result = context.client.upload_curl(context.get_rwd(), upload_file)
         print 'file saved to: %s !' % result.get('path')
     else:
         print 'no such file!'
Esempio n. 6
0
 def execute(self, args):
     # 处理过滤条件
     show_dir = len(args) == 0 or "dir" in args
     show_file = not ("dir" in args and len(args) == 1)
     exts = filter(lambda x: x != "dir", args)
     file_list = context.remote_tree.list(
         context.get_rwd(), show_dir=show_dir, show_file=show_file, show_exts=exts, force_fetch=True
     )
     self._print_file_list(file_list)
Esempio n. 7
0
 def execute(self, args):
     if args is None or len(args) == 0:
         print 'nothing to unmask'
         return
     target_dir = args[0]
     dir_objs = context.remote_tree.list_dir(context.get_rwd())
     for dir_obj in dir_objs:
         if dir_obj['server_filename'] == target_dir:
             self._unmask_start(dir_obj)
             break
Esempio n. 8
0
 def get_completer_words(self, prefix):
     if prefix.startswith('/'):
         target_path = prefix
     else:
         target_path = '%s%s' % (context.get_rwd(), prefix)
     target_path = tree.remote_abspath(target_path)
     parent_dir, name_prefix = tree.remote_splitpath(target_path)
     dirs = context.remote_tree.list_dir(parent_dir)
     dir_names = map(lambda x:'%s/' % util.unescape_arg(x['server_filename']), dirs)
     return filter(lambda x:len(name_prefix)==0 or x.startswith(name_prefix), dir_names)
Esempio n. 9
0
 def execute(self, arg=None):
     # 按名称降序获取文件列表
     rwd = context.get_rwd()
     result = context.client.list(rwd, order='name', desc=None)
     if result['errno'] == -9: raise NoSuchRemoteDirectory()
     file_list = result['list']
     # 缓存文件列表
     context.cache_file_list(rwd, file_list)
     # 输出文件列表
     self._print_file_list(file_list, arg)
Esempio n. 10
0
 def execute(self, args):
     if len(args) == 0:
         print 'nothing happened...'
     else:
         dir_name = args[0]
         dir_path = dir_name if dir_name.startswith('/') else '%s%s' % (context.get_rwd(), dir_name)
         if context.remote_tree.dir_exists(dir_path):
             print 'dir:%s existed!' % dir_path
         else:
             _logger.debug('create remote dir: %s' % dir_path)
             context.client.create_dir(dir_path)
Esempio n. 11
0
 def execute(self, args):
     # 处理过滤条件
     show_dir = len(args) == 0 or 'dir' in args
     show_file = not ('dir' in args and len(args) == 1)
     exts = filter(lambda x: x != 'dir', args)
     file_list = context.remote_tree.list(context.get_rwd(),
                                          show_dir=show_dir,
                                          show_file=show_file,
                                          show_exts=exts,
                                          force_fetch=True)
     self._print_file_list(file_list)
Esempio n. 12
0
 def execute(self, arg=None):
     if not arg: arg = '/'
     rwd = context.get_rwd()
     if arg.startswith('/'):
         rwd = arg
     else:
         rwd = '%s%s' % (rwd, arg)
         rwd = os.path.abspath(rwd)
         if not rwd.endswith('/'):
             rwd += '/'
     # TODO 检查目录是否存在
     context.set_rwd(rwd)
Esempio n. 13
0
 def execute(self, args):
     if len(args) == 0:
         print 'nothing happened...'
     else:
         dir_name = args[0]
         dir_path = dir_name if dir_name.startswith(
             '/') else '%s%s' % (context.get_rwd(), dir_name)
         if context.remote_tree.dir_exists(dir_path):
             print 'dir:%s existed!' % dir_path
         else:
             _logger.debug('create remote dir: %s' % dir_path)
             context.client.create_dir(dir_path)
Esempio n. 14
0
 def upload_one_file(self, fn):
     upload_file = fn if fn.startswith(
         '/') else '%s%s' % (context.get_lwd(), fn)
     if os.path.exists(upload_file) and os.path.isfile(upload_file):
         print 'upload file: %s ...' % upload_file
         result = context.client.upload_curl(context.get_rwd(), upload_file)
         if result.get('error_code') > 0:
             print 'upload failed! error code: %d' % result['error_code']
         else:
             print 'file saved to: %s !' % result.get('path')
     else:
         print 'no such file!'
Esempio n. 15
0
 def execute(self, args):
     target_dir = args[0] if len(args) > 0 else '/'
     if target_dir.startswith('/'):
         rwd = target_dir
     else:
         rwd = '%s%s' % (context.get_rwd(), target_dir)
     rwd = tree.remote_abspath(rwd)
     if not rwd.endswith('/'): rwd += '/'
     if context.remote_tree.dir_exists(rwd):
         context.set_rwd(rwd)
     else:
         print 'no such dir!'
Esempio n. 16
0
 def run(self):
     if context.client.is_login:
         print 'login user: %s' % context.client.user_name
     while context.is_alive():
         try:
             prompt = ('BaiduPan:%s> ' % context.get_rwd()).encode(_encoding)
             # 获取输入
             line = raw_input(prompt).strip().decode(_encoding)
             # 跳过空行
             if len(line) == 0: continue
             # 解析命令和参数
             cmd, args = manager.parse_input(line)
             if cmd is None:
                 print 'No such command'
                 continue
             if cmd.need_login and not context.client.is_login:
                 print 'You MUST login before use %s' % cmd.name
                 continue
             cmd.execute(args)
         except KeyboardInterrupt:
             print
         except:
             traceback.print_exc()
Esempio n. 17
0
 def run(self):
     if context.client.is_login:
         print 'login user: %s' % context.client.user_name
     while context.is_alive():
         try:
             prompt = ('BaiduPan:%s> ' %
                       context.get_rwd()).encode(_encoding)
             # 获取输入
             line = raw_input(prompt).strip().decode(_encoding)
             # 跳过空行
             if len(line) == 0: continue
             # 解析命令和参数
             cmd, args = manager.parse_input(line)
             if cmd is None:
                 print 'No such command'
                 continue
             if cmd.need_login and not context.client.is_login:
                 print 'You MUST login before use %s' % cmd.name
                 continue
             cmd.execute(args)
         except KeyboardInterrupt:
             print
         except:
             traceback.print_exc()
Esempio n. 18
0
 def get_completer_words(self, prefix):
     file_list = context.remote_tree.list_file(parent_dir=context.get_rwd())
     file_ids = map(lambda x:str(x['fs_id']), file_list)
     return filter(lambda x:len(prefix) == 0 or x.startswith(prefix), file_ids)
Esempio n. 19
0
 def execute(self, arg=None):
     print 'remote: %s' % context.get_rwd()
     print 'local : %s' % context.get_lwd()
Esempio n. 20
0
 def _download_ed2k(self, link):
     print 'create download for ed2k ...'
     result = context.client.cloud_dl_add_ed2k_task(link, context.get_rwd())
     task_id = result['task_id'] if result.has_key('task_id') else None
     return task_id
Esempio n. 21
0
 def execute(self, args):
     print 'remote: %s' % context.get_rwd()
     print 'local : %s' % context.get_lwd()
Esempio n. 22
0
 def get_completer_words(self, prefix):
     file_list = context.remote_tree.list_file(parent_dir=context.get_rwd())
     file_ids = map(lambda x:'%d ' % x['fs_id'], file_list)
     return filter(lambda x:len(prefix) == 0 or x.startswith(prefix), file_ids)
Esempio n. 23
0
 def get_completer_words(self, prefix):
     file_list = context.remote_tree.list(parent_dir=context.get_rwd(), force_fetch=False)
     file_ids = map(lambda x:'%d ' % x['fs_id'], file_list)
     return filter(lambda x:len(prefix) == 0 or x.startswith(prefix), file_ids)