Last active
March 9, 2026 12:06
-
-
Save growvv/4f9b01fcd1324a4b0ffd67cb73b33a87 to your computer and use it in GitHub Desktop.
cy.sh: fetch and print today usage from ai.changyou.club
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| print_help() { | |
| cat <<'EOF' | |
| 用法: | |
| CY_API_KEY='<你的API Key>' ./cy.sh | |
| ./cy.sh '<你的API Key>' | |
| ./cy.sh help | |
| ./cy.sh -h | |
| ./cy.sh --help | |
| 说明: | |
| - 脚本会登录 ai.changyou.club 并查询今日消耗 | |
| - 可通过 CY_BASE_URL 覆盖默认地址 | |
| EOF | |
| } | |
| if [[ "${1:-}" == "help" || "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then | |
| print_help | |
| exit 0 | |
| fi | |
| BASE_URL="${CY_BASE_URL:-https://ai.changyou.club}" | |
| API_KEY="${CY_API_KEY:-${1:-}}" | |
| if [[ -z "${API_KEY}" ]]; then | |
| print_help | |
| exit 1 | |
| fi | |
| if ! command -v curl >/dev/null 2>&1; then | |
| echo "错误: 未找到 curl" | |
| exit 1 | |
| fi | |
| if ! command -v jq >/dev/null 2>&1; then | |
| echo "错误: 未找到 jq,请先安装 jq" | |
| exit 1 | |
| fi | |
| login_resp="$(curl -sS "${BASE_URL}/api/user/auth/login" \ | |
| -H 'Content-Type: application/json' \ | |
| --data "{\"apiKey\":\"${API_KEY}\"}")" | |
| login_code="$(jq -r '.code // empty' <<<"${login_resp}")" | |
| if [[ "${login_code}" != "200" ]]; then | |
| login_msg="$(jq -r '.message // "登录失败"' <<<"${login_resp}")" | |
| echo "登录失败: ${login_msg}" | |
| exit 1 | |
| fi | |
| token="$(jq -r '.data.token // empty' <<<"${login_resp}")" | |
| if [[ -z "${token}" ]]; then | |
| echo "登录失败: 未获取到 token" | |
| exit 1 | |
| fi | |
| summary_resp="$(curl -sS "${BASE_URL}/api/user/usage/summary?period=today" \ | |
| -H "Authorization: Bearer ${token}")" | |
| summary_code="$(jq -r '.code // empty' <<<"${summary_resp}")" | |
| if [[ "${summary_code}" != "200" ]]; then | |
| summary_msg="$(jq -r '.message // "获取今日统计失败"' <<<"${summary_resp}")" | |
| echo "获取今日统计失败: ${summary_msg}" | |
| exit 1 | |
| fi | |
| total_cost_raw="$(jq -r '.data.totalCost // 0' <<<"${summary_resp}")" | |
| total_cost="$(awk -v v="${total_cost_raw}" 'BEGIN { printf "%.2f", v + 0 }')" | |
| if [[ -t 1 ]]; then | |
| HIGHLIGHT_START=$'\033[1;33m' | |
| HIGHLIGHT_END=$'\033[0m' | |
| else | |
| HIGHLIGHT_START="" | |
| HIGHLIGHT_END="" | |
| fi | |
| echo "今日消耗: $ ${HIGHLIGHT_START}${total_cost}${HIGHLIGHT_END}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment