Update fastapi_app.py
Browse files- fastapi_app.py +0 -90
fastapi_app.py
CHANGED
@@ -1,181 +1,91 @@
|
|
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 |
-
|
170 |
-
# Initialize Pyrogram client
|
171 |
-
@app.on_event("startup")
|
172 |
-
async def startup_event():
|
173 |
-
asyncio.create_task(bot_instance.start())
|
174 |
-
|
175 |
-
@app.on_event("shutdown")
|
176 |
-
async def shutdown_event():
|
177 |
-
await bot_instance.stop()
|
178 |
-
|
179 |
# Utility functions
|
180 |
async def get_chat_member(client, chat_id, user_id):
|
181 |
try:
|
|
|
1 |
from fastapi import FastAPI, HTTPException, Depends, Request
|
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
|
|
3 |
from fastapi.templating import Jinja2Templates
|
|
|
4 |
from pydantic import BaseModel, Field
|
|
|
5 |
from bot import Bot, temp
|
|
|
6 |
from database.users_chats_db import db
|
|
|
7 |
from database.ia_filterdb import Media, get_file_details, get_search_results
|
|
|
8 |
from database.connections_mdb import add_connection, active_connection, all_connections, if_active, make_active, make_inactive, delete_connection
|
|
|
9 |
from database.filters_mdb import add_filter, find_filter, get_filters, delete_filter, del_all, count_filters, filter_stats
|
|
|
10 |
from database.gfilters_mdb import add_gfilter, find_gfilter, get_gfilters, delete_gfilter, del_allg, count_gfilters, gfilter_stats
|
|
|
11 |
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
|
|
|
12 |
from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG, ADMINS, CHANNELS
|
|
|
13 |
from pyrogram import Client, filters, enums, types
|
|
|
14 |
from pyrogram.errors import ChatAdminRequired, UserIsBlocked, MessageNotModified, PeerIdInvalid, FloodWait
|
|
|
15 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message, CallbackQuery
|
|
|
16 |
from telegraph import upload_file
|
|
|
17 |
from io import BytesIO
|
|
|
18 |
import aiohttp
|
|
|
19 |
import asyncio
|
|
|
20 |
import requests
|
|
|
21 |
import os
|
|
|
22 |
import logging
|
|
|
23 |
import re
|
|
|
24 |
import traceback
|
|
|
25 |
import json
|
|
|
26 |
from googletrans import Translator
|
|
|
27 |
from gtts import gTTS
|
|
|
28 |
from youtube_search import YoutubeSearch
|
|
|
29 |
from youtubesearchpython import SearchVideos
|
|
|
30 |
from yt_dlp import YoutubeDL
|
|
|
31 |
import shutil
|
|
|
32 |
import psutil
|
|
|
33 |
import wget
|
|
|
34 |
from datetime import datetime, timedelta
|
|
|
35 |
from urllib.parse import quote
|
|
|
36 |
from telegraph import upload_file
|
|
|
37 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
38 |
import textwrap
|
39 |
|
|
|
40 |
# Initialize FastAPI
|
|
|
41 |
app = FastAPI()
|
42 |
|
|
|
43 |
# Configure templates and static files
|
|
|
44 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
45 |
templates = Jinja2Templates(directory="templates")
|
46 |
|
|
|
47 |
# Shared instance of the Bot class
|
|
|
48 |
bot_instance = Bot()
|
49 |
|
|
|
50 |
# Initialize Pyrogram client
|
|
|
51 |
@app.on_event("startup")
|
|
|
52 |
async def startup_event():
|
|
|
53 |
asyncio.create_task(bot_instance.start())
|
54 |
|
|
|
55 |
@app.on_event("shutdown")
|
|
|
56 |
async def shutdown_event():
|
|
|
57 |
await bot_instance.stop()
|
58 |
|
|
|
59 |
# Dashboard Route
|
|
|
60 |
@app.get("/", include_in_schema=False)
|
|
|
61 |
async def dashboard(request: Request):
|
|
|
62 |
try:
|
|
|
63 |
# Get statistics
|
|
|
64 |
total_users = await db.total_users_count()
|
|
|
65 |
total_chats = await db.total_chat_count()
|
|
|
66 |
uptime = datetime.now() - datetime.fromtimestamp(temp.UPTIME)
|
|
|
67 |
|
|
|
68 |
# System metrics
|
|
|
69 |
cpu = psutil.cpu_percent()
|
|
|
70 |
mem = psutil.virtual_memory().percent
|
|
|
71 |
disk = psutil.disk_usage('/').percent
|
72 |
|
|
|
73 |
return templates.TemplateResponse("dashboard.html", {
|
|
|
74 |
"request": request,
|
|
|
75 |
"total_users": total_users,
|
|
|
76 |
"total_chats": total_chats,
|
|
|
77 |
"uptime": str(uptime).split('.')[0],
|
|
|
78 |
"cpu_usage": f"{cpu}%",
|
|
|
79 |
"mem_usage": f"{mem}%",
|
|
|
80 |
"disk_usage": f"{disk}%",
|
|
|
81 |
"bot_username": (await bot_instance.get_me()).username,
|
|
|
82 |
"version": "PROFESSOR-BOT v4.5.0"
|
|
|
83 |
})
|
|
|
84 |
except Exception as e:
|
|
|
85 |
logging.error(f"Dashboard Error: {str(e)}")
|
|
|
86 |
return templates.TemplateResponse("error.html", {"request": request})
|
87 |
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
# Utility functions
|
90 |
async def get_chat_member(client, chat_id, user_id):
|
91 |
try:
|