mirror of
https://github.com/hanxi/xiaomusic.git
synced 2026-05-06 00:06:11 +08:00
Auto-Generate docs 🤖
This commit is contained in:
@@ -362,5 +362,162 @@ You are receiving this because you commented.Message ID: ***@***.***>
|
||||
用这个网址的token可以登录, 里面的userid换成自己的
|
||||
https://account.xiaomi.com/fe/service/account?userId=11111&_locale=zh_CN
|
||||
|
||||
---
|
||||
|
||||
### 评论 30 - chenxixian
|
||||
|
||||
我用的pip instal方式安装和更新xiaomusic在Mac上的,之前帐号密码也是时不时有问题,改用cookie登录会正常,但Mac的IP有时会变,所以又加了个启动脚本,IP没变直接启动xiaomusic,IP变了修改好conf/setting.json后,再启动xiaomusic。
|
||||
|
||||
start_xiaomusic.sh
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
# xiaomusic启动脚本
|
||||
# 功能:检查IP配置,如不一致则更新,然后启动xiaomusic
|
||||
|
||||
# 设置脚本目录
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
SETTING_FILE="$SCRIPT_DIR/conf/setting.json"
|
||||
|
||||
# 颜色输出
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 日志函数
|
||||
log_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查配置文件是否存在
|
||||
if [ ! -f "$SETTING_FILE" ]; then
|
||||
log_error "配置文件不存在: $SETTING_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 获取当前无线网卡IP (en0)
|
||||
get_current_ip() {
|
||||
# 尝试获取en0的IPv4地址
|
||||
local ip=$(ifconfig en0 2>/dev/null | grep "inet " | awk '{print $2}' | head -1)
|
||||
if [ -z "$ip" ]; then
|
||||
# 如果en0没有IP,尝试其他网络接口
|
||||
ip=$(ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | head -1)
|
||||
fi
|
||||
echo "$ip"
|
||||
}
|
||||
|
||||
# 获取配置文件中的hostname IP
|
||||
get_config_ip() {
|
||||
local ip=$(grep '"hostname"' "$SETTING_FILE" | sed 's/.*"hostname": *"http:\/\///' | sed 's/".*//' | tr -d ' ')
|
||||
echo "$ip"
|
||||
}
|
||||
|
||||
# 更新配置文件中的IP
|
||||
update_config_ip() {
|
||||
local new_ip="$1"
|
||||
log_info "更新配置文件中的IP为: $new_ip"
|
||||
|
||||
# 使用Python来更新JSON文件,更可靠
|
||||
python3 -c "
|
||||
import json
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open('$SETTING_FILE', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
data['hostname'] = 'http://$new_ip'
|
||||
|
||||
with open('$SETTING_FILE', 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print('更新成功')
|
||||
except Exception as e:
|
||||
print(f'更新失败: {e}')
|
||||
sys.exit(1)
|
||||
"
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
log_info "IP配置更新成功"
|
||||
return 0
|
||||
else
|
||||
log_error "IP配置更新失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 启动xiaomusic
|
||||
start_xiaomusic() {
|
||||
log_info "启动xiaomusic..."
|
||||
|
||||
# 检查xiaomusic命令是否存在
|
||||
if ! command -v xiaomusic &> /dev/null; then
|
||||
log_error "未找到xiaomusic命令,请确保已正确安装"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 获取xiaomusic命令路径
|
||||
local xiaomusic_cmd=$(command -v xiaomusic)
|
||||
log_info "xiaomusic命令: $xiaomusic_cmd"
|
||||
log_info "配置文件: $SETTING_FILE"
|
||||
|
||||
# 切换到脚本目录并启动
|
||||
cd "$SCRIPT_DIR" || exit 1
|
||||
|
||||
# 执行xiaomusic命令
|
||||
exec xiaomusic
|
||||
}
|
||||
|
||||
# 主程序
|
||||
main() {
|
||||
log_info "=== xiaomusic启动脚本 ==="
|
||||
|
||||
# 获取当前IP
|
||||
current_ip=$(get_current_ip)
|
||||
if [ -z "$current_ip" ]; then
|
||||
log_error "无法获取当前IP地址"
|
||||
exit 1
|
||||
fi
|
||||
log_info "当前IP地址: $current_ip"
|
||||
|
||||
# 获取配置文件中的IP
|
||||
config_ip=$(get_config_ip)
|
||||
if [ -z "$config_ip" ]; then
|
||||
log_error "无法从配置文件中获取hostname IP"
|
||||
exit 1
|
||||
fi
|
||||
log_info "配置文件IP: $config_ip"
|
||||
|
||||
# 比较IP
|
||||
if [ "$current_ip" = "$config_ip" ]; then
|
||||
log_info "IP配置一致,直接启动xiaomusic"
|
||||
else
|
||||
log_warn "IP配置不一致,需要更新配置"
|
||||
if ! update_config_ip "$current_ip"; then
|
||||
log_error "更新IP配置失败"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 启动xiaomusic
|
||||
start_xiaomusic
|
||||
}
|
||||
|
||||
# 捕获中断信号
|
||||
trap 'log_info "脚本被中断"; exit 130' INT TERM
|
||||
|
||||
# 执行主程序
|
||||
main "$@"
|
||||
```
|
||||
|
||||
---
|
||||
[链接到 GitHub Issue](https://github.com/hanxi/xiaomusic/issues/688)
|
||||
|
||||
Reference in New Issue
Block a user