Update fastapi_app.py
Browse files- fastapi_app.py +128 -1
fastapi_app.py
CHANGED
@@ -1,42 +1,169 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException, Depends
|
|
|
|
|
|
|
|
|
|
|
2 |
from pydantic import BaseModel, Field
|
|
|
3 |
from bot import Bot, temp
|
|
|
4 |
from database.users_chats_db import db
|
|
|
5 |
from database.ia_filterdb import Media, get_file_details, get_search_results
|
|
|
6 |
from database.connections_mdb import add_connection, active_connection, all_connections, if_active, make_active, make_inactive, delete_connection
|
|
|
7 |
from database.filters_mdb import add_filter, find_filter, get_filters, delete_filter, del_all, count_filters, filter_stats
|
|
|
8 |
from database.gfilters_mdb import add_gfilter, find_gfilter, get_gfilters, delete_gfilter, del_allg, count_gfilters, gfilter_stats
|
|
|
9 |
from utils import get_settings, save_group_settings, humanbytes, get_size, extract_user, get_file_id, get_poster, parser, get_shortlink, extract_time, admin_check
|
|
|
10 |
from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG, ADMINS, CHANNELS
|
|
|
11 |
from pyrogram import Client, filters, enums, types
|
|
|
12 |
from pyrogram.errors import ChatAdminRequired, UserIsBlocked, MessageNotModified, PeerIdInvalid, FloodWait
|
|
|
13 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message, CallbackQuery
|
|
|
14 |
from telegraph import upload_file
|
|
|
15 |
from io import BytesIO
|
|
|
16 |
import aiohttp
|
|
|
17 |
import asyncio
|
|
|
18 |
import requests
|
|
|
19 |
import os
|
|
|
20 |
import logging
|
|
|
21 |
import re
|
|
|
22 |
import traceback
|
|
|
23 |
import json
|
|
|
24 |
from googletrans import Translator
|
|
|
25 |
from gtts import gTTS
|
|
|
26 |
from youtube_search import YoutubeSearch
|
|
|
27 |
from youtubesearchpython import SearchVideos
|
|
|
28 |
from yt_dlp import YoutubeDL
|
|
|
29 |
import shutil
|
|
|
30 |
import psutil
|
|
|
31 |
import wget
|
|
|
32 |
from datetime import datetime, timedelta
|
|
|
33 |
from urllib.parse import quote
|
|
|
34 |
from telegraph import upload_file
|
|
|
35 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
36 |
import textwrap
|
37 |
|
|
|
|
|
|
|
38 |
app = FastAPI()
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Shared instance of the Bot class
|
41 |
bot_instance = Bot()
|
42 |
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Depends, Request
|
2 |
+
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
|
5 |
+
from fastapi.templating import Jinja2Templates
|
6 |
+
|
7 |
from pydantic import BaseModel, Field
|
8 |
+
|
9 |
from bot import Bot, temp
|
10 |
+
|
11 |
from database.users_chats_db import db
|
12 |
+
|
13 |
from database.ia_filterdb import Media, get_file_details, get_search_results
|
14 |
+
|
15 |
from database.connections_mdb import add_connection, active_connection, all_connections, if_active, make_active, make_inactive, delete_connection
|
16 |
+
|
17 |
from database.filters_mdb import add_filter, find_filter, get_filters, delete_filter, del_all, count_filters, filter_stats
|
18 |
+
|
19 |
from database.gfilters_mdb import add_gfilter, find_gfilter, get_gfilters, delete_gfilter, del_allg, count_gfilters, gfilter_stats
|
20 |
+
|
21 |
from utils import get_settings, save_group_settings, humanbytes, get_size, extract_user, get_file_id, get_poster, parser, get_shortlink, extract_time, admin_check
|
22 |
+
|
23 |
from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG, ADMINS, CHANNELS
|
24 |
+
|
25 |
from pyrogram import Client, filters, enums, types
|
26 |
+
|
27 |
from pyrogram.errors import ChatAdminRequired, UserIsBlocked, MessageNotModified, PeerIdInvalid, FloodWait
|
28 |
+
|
29 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message, CallbackQuery
|
30 |
+
|
31 |
from telegraph import upload_file
|
32 |
+
|
33 |
from io import BytesIO
|
34 |
+
|
35 |
import aiohttp
|
36 |
+
|
37 |
import asyncio
|
38 |
+
|
39 |
import requests
|
40 |
+
|
41 |
import os
|
42 |
+
|
43 |
import logging
|
44 |
+
|
45 |
import re
|
46 |
+
|
47 |
import traceback
|
48 |
+
|
49 |
import json
|
50 |
+
|
51 |
from googletrans import Translator
|
52 |
+
|
53 |
from gtts import gTTS
|
54 |
+
|
55 |
from youtube_search import YoutubeSearch
|
56 |
+
|
57 |
from youtubesearchpython import SearchVideos
|
58 |
+
|
59 |
from yt_dlp import YoutubeDL
|
60 |
+
|
61 |
import shutil
|
62 |
+
|
63 |
import psutil
|
64 |
+
|
65 |
import wget
|
66 |
+
|
67 |
from datetime import datetime, timedelta
|
68 |
+
|
69 |
from urllib.parse import quote
|
70 |
+
|
71 |
from telegraph import upload_file
|
72 |
+
|
73 |
from PIL import Image, ImageDraw, ImageFont
|
74 |
+
|
75 |
import textwrap
|
76 |
|
77 |
+
|
78 |
+
# Initialize FastAPI
|
79 |
+
|
80 |
app = FastAPI()
|
81 |
|
82 |
+
|
83 |
+
# Configure templates and static files
|
84 |
+
|
85 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
86 |
+
|
87 |
+
templates = Jinja2Templates(directory="templates")
|
88 |
+
|
89 |
+
|
90 |
+
# Shared instance of the Bot class
|
91 |
+
|
92 |
+
bot_instance = Bot()
|
93 |
+
|
94 |
+
|
95 |
+
# Initialize Pyrogram client
|
96 |
+
|
97 |
+
@app.on_event("startup")
|
98 |
+
|
99 |
+
async def startup_event():
|
100 |
+
|
101 |
+
asyncio.create_task(bot_instance.start())
|
102 |
+
|
103 |
+
|
104 |
+
@app.on_event("shutdown")
|
105 |
+
|
106 |
+
async def shutdown_event():
|
107 |
+
|
108 |
+
await bot_instance.stop()
|
109 |
+
|
110 |
+
|
111 |
+
# Dashboard Route
|
112 |
+
|
113 |
+
@app.get("/", include_in_schema=False)
|
114 |
+
|
115 |
+
async def dashboard(request: Request):
|
116 |
+
|
117 |
+
try:
|
118 |
+
|
119 |
+
# Get statistics
|
120 |
+
|
121 |
+
total_users = await db.total_users_count()
|
122 |
+
|
123 |
+
total_chats = await db.total_chat_count()
|
124 |
+
|
125 |
+
uptime = datetime.now() - datetime.fromtimestamp(temp.UPTIME)
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
# System metrics
|
130 |
+
|
131 |
+
cpu = psutil.cpu_percent()
|
132 |
+
|
133 |
+
mem = psutil.virtual_memory().percent
|
134 |
+
|
135 |
+
disk = psutil.disk_usage('/').percent
|
136 |
+
|
137 |
+
|
138 |
+
return templates.TemplateResponse("dashboard.html", {
|
139 |
+
|
140 |
+
"request": request,
|
141 |
+
|
142 |
+
"total_users": total_users,
|
143 |
+
|
144 |
+
"total_chats": total_chats,
|
145 |
+
|
146 |
+
"uptime": str(uptime).split('.')[0],
|
147 |
+
|
148 |
+
"cpu_usage": f"{cpu}%",
|
149 |
+
|
150 |
+
"mem_usage": f"{mem}%",
|
151 |
+
|
152 |
+
"disk_usage": f"{disk}%",
|
153 |
+
|
154 |
+
"bot_username": (await bot_instance.get_me()).username,
|
155 |
+
|
156 |
+
"version": "PROFESSOR-BOT v4.5.0"
|
157 |
+
|
158 |
+
})
|
159 |
+
|
160 |
+
except Exception as e:
|
161 |
+
|
162 |
+
logging.error(f"Dashboard Error: {str(e)}")
|
163 |
+
|
164 |
+
return templates.TemplateResponse("error.html", {"request": request})
|
165 |
+
|
166 |
+
|
167 |
# Shared instance of the Bot class
|
168 |
bot_instance = Bot()
|
169 |
|