feat: 添加 weread 启停命令 & 修复 macOS SSL 证书问题

- 新增 weread CLI 脚本,支持 weread start/stop [port] 一键管理服务
- app.py 端口改为从环境变量 WEREAD_PORT 读取,默认 8080
- 修复 macOS Python urllib 调用微信读书 API 时 SSL 证书验证失败问题(引入 certifi)
This commit is contained in:
admin committed 2026-07-17 14:34:35 +08:00
1 parent c3f5c0ff03
commit 5608562f02
2 files changed
+113 -3

No files matched your search

+11 -3
View File
@@ -18,6 +18,7 @@
import json
import os
import ssl
import subprocess
import time
import urllib.request
@@ -25,6 +26,12 @@ from datetime import datetime
from functools import wraps
import pymysql
try:
import certifi
_ssl_context = ssl.create_default_context(cafile=certifi.where())
except ImportError:
_ssl_context = ssl.create_default_context()
from flask import Flask, jsonify, render_template, request
# ======================================================================
@@ -158,7 +165,7 @@ def weread_api_call(api_name, **kwargs):
},
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as resp:
with urllib.request.urlopen(req, timeout=30, context=_ssl_context) as resp:
return json.loads(resp.read().decode("utf-8"))
@@ -1270,5 +1277,6 @@ if __name__ == "__main__":
else:
print(f"[信息] {msg}")
# 启动 Flask 服务,监听 8080 端口
app.run(host="0.0.0.0", port=8080, debug=False)
# 启动 Flask 服务,端口从环境变量 WEREAD_PORT 读取,默认 8080
port = int(os.environ.get("WEREAD_PORT", 8080))
app.run(host="0.0.0.0", port=port, debug=False)
Executable
+102
View File
@@ -0,0 +1,102 @@
#!/bin/bash
# weread - 微信读书数据看板 服务管理命令
# 用法:
# weread start [port] 启动服务(默认端口 8080)
# weread stop [port] 停止服务(默认端口 8080)
PROJECT_DIR="/Users/hwj/Codes/MyProjects/WeReadBoard"
PID_FILE="$PROJECT_DIR/.server.pid"
LOG_FILE="/tmp/weread_server.log"
DEFAULT_PORT=8080
get_port() {
echo "${1:-$DEFAULT_PORT}"
}
get_pid() {
local port="$1"
if [ -f "$PID_FILE" ]; then
local saved_port
saved_port=$(sed -n '2p' "$PID_FILE" 2>/dev/null)
local pid
pid=$(head -1 "$PID_FILE" 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return
fi
fi
lsof -i :"$port" -t 2>/dev/null | head -1
}
is_running() {
local port="$1"
if [ -n "$(get_pid "$port")" ]; then
return 0
fi
return 1
}
cmd_start() {
local port
port=$(get_port "$1")
if is_running "$port"; then
echo "❌ 服务已在运行 (PID: $(get_pid "$port"), 端口: $port)"
exit 1
fi
echo "启动服务(端口: $port)..."
WEREAD_PORT="$port" nohup python3 "$PROJECT_DIR/app.py" > "$LOG_FILE" 2>&1 &
local pid=$!
printf '%s\n%s\n' "$pid" "$port" > "$PID_FILE"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
echo "✅ 服务已启动"
echo " PID: $pid"
echo " 端口: $port"
echo " 访问: http://127.0.0.1:$port"
echo " 日志: $LOG_FILE"
else
echo "❌ 启动失败,请查看日志: $LOG_FILE"
cat "$LOG_FILE" 2>/dev/null
rm -f "$PID_FILE"
exit 1
fi
}
cmd_stop() {
local port
port=$(get_port "$1")
if ! is_running "$port"; then
echo "服务未运行(端口: $port)"
exit 0
fi
local pid
pid=$(get_pid "$port")
echo "停止服务 (PID: $pid, 端口: $port)..."
kill "$pid" 2>/dev/null
sleep 1
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null
fi
rm -f "$PID_FILE"
echo "✅ 已停止"
}
usage() {
echo "用法:"
echo " weread start [port] 启动服务(默认端口 8080)"
echo " weread stop [port] 停止服务(默认端口 8080)"
exit 1
}
case "${1}" in
start) cmd_start "$2" ;;
stop) cmd_stop "$2" ;;
*) usage ;;
esac