使用方法

使用示例

curl


#!/bin/bash

# 定义全局变量
info_token="your_token"
info_name="your_name"
info_msg="your_msg"

update_info() {
    local history_file="/tmp/info_history_${info_name}"

    # 检查并创建历史文件(如果不存在)
    if [ ! -f "$history_file" ]; then
        touch "$history_file" || {
            echo "无法创建历史文件,程序退出."
            return 1
        }
    fi

    # 安全读取历史文件内容
    local history_content=$(cat "$history_file" 2>/dev/null)
    if [ $? -ne 0 ]; then
        echo "无法读取历史文件内容,程序退出."
        return 1
    fi

    # 比较当前消息与历史内容
    if [ "$info_msg" == "$history_content" ]; then
        echo "信息内容与历史文件内容一致,程序退出."
        return 0
    fi

    # 发送请求
    local response=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
        -H "Authorization: ${info_token}" \
        -d "${info_msg}" "https://info.zshot.top/set?name=${info_name}")

    # 检查请求是否成功
    if [ "$response" -eq 200 ]; then
        # 更新历史文件
        echo "$info_msg" >"$history_file" || {
            echo "无法更新历史文件,程序退出."
            return 1
        }
    else
        # 记录错误日志
        echo "请求失败,HTTP状态码: $response" >&2
        return 1
    fi
}

# 调用函数
info_msg="123"
update_info
    

Python


# 依赖 requests
def load_script_info_updater(token, enable_history=True):
    import os
    import requests
    import hashlib

    script_name = 'info_updater.py'
    script_dir = 'net_script'
    script_path = os.path.join(script_dir, script_name)
    if not os.path.exists(script_dir):
        os.makedirs(script_dir)

    # MD5 校验
    r = requests.get('https://cdn.zshot.top/script/md5.json', timeout=10)
    online_md5 = r.json()[script_name]  # 远程MD5

    esixt_md5 = ''
    if os.path.exists(script_path):
        with open(script_path, 'rb') as f:
            content = f.read()
            esixt_md5 = hashlib.md5(content).hexdigest()

    if online_md5.upper() != esixt_md5.upper():
        r = requests.get(f'https://cdn.zshot.top/script/{script_name}', timeout=10)
        with open(script_path, 'wb') as f:
            f.write(r.text.encode())

    from net_script.info_updater import InfoUpdater
    return InfoUpdater(token, enable_history)
    

# 加载info_updater.py
api = load_script_info_updater('test')
api.update_info('name', 'info')
    

JavaScript (fetch)


const url = "https://info.zshot.top/set?name=your_name";
const headers = { "Authorization": "xxx" };
const data = "Your info here";

fetch(url, {
  method: "POST",
  headers: headers,
  body: data,
})
.then(response => {
  if (response.ok) {
    console.log("Info updated successfully");
  } else {
    console.error(\`Error: \${response.status} - \${response.statusText}\`);
  }
});