SoFunction
Updated on 2025-03-03

Automatically clean up python scripts for cloudinary CDN image cache in EarthLiveSharp

It happened to be discovered that there is a "EarthLiveSharp", which can set the real-time picture of the Earth of the Japanese Sunflower 8 satellite as a screensaver. The official website of the Earth's real-time picture of Sunflower 8 satellite is:/, the project address of EarthLiveSharp is:/bitdust/EarthLiveSharp
In order to reduce the server burden of Sunflower 8 and also improve the success rate of obtaining real-time pictures on the earth, cloudinary is needed to make CDN. Registration and configuration are all explained in the software.

At present, EarthLiveSharp does not have the function to clean up cloudinary's CDN image cache yet, so I wrote one in python and tried to manage it with gist, with the address:/creke/c5a8a18fa41b8f5c1a0719a7e0cf4de6

At the same time, for everyone's convenience, it was compiled into a Windows executable file by the way. Download: /s/1c27fXEo Extraction code: k33n
For the convenience of retrieval by yourself, the source code of python scripts is attached here, which can be used as an example of how to use RESTful interface in python, and related tool-like functions are also easy to refer to.

# -*- coding: utf-8 -*-
# Author: Creke
# HomePage: 

import sys
import urllib, urllib2
import base64
import json

URLLIB_DEBUG_LEVEL = 1
URLLIB_TIMEOUT = 5

def Dict2Uri(dic):
  return (dic)

def GenUrllibReq(url, method, api_key, api_secret, post_data=None):
  urlreq = None
  if post_data is None:
    urlreq = (url)
  else:
    urlreq = (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 = "/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((debuglevel=URLLIB_DEBUG_LEVEL))
    urllib_open = (urlreq, timeout=URLLIB_TIMEOUT)
    response = urllib_open.read()
    print "==========================="
    print "Response:"
    print "%s" % (response)
    print "==========================="
    urllib_open.close()
    print "Done Requesting"

    res_json = (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() < 4:
    PrintHelp()
    exit(1)
  print "RUNNING main"
  main()
  print "DONE main"

# -*- coding: utf-8 -*-
# Author: Creke
# HomePage: 

from  import setup
import py2exe

setup(console=[''])

Download the Windows compiled version:

Link:/s/1skADZeHPassword: rdgb