def upload_bpcs(upload_dir, upload_file, del_file): access_token = "access" # 从百度云获取 disk = BaiduPan(access_token) bpcs_dir = '/apps/bpcs_uploader/' if not disk.meta(bpcs_dir + upload_dir): disk.mkdir(bpcs_dir + upload_dir) # 查看使用情况 # print disk.quota() disk.upload(upload_file, path=bpcs_dir + upload_dir + upload_file, ondup='overwrite') # 上传文件,如果存在,直接覆盖 if disk.meta(bpcs_dir + upload_dir + del_file): # 删除历史文件 disk.rm(bpcs_dir + upload_dir + del_file)
def upload_bpcs(upload_dir,upload_file,del_file): access_token="access"#从百度云获取 disk=BaiduPan(access_token) bpcs_dir='/apps/bpcs_uploader/' if not disk.meta(bpcs_dir+upload_dir): disk.mkdir(bpcs_dir+upload_dir) #查看使用情况 #print disk.quota() disk.upload(upload_file, path=bpcs_dir+upload_dir+upload_file,ondup='overwrite') #上传文件,如果存在,直接覆盖 if disk.meta(bpcs_dir+upload_dir+del_file): #删除历史文件 disk.rm(bpcs_dir+upload_dir+del_file)
class BaiduFS(Fuse): '''Baidu netdisk filesystem''' def __init__(self, *args, **kw): Fuse.__init__(self, *args, **kw) self.disk = BaiduPan(Baidufuseconf.baidu_token) def get_abs_path(self, path): return "%s%s" % (baidu_rootdir, path) def getattr(self, path): logger.error("getattr is: " + path) abs_path = self.get_abs_path(path) st = MyStat() jdata = json.loads(self.disk.meta(abs_path)) if 'list' not in jdata: logger.error("getattr is None") return -errno.ENOENT st.st_ctime = jdata['list'][0]['ctime'] st.st_mtime = jdata['list'][0]['mtime'] st.st_mode = (stat.S_IFDIR | 0755) if jdata['list'][0]['isdir']\ else (stat.S_IFREG | 0755) st.st_nlink = 2 if jdata['list'][0]['isdir'] else 1 st.st_size = jdata['list'][0]['size'] return st def readdir(self, path, offset): logger.error("readdir is: " + path) abs_path = self.get_abs_path(path) jdata = json.loads(self.disk.ls(abs_path)) files = ['.', '..'] for r in jdata['list']: files.append(r['path'].encode('ascii', 'ignore')[len(abs_path):]) logger.error(files) for r in files: yield fuse.Direntry(r) def open(self, path, flags): logger.error("open is: " + path) pass accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR if (flags & accmode) != os.O_RDONLY: return -errno.EACCES def mkdir(self, path, mode): logger.error("mkdir is:" + path) abs_path = self.get_abs_path(path) self.disk.mkdir(abs_path) def rmdir(self, path): logger.error("rmdir is:" + path) abs_path = self.get_abs_path(path) self.disk.rm(abs_path) def read(self, path, size, offset): logger.error("read is: " + path) abs_path = self.get_abs_path(path) paras = {'Range': 'bytes=%s-%s' % (offset, offset + size)} return self.disk.download(abs_path, paras)
class BaiduFS(Fuse): '''Baidu netdisk filesystem''' def __init__(self, *args, **kw): Fuse.__init__(self, *args, **kw) self.disk = BaiduPan(Baidufuseconf.baidu_token) def get_abs_path(self, path): return "%s%s" % (Baidufuseconf.baidu_rootdir, path) def getattr(self, path): logger.error("getattr is: " + path) abs_path = self.get_abs_path(path) st = MyStat() jdata = json.loads(self.disk.meta(abs_path)) if 'list' not in jdata: logger.error("getattr is None") return -errno.ENOENT st.st_ctime = jdata['list'][0]['ctime'] st.st_mtime = jdata['list'][0]['mtime'] st.st_mode = (stat.S_IFDIR | 0755) if jdata['list'][0]['isdir']\ else (stat.S_IFREG | 0755) st.st_nlink = 2 if jdata['list'][0]['isdir'] else 1 st.st_size = jdata['list'][0]['size'] return st def readdir(self, path, offset): logger.error("readdir is: " + path) abs_path = self.get_abs_path(path) jdata = json.loads(self.disk.ls(abs_path)) files = ['.', '..'] for r in jdata['list']: files.append(r['path'].encode('ascii', 'ignore')[len(abs_path):]) logger.error(files) for r in files: yield fuse.Direntry(r) def open(self, path, flags): logger.error("open is: " + path) pass accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR if (flags & accmode) != os.O_RDONLY: return -errno.EACCES def mkdir(self, path, mode): logger.error("mkdir is:" + path) abs_path = self.get_abs_path(path) self.disk.mkdir(abs_path) def rmdir(self, path): logger.error("rmdir is:" + path) abs_path = self.get_abs_path(path) self.disk.rm(abs_path) def read(self, path, size, offset): logger.error("read is: " + path) abs_path = self.get_abs_path(path) paras = {'Range': 'Range: bytes=%s-%s' % (offset, offset + size)} return self.disk.download(abs_path, headers=paras)
class DefaultTestCase(unittest.TestCase): def setUp(self): self.baidupan = BaiduPan() self.request_patcher = patch("baidupan.baidupan.BaiduPan._request") self.mock_request = self.request_patcher.start() def tearDown(self): self.request_patcher.stop() def test_version(self): self.assertIsNotNone(baidupan.__version__, '0.0.1') @patch("__builtin__.open") def test_upload(self, mock_open): mock_open.return_value.read.return_value = "hulahoop" self.baidupan.upload({"keyword": "hulahoop"}) self.assertEqual(self.mock_request.call_args, call(file='hulahoop', filename={'keyword': 'hulahoop'})) def test_merge(self): self.baidupan.merge('path', 'param', keyword="hulahoop") self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param', path='path')) def test_similar_path_functions(self): for method in ('download', 'mkdir', 'meta'): getattr(self.baidupan, method)('path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', path='path')) def test_similar_param_functions(self): for method in ('mmv', 'mmeta'): getattr(self.baidupan, method)('param', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param')) def test_mv_cp(self): for method in ('mv', 'cp'): getattr(self.baidupan, method)('from_path', 'to_path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', from_path='from_path', to='to_path')) @patch("requests.post") @patch("requests.get") def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue(mock_get.call_args, call('base_urlurlpath', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual(mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual(mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()
#texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False ########NEW FILE######## __FILENAME__ = example #!/usr/bin/env python #coding=utf-8 import json from baidupan.baidupan import BaiduPan if __name__ == '__main__': access_token = '' disk = BaiduPan(access_token) #quota print disk.quota() #upload print disk.upload('hello', path='/apps/appname/hello.txt') #merge ''' def merge(self, path, param, **kw): self.urlpath = 'file' self.method = 'createsuperfile' self._method = 'POST' return self._request(path=path, param=param, **kw) ''' param = '' print disk.merge('/apps/appname/hello.txt', param=param) #download
def __init__(self, root, token): self.root = root self.token = token self.api = BaiduPan(self.token)
class BaiFuse(LoggingMixIn, Operations): def __init__(self, root, token): self.root = root self.token = token self.api = BaiduPan(self.token) def get_path(self, path): return os.path.join(BAIDUPATH,path) # def chmod(self, path, mode): # return True # def chown(self, path, uid, gid): # return True # def create(self, path, mode): # f = self.sftp.open(path, 'w') # f.chmod(mode) # f.close() # return 0 # def destroy(self, path): # self.sftp.close() # self.client.close() def getattr(self, path, fh=None): resp = json.loads(self.api.meta(self.get_path(path))) if 'list' not in resp: return {} return { 'st_ino': 0, 'st_dev': 0, 'st_atime': 0, 'st_mtime': resp['list'][0]['mtime'], 'st_ctime': resp['list'][0]['ctime'], 'st_gid': os.getgid(), 'st_uid': os.getuid(), 'st_mode': ((stat.S_IFDIR | 0755) if resp['list'][0]['isdir'] else (stat.S_IFREG | 0755)), 'st_size': resp['list'][0]['size'], 'st_nlink': (2 if resp['list'][0]['isdir'] else 1), } def mkdir(self, path, mode): self.api.mkdir(self.get_path(path)) #return? def read(self, path, size, offset, fh): return self.api.download(self.get_path(path), headers={'Range':"Range: bytes=%s-%s"%(offset,offset+size)}) def readdir(self, path, fh): resp = json.loads(self.api.ls(self.get_path(path))) return ['.', '..'] + [name['path'].encode('utf-8') for name in resp['list']] # def readlink(self, path): # return self.sftp.readlink(path) # def rename(self, old, new): # return self.sftp.rename(old, self.root + new) def rmdir(self, path): self.api.rm(self.get_path(path)) # return ? # def symlink(self, target, source): # return self.sftp.symlink(source, target) # def truncate(self, path, length, fh=None): # return self.sftp.truncate(path, length) # def unlink(self, path): # return self.sftp.unlink(path) # def utimens(self, path, times=None): # return self.sftp.utime(path, times) def write(self, path, data, offset, fh): # can't use the api -> need file dissociation + merge (super file) return self.api.upload(path)
def __init__(self, *args, **kw): Fuse.__init__(self, *args, **kw) self.disk = BaiduPan(Baidufuseconf.baidu_token)
def setUp(self): self.baidupan = BaiduPan() self.request_patcher = patch("baidupan.baidupan.BaiduPan._request") self.mock_request = self.request_patcher.start()
#!/usr/bin/env python #coding=utf-8 import json from baidupan.baidupan import BaiduPan if __name__ == '__main__': access_token = '' disk = BaiduPan(access_token) #quota print disk.quota() #upload print disk.upload('hello', path='/apps/appname/hello.txt') #merge ''' def merge(self, path, param, **kw): self.urlpath = 'file' self.method = 'createsuperfile' self._method = 'POST' return self._request(path=path, param=param, **kw) ''' param = '' print disk.merge('/apps/appname/hello.txt', param=param) #download print disk.download(path='/apps/appname/hello.txt') #mkdir print disk.mkdir('/apps/appname/dirname') #meta print disk.meta('/apps/appname/filename') #mmeta print disk.mmeta(json.dumps({"list": [{"path": "/apps/appname/"}]}))
logger = logging.getLogger('mylogger') cp = ConfigParser.SafeConfigParser() cp.read('cloud_album.conf') gpgkeyname = cp.get('gnupg', 'keyname') gpg = gnupg.GPG(gnupghome=cp.get('gnupg', 'home')) appid = int(cp.get('cos', 'appid')) secretid = unicode(cp.get('cos', 'secretid'), "utf-8") secretkey = unicode(cp.get('cos', 'secretkey'), "utf-8") cos_client = qcloud_cos.CosClient(appid, secretid, secretkey) access_token = unicode(cp.get('baiduyun', 'access_token'), "utf-8") baiduyundisk = BaiduPan(access_token) leancloud.init(cp.get('leancloud', 'appid'), cp.get('leancloud', 'appkey')) logger.setLevel(logging.INFO) #print args.photofiles if os.path.isfile(args.photofiles[0]): totalfilename = args.photofiles[0] p, photofile = os.path.split(totalfilename) if not isphototype(totalfilename): logger.error('Not photo : ' + photofile) sys.exit(0) (filesize, photodateYear, photodateYearMonth, remotefilename) = get_file_info(totalfilename, photofile)
def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue( mock_get.call_args, call('base_urlurlpath', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual( mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual( mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()
def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue(mock_get.call_args, call('base_urlurlpath', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual(mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual(mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()
class DefaultTestCase(unittest.TestCase): def setUp(self): self.baidupan = BaiduPan() self.request_patcher = patch("baidupan.baidupan.BaiduPan._request") self.mock_request = self.request_patcher.start() def tearDown(self): self.request_patcher.stop() def test_version(self): self.assertIsNotNone(baidupan.__version__, '0.0.1') @patch("__builtin__.open") def test_upload(self, mock_open): mock_open.return_value.read.return_value = "hulahoop" self.baidupan.upload({"keyword": "hulahoop"}) self.assertEqual( self.mock_request.call_args, call(file='hulahoop', filename={'keyword': 'hulahoop'})) def test_merge(self): self.baidupan.merge('path', 'param', keyword="hulahoop") self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param', path='path')) def test_similar_path_functions(self): for method in ('download', 'mkdir', 'meta'): getattr(self.baidupan, method)('path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', path='path')) def test_similar_param_functions(self): for method in ('mmv', 'mmeta'): getattr(self.baidupan, method)('param', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param')) def test_mv_cp(self): for method in ('mv', 'cp'): getattr(self.baidupan, method)('from_path', 'to_path', keyword='hulahoop') self.assertEqual( self.mock_request.call_args, call(keyword='hulahoop', from_path='from_path', to='to_path')) @patch("requests.post") @patch("requests.get") def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue( mock_get.call_args, call('base_urlurlpath', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual( mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual( mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()