Giter VIP home page Giter VIP logo

baidupcsapi's Introduction

百度网盘API

项目名称解释

后来我才搞清楚pcs并不是指百度网盘,想过改成baidupanapi但是由于历史原因就算了。

文档

http://ly0.github.io/baidupcsapi

随时更新处

  • 再也不会看到 wenxintishi.avi 了!
  • 百度网盘登录现在需要RSA加密后传输, 详情请参考 api.py 中的 PCS._get_pubkeyPCS._login 函数
  • 新加入 PCS.download_url 方法, 可以直接获得可用下载链接 (支持批量下载, 参数为 str 或者 str list)
  • 记住验证码的处理函数在初始化 PCS 类时通过 captcha_func 参数指定, 其中第一个参数会给予 JPEG 数据, handler 处理完后需要返回一个可行的验证码.

正文

百度网盘现在不开放PCS API,所以目前只能用百度自己的API 应该是可以了,参考 (https://github.com/mozillazg/baidu-pcs-python-sdk/wiki/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96-Access-Token-%E5%92%8C-Refresh-Token%EF%BC%9F)

一个基于此api的 fuse(测试环境ubuntu12.04) http://github.com/ly0/baidu-fuse

web版百度网盘(可以用来开放资源,测试环境ubuntu14.04) https://github.com/ly0/web.baidupan

Installation

To install baidupcsapi, simply:

$ pip install baidupcsapi

一些简单的例子

>>> from baidupcsapi import PCS
>>> pcs = PCS('username','password')
>>> print pcs.quota().content
>>> print pcs.list_files('/').content

断点续传

下载

>>> headers = {'Range': 'bytes=0-99'}
>>> pcs = PCS('username','password')
>>> pcs.download('/test_sdk/test.txt', headers=headers)

上传

有时间写个demo,大概是 将文件分块,计算每一块的MD5 precreate 对于服务器上的某个文件需要上传的块 upload_tmpfile 上传临时文件(块) upload_superfile 合并块(在precreate返回所需块为空时调用本函数可合并文件) 注意,百度服务器上文件是分块保存的,块不会消失

上传文件的进度条实现范例

回调函数参数要求 有size和progress两个参数名, size:文件总字节数 progress:当前传输完成字节数

	import progressbar
	from baidupcsapi import PCS
	class ProgressBar():
	    def __init__(self):
	        self.first_call = True
	    def __call__(self, *args, **kwargs):
	        if self.first_call:
	            self.widgets = [progressbar.Percentage(), ' ', progressbar.Bar(marker=progressbar.RotatingMarker('>')),
	                            ' ', progressbar.ETA()]
	            self.pbar = progressbar.ProgressBar(widgets=self.widgets, maxval=kwargs['size']).start()
	            self.first_call = False
	
	        if kwargs['size'] <= kwargs['progress']:
	            self.pbar.finish()
	        else:
	            self.pbar.update(kwargs['progress'])
	
	
	pcs = PCS('username','password')
	test_file = open('bigfile.pdf','rb').read()
	ret = pcs.upload('/',test_file,'bigfile.pdf',callback=ProgressBar())

合并文件

可以用两个纯文本文档合并,这样产生的新文档是两个文本文档的文字合并 注意upload系列的函数都可以指定callback参数

pcs = PCS('username','password')
print 'chunk1'
ret = pcs.upload_tmpfile(open('1.txt','rb'))
md51 = json.loads(ret.content)['md5']
print 'chunk2'
ret = pcs.upload_tmpfile(open('2.txt','rb'))
md52 = json.loads(ret.content)['md5']
print 'merge'
ret = pcs.upload_superfile('/3.txt',[md51,md52])
print ret.content

在根目录下就会有3.txt

baidupcsapi's People

Contributors

ly0 avatar jonans avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.