import os
import qcloud_cos
'''
发布博客到腾讯云cos上面去
腾讯云上面对应的根目录为/blog
'''
COS_APPID=125100066
COS_SECRET_ID=u'<>'
COS_SECRET_KEY=u'<>'
COS_REGION_INFO='guangzhou'
currentPath=os.path.dirname(os.path.abspath(__file__))
cos_client=None
lastChange={}
if os.path.exists(os.path.join(currentPath,'lastchange.txt')):
with open(os.path.join(currentPath,'lastchange.txt'),'r') as f:
for line in f:
item=line.split(',')
lastChange[unicode(item[0],'utf-8')] = float(item[1])
def upload(remotePath,localPath):
global cos_client
if cos_client==None:
cos_client=qcloud_cos.CosClient(COS_APPID,COS_SECRET_ID,COS_SECRET_KEY,COS_REGION_INFO)
request=qcloud_cos.UploadFileRequest(u'wwwlixinme',unicode(remotePath) ,unicode( localPath))
request.set_insert_only(0)
upload_file_ret = cos_client.upload_file(request)
print upload_file_ret
def scanPath(pathname,remotePre=''):
for root,path,filelist in os.walk(os.path.join(currentPath,pathname)):
for filename in filelist:
fullPath =unicode( os.path.join(root, filename),'utf-8')
mtime=float(os.path.getmtime(fullPath))
if fullPath in lastChange:
if mtime-lastChange[fullPath]>1:
lastChange[fullPath]=float(mtime)
remotePath=fullPath.replace(os.path.join(currentPath,pathname),'/blog/'+remotePre)
upload(remotePath,fullPath)
else:
lastChange[fullPath]=float(mtime)
remotePath=fullPath.replace(os.path.join(currentPath,pathname),'/blog/'+remotePre)
upload(remotePath,fullPath)
if not os.path.exists(os.path.join(currentPath,'static')) or not os.path.exists(os.path.join(currentPath,'blog')):
print '当前目录不存在./blog或者./static目录'
exit(1)
scanPath('blog/public/')
scanPath('static/','static/')
with open(os.path.join(currentPath,'lastchange.txt'),'w') as f2:
for key in lastChange:
f2.write('%s,%f\n' % (key.encode('utf-8'), lastChange[key]))