1 Star 1 Fork 0

lucky/wps_download

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wps.py 5.38 KB
一键复制 编辑 原始数据 按行查看 历史
lucky 提交于 2016-10-14 12:42 . init readme
# coding=utf-8
import urllib2
import urllib
import json
import os
import ConfigParser
import codecs
import sys
from optparse import OptionParser
reload(sys)
sys.setdefaultencoding('utf-8')
class MetaData(object):
filename = None
fileid = None
fsize = None
nickname = None
mtime = None
key = None
class WPS(object):
key = None
preview_url = "https://qing.wps.cn/preview/#l/{key}"
download_url = "https://qing.wps.cn/api/links/{key}/download?chkcode="
metadata_url = "https://qing.wps.cn/api/skiplogin/links/{key}"
remote_metadata = None
local_metadata = None
base_dir = None # 文档输出主目录
filedir = None # 文档目录,以fileid命名
metadata_filepath = None # 元数据文件路径
document_filepath = None # 文档路径
def __init__(self, key, base_dir):
self.key = key
self.preview_url = self.preview_url.format(key=key)
self.download_url = self.download_url.format(key=key)
self.metadata_url = self.metadata_url.format(key=key)
self.base_dir = base_dir
self.remote_metadata = self.get_remote_metadata()
self.filedir = "%s/%s" % (self.base_dir, self.remote_metadata.fileid)
self.metadata_filepath = "%s/metadata.ini" % self.filedir
self.document_filepath = "%s/%s" % (self.filedir, self.remote_metadata.filename)
if not os.path.exists(self.filedir):
os.makedirs(self.filedir)
def get_remote_metadata(self):
if self.remote_metadata:
return self.remote_metadata
response = urllib2.urlopen(self.metadata_url)
html = response.read()
obj = json.loads(html)
metadata = MetaData()
metadata.filename = obj["lightlink"]['fname']
metadata.fileid = obj["lightlink"]['fileid']
metadata.fsize = obj["lightlink"]["fsize"]
metadata.nickname = obj["lightlink"]["nickname"]
metadata.mtime = obj["lightlink"]["mtime"]
metadata.key = self.key
self.remote_metadata = metadata
return self.remote_metadata
def get_download_file(self):
response = urllib2.urlopen(self.download_url)
html = response.read()
obj = json.loads(html)
file_url = obj['fileinfo']['url']
urllib.urlretrieve(file_url, self.document_filepath)
def get_local_metadata(self):
if self.local_metadata:
return self.local_metadata
if not os.path.exists(self.metadata_filepath):
return None
else:
cp = ConfigParser.ConfigParser()
cp.read(self.metadata_filepath)
metadata = MetaData()
metadata.filename = cp.get('metadata', 'filename')
metadata.fileid = int(cp.get('metadata', 'fileid'))
metadata.fsize = int(cp.get('metadata', 'fsize'))
metadata.nickname = cp.get('metadata', 'nickname')
metadata.mtime = int(cp.get('metadata', 'mtime'))
metadata.key = cp.get('metadata', 'key')
self.local_metadata = metadata
return metadata
def check_need_update(self):
'''
检查是否需要更新本地文件:
1.如果本地metadata文件不存在,需要更新
2.如果本地metadata.mtime 与远程不一致,需要更新
3.如果本地文档不存在,需要更新
:return:
'''
if not self.get_local_metadata():
return True
if self.get_local_metadata().mtime <> self.get_remote_metadata().mtime:
return True
if not os.path.exists(self.document_filepath):
return True
return False
def update_local_metadata(self):
self.local_metadata = self.get_remote_metadata()
md = self.local_metadata
cp = ConfigParser.ConfigParser()
cp.add_section("metadata")
cp.set("metadata", "fileid", md.fileid)
cp.set("metadata", "filename", md.filename)
cp.set("metadata", "fsize", md.fsize)
cp.set("metadata", "nickname", md.nickname)
cp.set("metadata", "mtime", md.mtime)
cp.set("metadata", "key", md.key)
with codecs.open(self.metadata_filepath, 'w', 'utf-8') as configfile:
cp.write(configfile)
def run(self):
if self.check_need_update():
self.get_download_file()
self.update_local_metadata()
metadata = self.get_local_metadata()
print "file:%s key:%s update success" % (metadata.filename, metadata.key)
else:
metadata = self.get_local_metadata()
print "file:%s key:%s mtime:%s don't have to update" % (metadata.filename, metadata.key, metadata.mtime)
def usage():
print "aaa"
def main():
parser = OptionParser()
parser.add_option("-k", "--key", dest="key", help="the wps document short url key")
parser.add_option("-o", "--output", dest="output", help="the base dir to store download document file ")
(options, args) = parser.parse_args()
key=options.key
output=options.output
if key and output:
if os.path.exists(output):
wps_download = WPS(key=key, base_dir=output)
wps_download.run()
else:
parser.error("options -o output dir %s don't exists"%output)
parser.print_help()
else:
parser.print_help()
parser.error("options -k and -o are mutually exclusive")
exit()
if __name__ == '__main__':
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lucky-nj/wps_download.git
git@gitee.com:lucky-nj/wps_download.git
lucky-nj
wps_download
wps_download
master

搜索帮助