File size: 2,732 Bytes
c2b0f3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import os
import json
import sys
import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler

class DashboardHandler(BaseHTTPRequestHandler):
    def log_message(self, fmt, *args):
        pass

    def do_GET(self):
        path = self.path.split('?')[0]
        if path == '/':
            self.serve_dashboard()
        elif path == '/api/logs':
            self.serve_logs()
        elif path == '/api/stats':
            self.serve_stats()
        else:
            self.send_response(404)
            self.end_headers()

    def serve_dashboard(self):
        try:
            with open("dashboard.html", "r", encoding="utf-8") as f:
                html = f.read()
            self.send_response(200)
            self.send_header("Content-type", "text/html; charset=utf-8")
            self.send_header("Access-Control-Allow-Origin", "*")
            self.end_headers()
            self.wfile.write(html.encode('utf-8'))
        except Exception as e:
            self.send_response(500)
            self.end_headers()
            self.wfile.write(f"Error loading dashboard: {e}".encode('utf-8'))

    def serve_logs(self):
        self.send_response(200)
        self.send_header("Content-type", "application/json; charset=utf-8")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()

        log_content = ""
        log_path = os.path.join("output", "bot.log")
        if os.path.exists(log_path):
            try:
                with open(log_path, "r", encoding="utf-8") as f:
                    lines = f.readlines()
                    log_content = "".join(lines[-100:])
            except Exception:
                pass

        if not log_content:
            log_content = "No logs available."

        response = {"logs": log_content}
        self.wfile.write(json.dumps(response).encode('utf-8'))

    def serve_stats(self):
        self.send_response(200)
        self.send_header("Content-type", "application/json; charset=utf-8")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()

        response = {
            "total_posts": 0,
            "total_likes": 0,
            "total_replies": 0,
            "top_posts": []
        }
        self.wfile.write(json.dumps(response, ensure_ascii=False).encode('utf-8'))

def run_server(port):
    server = HTTPServer(("0.0.0.0", port), DashboardHandler)
    print(f"[Dashboard] Server running on port {port}")
    server.serve_forever()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Dashboard Server")
    parser.add_argument("--port", type=int, default=8080)
    args = parser.parse_args()
    run_server(args.port)