1. 创建自动刷新脚本

[根目录]创建一个文件名为 RefreshCDN.py
将其中的access_key='你的AccessKey',secret_key ='你的SecretKey', 替换为自己的 key
https://xxxxx/xxxxx 替换为你的博客域名

from hashlib import sha1
import hmac
import requests
import json
import urllib
import os

def dogecloud_api(api_path, data={}, json_mode=False):
    """
    调用多吉云API

    :param api_path:    调用的 API 接口地址,包含 URL 请求参数 QueryString,例如:/console/vfetch/add.json?url=xxx&a=1&b=2
    :param data:        POST 的数据,字典,例如 {'a': 1, 'b': 2},传递此参数表示不是 GET 请求而是 POST 请求
    :param json_mode:   数据 data 是否以 JSON 格式请求,默认为 false 则使用表单形式(a=1&b=2)

    :type api_path: string
    :type data: dict
    :type json_mode bool

    :return dict: 返回的数据
    """

    # 这里替换为你的多吉云永久 AccessKey 和 SecretKey,可在用户中心 - 密钥管理中查看
    # 请勿在客户端暴露 AccessKey 和 SecretKey,否则恶意用户将获得账号完全控制权
    access_key = '你的AccessKey'
    secret_key = '你的SecretKey'

    body = ''
    mime = ''
    if json_mode:
        body = json.dumps(data)
        mime = 'application/json'
    else:
        body = urllib.parse.urlencode(data) # Python 2 可以直接用 urllib.urlencode
        mime = 'application/x-www-form-urlencoded'
    sign_str = api_path + "\n" + body
    signed_data = hmac.new(secret_key.encode('utf-8'), sign_str.encode('utf-8'), sha1)
    sign = signed_data.digest().hex()
    authorization = 'TOKEN ' + access_key + ':' + sign
    response = requests.post('https://api.dogecloud.com' + api_path, data=body, headers = {
        'Authorization': authorization,
        'Content-Type': mime
    })
    return response.json()




url_list = [
    'https://xxxxx/'# xxx替换为你的博客域名
]

api = dogecloud_api('/cdn/refresh/add.json', {
    'rtype': 'path',
    'urls': json.dumps(url_list)
})
if api['code'] == 200:
    print(api['data']['task_id'])
else:
    print("api failed: " + api['msg']) # 失败

2. 创建GitHub Actions脚本

创建目录[根目录]\.github\workflows已经有的可以略过

workflows下新建RefreshCDN.yml

name: Refresh CDN

on:
  push:
    branches:
      - master

jobs:
  refresh-cdn:
    runs-on: ubuntu-latest
    steps:
      - name: 安装 Node
        uses: actions/checkout@v2
      - name: 安装 python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: 安装依赖
        run: pip install requests
      - name: 等待源站部署
        run: sleep 1m
      - name: 刷新CDN
        run: python RefreshCDN.py

这里用了个笨办法,等待 1 分钟后进行刷新
创建完后直接提交上去应该就行了

最后修改:2024 年 05 月 06 日
如果觉得我的文章对你有用,请随意赞赏