Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
EarthLiveSharp 清理cloudinary CDN缓存
# -*- 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"
# -*- coding: utf-8 -*-
# Author: Creke
# HomePage: http://blog.creke.net
from distutils.core import setup
import py2exe
setup(console=['EarthLiveCleanCloudinary.py'])

li360 commented Mar 16, 2017

出现错误:

...
...
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Cache-Control: no-cache
header: Content-Type: application/json; charset=utf-8
header: Date: Thu, 16 Mar 2017 05:40:34 GMT
header: Server: cloudinary
header: Status: 401 Unauthorized
header: WWW-Authenticate: Basic realm="Cloudinary"
header: X-Cld-Error: disabled account
header: X-Request-Id: a1b8a7f3e076c220
header: X-UA-Compatible: IE=Edge,chrome=1
header: Content-Length: 40
header: Connection: Close
Traceback (most recent call last):
File "EarthLiveCleanCloudinary.py", line 77, in
File "EarthLiveCleanCloudinary.py", line 49, in main
File "urllib2.pyc", line 437, in open
File "urllib2.pyc", line 550, in http_response
File "urllib2.pyc", line 475, in error
File "urllib2.pyc", line 409, in _call_chain
File "urllib2.pyc", line 558, in http_error_default
urllib2.HTTPError: HTTP Error 401: Unauthorized

是我什么没搞对吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment