EarthLiveSharp中cloudinary的CDN图片缓存自动清理python脚本
程序员文章站
2024-03-13 11:05:27
恰巧发现有个叫“earthlivesharp”,可用将日本向日葵8号卫星的地球实时图片设为屏保。向日葵8号卫星的地球实时图片官网为:,earthlivesharp的项目地址...
恰巧发现有个叫“earthlivesharp”,可用将日本向日葵8号卫星的地球实时图片设为屏保。向日葵8号卫星的地球实时图片官网为:,earthlivesharp的项目地址是:https://github.com/bitdust/earthlivesharp。
为了减轻向日葵8号的服务器负担,同时也是提高地球实时图片的获取成功率,需要使用cloudinary来做cdn。注册配置都在软件里有说明。
目前earthlivesharp暂时没有清理cloudinary的cdn图片缓存的功能,于是我用python写了一个,并尝试用gist管理,地址为:
同时,为了大家方便,顺便编译成了windows可执行文件,下载: https://pan.baidu.com/s/1c27fxeo 提取码:k33n
为了自己检索方便,顺便在这里附上python脚本源码,可以作为python如何使用restful接口的例子,相关工具类函数也便于参考。
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