Files
UpdateServer/app.py
T

96 lines
2.4 KiB
Python
Raw Normal View History

2026-03-11 19:43:33 +00:00
#!/usr/bin/env python3
"""
Update Server for Bots
Serves version information to bots checking for updates.
"""
from flask import Flask, jsonify, request
import json
import os
from datetime import datetime
app = Flask(__name__)
# Load version data from JSON file
VERSIONS_FILE = 'versions.json'
LOG_FILE = 'update_checks.log'
def load_versions():
"""Load version info from file."""
if os.path.exists(VERSIONS_FILE):
try:
with open(VERSIONS_FILE, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading versions.json: {e}")
return {}
return {}
def log_check(bot_name):
"""Log update check requests."""
try:
with open(LOG_FILE, 'a') as f:
f.write(f"{datetime.now().isoformat()} - {bot_name}\n")
except Exception as e:
print(f"Error logging check: {e}")
@app.route('/api/version/<bot_name>', methods=['GET'])
def get_version(bot_name):
"""Get latest version for a specific bot."""
log_check(bot_name)
versions = load_versions()
if bot_name in versions:
return jsonify(versions[bot_name])
return jsonify({"error": "Bot not found"}), 404
@app.route('/api/versions', methods=['GET'])
def get_all_versions():
"""Get all bot versions."""
return jsonify(load_versions())
@app.route('/health', methods=['GET'])
def health():
"""Health check endpoint."""
return jsonify({"status": "ok"})
@app.route('/', methods=['GET'])
def index():
"""Index page with server info."""
versions = load_versions()
bot_count = len(versions)
return jsonify({
"service": "Update Server",
"status": "running",
"bots_tracked": bot_count,
"endpoints": {
"/health": "Health check",
"/api/versions": "Get all bot versions",
"/api/version/<bot_name>": "Get specific bot version"
}
})
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors."""
return jsonify({"error": "Endpoint not found"}), 404
@app.errorhandler(500)
def internal_error(error):
"""Handle 500 errors."""
return jsonify({"error": "Internal server error"}), 500
if __name__ == '__main__':
print("Starting Update Server...")
print(f"Loaded {len(load_versions())} bots from versions.json")
app.run(host='0.0.0.0', port=5000, debug=False)