is-a / app.py
Kastg
Update app.py
c882742 verified
raw
history blame
2.51 kB
from flask import Flask, request, jsonify, send_from_directory
import os
import requests
import json
import psutil
import time
import datetime
app = Flask(__name__)
# Define the path to static files
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
@app.route('/')
def index():
return send_from_directory(static_dir, 'index.html')
@app.route('/ai')
def ai():
return send_from_directory(static_dir, 'views/ai.html')
@app.route('/info')
def info():
ip = request.remote_addr
current_time = datetime.datetime.now().strftime("%H:%M:%S")
return jsonify({'ip': ip, 'current_time': current_time})
# Define the visitor count routes
visitor_count = 0
visitor_today = 0
last_update_date = datetime.datetime.now().date()
@app.before_request
def update_visitor_counts():
global visitor_count, visitor_today, last_update_date
allowed_paths = ['/ai', '/api', '/tool']
if request.path.startswith(tuple(allowed_paths)):
current_date = datetime.datetime.now().date()
if current_date != last_update_date:
visitor_today = 0
last_update_date = current_date
visitor_count += 1
visitor_today += 1
@app.route('/count')
def count():
return jsonify({'visitor_count': visitor_count, 'visitor_today': visitor_today})
# Define the status route
@app.route('/status')
def status():
uptime_seconds = int(time.time() - psutil.boot_time())
uptime = str(datetime.timedelta(seconds=uptime_seconds))
memory_free = psutil.virtual_memory().available
memory_total = psutil.virtual_memory().total
return jsonify({'runtime': uptime, 'memory': f'{memory_free} / {memory_total}'})
# Handle 404 errors
@app.errorhandler(404)
def page_not_found(e):
return send_from_directory(static_dir, '404.html'), 404
@app.route("/uptime")
def uptime():
# Get system uptime using psutil
uptime_seconds = int(time.time() - psutil.boot_time())
uptime_string = str(datetime.timedelta(seconds=uptime_seconds))
return jsonify({"uptime": uptime_string})
@app.route("/ping")
def ping():
# Ping an external service
try:
response = requests.get("https://example.com") # Change this to your desired external service
response.raise_for_status()
return jsonify({"ping": "success"})
except requests.exceptions.RequestException as e:
return jsonify({"ping": f"failed: {str(e)}"}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)