代码拉取完成,页面将自动刷新
# 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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。