EarthLiveSharp中cloudinary的CDN圖片緩存自動清理python腳本
恰巧發(fā)現(xiàn)有個叫“EarthLiveSharp”,可用將日本向日葵8號衛(wèi)星的地球?qū)崟r圖片設(shè)為屏保。向日葵8號衛(wèi)星的地球?qū)崟r圖片官網(wǎng)為:http://himawari8.nict.go.jp/,EarthLiveSharp的項目地址是:https://github.com/bitdust/EarthLiveSharp。
為了減輕向日葵8號的服務(wù)器負擔,同時也是提高地球?qū)崟r圖片的獲取成功率,需要使用cloudinary來做CDN。注冊配置都在軟件里有說明。
目前EarthLiveSharp暫時沒有清理cloudinary的CDN圖片緩存的功能,于是我用python寫了一個,并嘗試用gist管理,地址為:https://gist.github.com/creke/c5a8a18fa41b8f5c1a0719a7e0cf4de6
同時,為了大家方便,順便編譯成了Windows可執(zhí)行文件,下載: https://pan.baidu.com/s/1c27fXEo 提取碼:k33n
為了自己檢索方便,順便在這里附上python腳本源碼,可以作為python如何使用RESTful接口的例子,相關(guān)工具類函數(shù)也便于參考。
EarthLiveCleanCloudinary.py
# -*- coding: utf-8 -*-
# Author: Creke
# HomePage: http://blog.creke.net
import sys
import urllib, urllib2
import base64
import json
URLLIB_DEBUG_LEVEL = 1
URLLIB_TIMEOUT = 5
def Dict2Uri(dic):
return urllib.urlencode(dic)
def GenUrllibReq(url, method, api_key, api_secret, post_data=None):
urlreq = None
if post_data is None:
urlreq = urllib2.Request(url)
else:
urlreq = urllib2.Request(url, post_data)
urlreq.get_method = lambda: method
auth_str = base64.b64encode('%s:%s' % (api_key, api_secret))
urlreq.add_header("Authorization", "Basic %s" % auth_str)
urlreq.add_header('Cache-Control', 'no-cache')
return urlreq
def GetApiDelUrl(cloud_name, img_type):
url = "https://api.cloudinary.com/v1_1/%s/resources/image/%s" % (cloud_name, img_type)
params = {"prefix": "http://himawari8-dl"}
url = url + "?" + Dict2Uri(params)
return url
def main(argv):
arg_idx = 1
api_key = argv[arg_idx]
arg_idx += 1
api_secret = argv[arg_idx]
arg_idx += 1
cloud_name = argv[arg_idx]
while True:
del_url = GetApiDelUrl(cloud_name, 'fetch')
urlreq = GenUrllibReq(del_url, 'DELETE', api_key, api_secret)
print "==========================="
print "Requesting %s" % (del_url)
opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=URLLIB_DEBUG_LEVEL))
urllib_open = opener.open(urlreq, timeout=URLLIB_TIMEOUT)
response = urllib_open.read()
print "==========================="
print "Response:"
print "%s" % (response)
print "==========================="
urllib_open.close()
print "Done Requesting"
res_json = json.loads(response)
deleted_cnt = len(res_json['deleted'])
print "Deleted %u himawari8 pics" % (deleted_cnt)
print "==========================="
if 'next_cursor' in res_json and deleted_cnt>0:
print "Due to Cloudinary limits, we're starting a new round"
else:
break
return 0
def PrintHelp(argv):
print "\t USAGE: %s [api_key] [api_secret] [cloud_name]" % (argv[0])
if __name__ == '__main__':
if len(sys.argv) < 4:
PrintHelp(sys.argv)
exit(1)
print "RUNNING main"
main(sys.argv)
print "DONE main"
EarthLiveCleanCloudinary2exe.py
# -*- coding: utf-8 -*- # Author: Creke # HomePage: http://blog.creke.net from distutils.core import setup import py2exe setup(console=['EarthLiveCleanCloudinary.py'])
Windows編譯版本下載:
鏈接: https://pan.baidu.com/s/1skADZeH 密碼: rdgb
相關(guān)文章
PHP實現(xiàn)將多個文件中的內(nèi)容合并為新文件的方法示例
這篇文章主要介紹了PHP實現(xiàn)將多個文件中的內(nèi)容合并為新文件的方法,涉及php編碼轉(zhuǎn)換、文件與目錄的遍歷以及文件讀寫相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進后退等功能
這篇文章主要介紹了php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進后退等功能,較為詳細的分析了php使用歷史記錄功能所涉及的相關(guān)技巧與實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
php通過ajax實現(xiàn)雙擊table修改內(nèi)容
這篇文章主要介紹了php通過如何ajax實現(xiàn)雙擊table修改內(nèi),需要的朋友可以參考下2014-04-04

