EarthLiveSharp自动清理Cloudinary CDN缓存的Python脚本

小石头
小石头
发布时间:2026-10-13 | 3人浏览过
原创

EarthLiveSharp利用日本向日葵8号卫星实时图片制作屏保,为减轻源服务器压力并提高获取成功率,项目推荐使用Cloudinary作为CDN。针对该软件目前缺乏自动清理CDN缓存的功能,作者编写了Python脚本并通过Gist开源。文章提供了Windows可执行文件下载链接及Python源码,展示了如何利用RESTful API实现自动化清理,并附带编译脚本供参考。

EarthLiveSharp是一款可将日本向日葵8号卫星的地球实时图像设置为电脑屏保的工具。向日葵8号卫星的实时图像官方获取地址为:http://himawari8.nict.go.jp/,而EarthLiveSharp的项目源码托管于:https://github.com/bitdust/EarthLiveSharp。为了降低向日葵8号官方服务器的负载,同时提升获取实时图像的成功率,建议在软件中使用Cloudinary进行CDN加速。具体的注册与配置步骤在软件内部均有详细说明。

鉴于EarthLiveSharp当前版本尚未内置清理Cloudinary CDN图片缓存的功能,作者使用Python编写了一个自动化清理脚本,并尝试通过Gist进行代码管理。该脚本的访问地址为:https://gist.github.com/creke/c5a8a18fa41b8f5c1a0719a7e0cf4de6。

为了方便广大用户直接使用,作者已将Python脚本编译为Windows可执行文件。下载地址为:https://pan.baidu.com/s/1c27fXEo,提取码为:k33n。此外,为了便于检索和参考,这里附上了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

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表