Files
ZDXX/1.1/test1.py

151 lines
5.1 KiB
Python
Raw Normal View History

2026-01-06 15:27:10 +08:00
import os
import json
import threading
import requests
from datetime import datetime
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from lxml import etree
app = Flask(__name__)
2026-01-06 15:36:42 +08:00
CORS(app)
2026-01-06 15:27:10 +08:00
2026-01-06 15:36:42 +08:00
# --- 数据库配置 ---
2026-01-06 15:27:10 +08:00
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///monitor_data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class ErrorLog(db.Model):
id = db.Column(db.Integer, primary_key=True)
2026-01-06 15:36:42 +08:00
source = db.Column(db.String(50))
name = db.Column(db.String(100))
reason = db.Column(db.String(255))
offset = db.Column(db.String(50))
latest_time = db.Column(db.String(50))
check_time = db.Column(db.String(50))
content = db.Column(db.Text, nullable=True)
2026-01-06 15:27:10 +08:00
with app.app_context():
db.create_all()
CONFIG = {
"106": {
"base_url": "http://106.75.72.40:7500/api/proxy/tcp",
"primary_auth": "Basic YWRtaW46bGljYWhr",
"login_payload": {"username": "admin", "password": "licahk", "recaptcha": ""}
},
"82": {
"base_url": "http://82.156.1.111/weather/php",
"login": {'username': 'renlixin', 'password': 'licahk', 'login': '123'}
}
}
2026-01-06 15:36:42 +08:00
is_running = False
2026-01-06 15:27:10 +08:00
def add_error_to_db(source, name, reason, latest_time="N/A", content=None):
days_diff = "N/A"
if latest_time and latest_time != "N/A":
try:
clean_date_str = str(latest_time).split()[0].replace('_', '-')
target_date = datetime.strptime(clean_date_str, "%Y-%m-%d").date()
2026-01-06 15:36:42 +08:00
diff = (datetime.now().date() - target_date).days
2026-01-06 15:27:10 +08:00
days_diff = f"滞后 {diff} 天" if diff > 0 else "当天已同步"
except:
days_diff = "解析失败"
log = ErrorLog(
2026-01-06 15:36:42 +08:00
source=source, name=name, reason=reason, offset=days_diff,
latest_time=latest_time, content=content,
check_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2026-01-06 15:27:10 +08:00
)
db.session.add(log)
2026-01-06 15:36:42 +08:00
# --- 106 逻辑 ---
def get_106_token(port):
2026-01-06 15:27:10 +08:00
try:
2026-01-06 15:36:42 +08:00
resp = requests.post(f"http://106.75.72.40:{port}/api/login", json=CONFIG["106"]["login_payload"], timeout=5)
return resp.text.strip().replace('"', '') if resp.status_code == 200 else None
2026-01-06 15:27:10 +08:00
except:
2026-01-06 15:36:42 +08:00
return None
2026-01-06 15:27:10 +08:00
def run_106_logic():
c = CONFIG["106"]
today_str = datetime.now().strftime("%Y_%m_%d")
try:
2026-01-06 15:36:42 +08:00
resp = requests.get(c["base_url"], headers={"Authorization": c["primary_auth"]}, timeout=10)
2026-01-06 15:27:10 +08:00
for item in resp.json().get('proxies', []):
2026-01-06 15:36:42 +08:00
name = item.get('name', '')
2026-01-06 15:27:10 +08:00
if not name.lower().endswith('_data'): continue
2026-01-06 15:36:42 +08:00
if str(item.get('status')).lower() != 'online':
add_error_to_db("106网站", name, f"离线({item.get('status')})")
2026-01-06 15:27:10 +08:00
continue
2026-01-06 15:36:42 +08:00
# 此处应包含 106 具体的 JSON 内容爬取逻辑,拿到 content 字符串
# 示例:add_error_to_db("106网站", name, "运行正常", today_str, content=raw_json_str)
add_error_to_db("106网站", name, "同步成功", today_str, content='{"info": "106数据报文示例"}')
2026-01-06 15:27:10 +08:00
except Exception as e:
2026-01-06 15:36:42 +08:00
add_error_to_db("106网站", "全局错误", str(e))
2026-01-06 15:27:10 +08:00
2026-01-06 15:36:42 +08:00
# --- 82 逻辑 ---
2026-01-06 15:27:10 +08:00
def run_82_logic():
c = CONFIG["82"]
session = requests.Session()
today_fmt = datetime.now().strftime("%Y-%m-%d")
try:
session.post(f"{c['base_url']}/login.php", data=c["login"], timeout=10)
resp = session.post(f"{c['base_url']}/GetStationList.php", timeout=10)
stations = etree.HTML(resp.content).xpath('//option/@value')
2026-01-06 15:36:42 +08:00
for sid in [s for s in stations if s]:
2026-01-06 15:27:10 +08:00
try:
r = session.post(f"{c['base_url']}/getLastWeatherData.php", data=str(sid),
headers={'Content-Type': 'text/plain'}, timeout=10)
data = r.json()
2026-01-06 15:36:42 +08:00
if data:
d_list = data.get('date', [])
latest = str(d_list[-1]) if d_list else "N/A"
add_error_to_db("82网站", sid, "同步成功", latest, content=json.dumps(data, ensure_ascii=False))
except:
add_error_to_db("82网站", sid, "采集异常")
2026-01-06 15:27:10 +08:00
except Exception as e:
2026-01-06 15:36:42 +08:00
add_error_to_db("82网站", "初始化错误", str(e))
2026-01-06 15:27:10 +08:00
2026-01-06 15:36:42 +08:00
def background_task():
2026-01-06 15:27:10 +08:00
global is_running
with app.app_context():
2026-01-06 15:36:42 +08:00
ErrorLog.query.delete()
run_106_logic()
run_82_logic()
db.session.commit()
is_running = False
2026-01-06 15:27:10 +08:00
@app.route('/api/run', methods=['POST'])
2026-01-06 15:36:42 +08:00
def start():
2026-01-06 15:27:10 +08:00
global is_running
2026-01-06 15:36:42 +08:00
if is_running: return jsonify({"status": "busy"}), 400
2026-01-06 15:27:10 +08:00
is_running = True
2026-01-06 15:36:42 +08:00
threading.Thread(target=background_task).start()
return jsonify({"status": "started"})
2026-01-06 15:27:10 +08:00
2026-01-06 15:36:42 +08:00
@app.route('/api/status')
def status(): return jsonify({"is_running": is_running})
2026-01-06 15:27:10 +08:00
2026-01-06 15:36:42 +08:00
@app.route('/api/logs')
def logs():
data = ErrorLog.query.all()
return jsonify([{"source": l.source, "name": l.name, "reason": l.reason, "offset": l.offset,
"latest_time": l.latest_time, "check_time": l.check_time, "content": l.content} for l in data])
2026-01-06 15:27:10 +08:00
if __name__ == "__main__":
2026-01-06 15:36:42 +08:00
app.run(debug=True, port=5000)