EarthLiveCleanCloudinary.py
· 2.3 KiB · Python
Raw
# -*- 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"
| 1 | # -*- coding: utf-8 -*- |
| 2 | # Author: Creke |
| 3 | # HomePage: http://blog.creke.net |
| 4 | |
| 5 | import sys |
| 6 | import urllib, urllib2 |
| 7 | import base64 |
| 8 | import json |
| 9 | |
| 10 | URLLIB_DEBUG_LEVEL = 1 |
| 11 | URLLIB_TIMEOUT = 5 |
| 12 | |
| 13 | def Dict2Uri(dic): |
| 14 | return urllib.urlencode(dic) |
| 15 | |
| 16 | def GenUrllibReq(url, method, api_key, api_secret, post_data=None): |
| 17 | urlreq = None |
| 18 | if post_data is None: |
| 19 | urlreq = urllib2.Request(url) |
| 20 | else: |
| 21 | urlreq = urllib2.Request(url, post_data) |
| 22 | urlreq.get_method = lambda: method |
| 23 | auth_str = base64.b64encode('%s:%s' % (api_key, api_secret)) |
| 24 | urlreq.add_header("Authorization", "Basic %s" % auth_str) |
| 25 | urlreq.add_header('Cache-Control', 'no-cache') |
| 26 | return urlreq |
| 27 | |
| 28 | def GetApiDelUrl(cloud_name, img_type): |
| 29 | url = "https://api.cloudinary.com/v1_1/%s/resources/image/%s" % (cloud_name, img_type) |
| 30 | params = {"prefix": "http://himawari8-dl"} |
| 31 | url = url + "?" + Dict2Uri(params) |
| 32 | return url |
| 33 | |
| 34 | def main(argv): |
| 35 | arg_idx = 1 |
| 36 | api_key = argv[arg_idx] |
| 37 | arg_idx += 1 |
| 38 | api_secret = argv[arg_idx] |
| 39 | arg_idx += 1 |
| 40 | cloud_name = argv[arg_idx] |
| 41 | |
| 42 | while True: |
| 43 | del_url = GetApiDelUrl(cloud_name, 'fetch') |
| 44 | urlreq = GenUrllibReq(del_url, 'DELETE', api_key, api_secret) |
| 45 | |
| 46 | print "===========================" |
| 47 | print "Requesting %s" % (del_url) |
| 48 | opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=URLLIB_DEBUG_LEVEL)) |
| 49 | urllib_open = opener.open(urlreq, timeout=URLLIB_TIMEOUT) |
| 50 | response = urllib_open.read() |
| 51 | print "===========================" |
| 52 | print "Response:" |
| 53 | print "%s" % (response) |
| 54 | print "===========================" |
| 55 | urllib_open.close() |
| 56 | print "Done Requesting" |
| 57 | |
| 58 | res_json = json.loads(response) |
| 59 | deleted_cnt = len(res_json['deleted']) |
| 60 | print "Deleted %u himawari8 pics" % (deleted_cnt) |
| 61 | print "===========================" |
| 62 | if 'next_cursor' in res_json and deleted_cnt>0: |
| 63 | print "Due to Cloudinary limits, we're starting a new round" |
| 64 | else: |
| 65 | break |
| 66 | |
| 67 | return 0 |
| 68 | |
| 69 | def PrintHelp(argv): |
| 70 | print "\t USAGE: %s [api_key] [api_secret] [cloud_name]" % (argv[0]) |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | if len(sys.argv) < 4: |
| 74 | PrintHelp(sys.argv) |
| 75 | exit(1) |
| 76 | print "RUNNING main" |
| 77 | main(sys.argv) |
| 78 | print "DONE main" |
EarthLiveCleanCloudinary2exe.py
· 169 B · Python
Raw
# -*- coding: utf-8 -*-
# Author: Creke
# HomePage: http://blog.creke.net
from distutils.core import setup
import py2exe
setup(console=['EarthLiveCleanCloudinary.py'])
| 1 | # -*- coding: utf-8 -*- |
| 2 | # Author: Creke |
| 3 | # HomePage: http://blog.creke.net |
| 4 | |
| 5 | from distutils.core import setup |
| 6 | import py2exe |
| 7 | |
| 8 | setup(console=['EarthLiveCleanCloudinary.py']) |
《EarthLiveSharp中cloudinary的CDN图片缓存自动清理》
http://blog.creke.net/823.html
Windows编译版本下载:
https://pan.baidu.com/s/1c27fXEo 提取码:k33n