Spaces:
Running
Running
Commit
·
97f9e03
1
Parent(s):
1a47129
Code Updates & Optimisations Clearing UP ACTIVE_CLIENTS
Browse files- FileStream/__main__.py +18 -14
- FileStream/server/__init__.py +32 -7
- FileStream/server/routes_api.py +6 -3
- Unused Codes/{Dockerfile_old → Dockerfile} +0 -0
- Unused Codes/__init__old.py +69 -0
FileStream/__main__.py
CHANGED
@@ -2,14 +2,16 @@ import sys
|
|
2 |
import asyncio
|
3 |
import logging
|
4 |
import traceback
|
5 |
-
import logging.handlers as handlers
|
6 |
-
from FileStream.config import Telegram, Server
|
7 |
from aiohttp import web
|
8 |
from pyrogram import idle
|
|
|
|
|
|
|
9 |
|
10 |
from FileStream.bot import FileStream
|
11 |
-
from FileStream.Tools import Time_ISTKolNow
|
12 |
from FileStream.server import web_server
|
|
|
|
|
13 |
from FileStream.bot.clients import initialize_clients
|
14 |
|
15 |
# Configure logging
|
@@ -39,6 +41,7 @@ async def start_services():
|
|
39 |
)
|
40 |
+ "\n****\n-------------------- Initializing Telegram Bot --------------------"
|
41 |
)
|
|
|
42 |
await FileStream.start()
|
43 |
bot_info = await FileStream.get_me()
|
44 |
FileStream.id = bot_info.id
|
@@ -57,9 +60,9 @@ async def start_services():
|
|
57 |
|
58 |
print(
|
59 |
f"------------------------- Service Started -------------------------\n"
|
60 |
-
f"Bot =>> {bot_info.first_name}\n"
|
61 |
-
|
62 |
-
|
63 |
"------------------------------------------------------------------"
|
64 |
)
|
65 |
|
@@ -73,16 +76,17 @@ async def start_services():
|
|
73 |
|
74 |
async def cleanup():
|
75 |
await server.cleanup()
|
|
|
|
|
76 |
|
77 |
-
|
78 |
try:
|
79 |
-
|
80 |
except KeyboardInterrupt:
|
81 |
-
print("\n
|
82 |
except Exception as err:
|
83 |
-
logging.error(
|
84 |
finally:
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
asyncio.run(main())
|
|
|
2 |
import asyncio
|
3 |
import logging
|
4 |
import traceback
|
|
|
|
|
5 |
from aiohttp import web
|
6 |
from pyrogram import idle
|
7 |
+
import logging.handlers as handlers
|
8 |
+
|
9 |
+
#------------------Local Imports----------------------#
|
10 |
|
11 |
from FileStream.bot import FileStream
|
|
|
12 |
from FileStream.server import web_server
|
13 |
+
from FileStream.Tools import Time_ISTKolNow
|
14 |
+
from FileStream.config import Telegram, Server
|
15 |
from FileStream.bot.clients import initialize_clients
|
16 |
|
17 |
# Configure logging
|
|
|
41 |
)
|
42 |
+ "\n****\n-------------------- Initializing Telegram Bot --------------------"
|
43 |
)
|
44 |
+
|
45 |
await FileStream.start()
|
46 |
bot_info = await FileStream.get_me()
|
47 |
FileStream.id = bot_info.id
|
|
|
60 |
|
61 |
print(
|
62 |
f"------------------------- Service Started -------------------------\n"
|
63 |
+
f"Bot =>> {bot_info.first_name}\n" +
|
64 |
+
(f"DC ID =>> {bot_info.dc_id}\n" if bot_info.dc_id else "") +
|
65 |
+
f"URL =>> {Server.URL}\n"
|
66 |
"------------------------------------------------------------------"
|
67 |
)
|
68 |
|
|
|
76 |
|
77 |
async def cleanup():
|
78 |
await server.cleanup()
|
79 |
+
await FileStream.stop()
|
80 |
+
|
81 |
|
82 |
+
if __name__ == "__main__":
|
83 |
try:
|
84 |
+
loop.run_until_complete(start_services())
|
85 |
except KeyboardInterrupt:
|
86 |
+
print("\n------------------ Stopping Services KeyBoard Interrupt -----------------")
|
87 |
except Exception as err:
|
88 |
+
logging.error(traceback.format_exc())
|
89 |
finally:
|
90 |
+
loop.run_until_complete(cleanup())
|
91 |
+
loop.stop()
|
92 |
+
print("------------------------ Stopped Services ------------------------")
|
|
FileStream/server/__init__.py
CHANGED
@@ -1,21 +1,46 @@
|
|
|
|
|
|
1 |
from aiohttp import web
|
2 |
-
|
3 |
-
|
4 |
-
from .
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
async def root_route_handler(request):
|
|
|
|
|
9 |
return web.json_response({"status": "alive", "message": "Server is running"})
|
10 |
|
11 |
|
12 |
-
|
13 |
def web_server():
|
14 |
web_app = web.Application(client_max_size=500)
|
15 |
web_app.router.add_get('/', root_route_handler)
|
16 |
web_app.add_routes(routes)
|
17 |
web_app.add_subapp('/app', sub_app)
|
18 |
web_app.add_subapp('/api', api)
|
|
|
|
|
19 |
return web_app
|
20 |
-
|
21 |
-
|
|
|
1 |
+
import asyncio
|
2 |
+
import logging
|
3 |
from aiohttp import web
|
4 |
+
|
5 |
+
#-----------------------Local Imports-------------------------#
|
6 |
+
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
7 |
+
|
8 |
+
|
9 |
+
# Set time to consider a client inactive (e.g., 10 minutes)
|
10 |
+
INACTIVITY_TIMEOUT = 600 # 10 minutes
|
11 |
+
|
12 |
+
async def clear_inactive_clients():
|
13 |
+
"""
|
14 |
+
Periodically checks and clears inactive clients from ACTIVE_CLIENTS.
|
15 |
+
"""
|
16 |
+
while True:
|
17 |
+
await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every 5 minutes
|
18 |
+
|
19 |
+
# Find clients that are inactive and clear them
|
20 |
+
now = asyncio.get_event_loop().time()
|
21 |
+
inactive_clients = [
|
22 |
+
client for client, last_activity in ACTIVE_CLIENTS.items()
|
23 |
+
if now - last_activity > INACTIVITY_TIMEOUT
|
24 |
+
]
|
25 |
+
|
26 |
+
for client in inactive_clients:
|
27 |
+
# Log and clear the inactive client
|
28 |
+
logging.info(f"Clearing inactive client: {client}")
|
29 |
+
del ACTIVE_CLIENTS[client] # Clear the client
|
30 |
|
31 |
|
32 |
async def root_route_handler(request):
|
33 |
+
client_id = request.remote # You can use the client's IP or any identifier
|
34 |
+
ACTIVE_CLIENTS[client_id] = asyncio.get_event_loop().time() # Update last activity time
|
35 |
return web.json_response({"status": "alive", "message": "Server is running"})
|
36 |
|
37 |
|
|
|
38 |
def web_server():
|
39 |
web_app = web.Application(client_max_size=500)
|
40 |
web_app.router.add_get('/', root_route_handler)
|
41 |
web_app.add_routes(routes)
|
42 |
web_app.add_subapp('/app', sub_app)
|
43 |
web_app.add_subapp('/api', api)
|
44 |
+
# Start the background task to clear inactive clients
|
45 |
+
asyncio.create_task(clear_inactive_clients())
|
46 |
return web_app
|
|
|
|
FileStream/server/routes_api.py
CHANGED
@@ -12,12 +12,14 @@ from aiohttp.http_exceptions import BadStatusLine
|
|
12 |
|
13 |
#---------------------Local Imports----------------------------------#
|
14 |
from FileStream.bot import req_client
|
15 |
-
from FileStream.config import Telegram, Server
|
16 |
from FileStream.Database import Database
|
17 |
-
from FileStream.
|
|
|
18 |
from FileStream.server.exceptions import FIleNotFound, InvalidHash
|
|
|
|
|
|
|
19 |
|
20 |
-
from .Functions.downloader import media_streamer
|
21 |
|
22 |
CORS_HEADERS = {
|
23 |
"Access-Control-Allow-Origin": "*",
|
@@ -163,6 +165,7 @@ cors = aiohttp_cors.setup(api, defaults={"*": aiohttp_cors.ResourceOptions(
|
|
163 |
)})
|
164 |
|
165 |
cors.add(api.router.add_get('/', handle_v2))
|
|
|
166 |
api.router.add_get('/files', list_all_files_db)
|
167 |
api.router.add_get('/files/mix', list_all_files)
|
168 |
api.router.add_get('/tmdb/mix', list_all_files_tmdb)
|
|
|
12 |
|
13 |
#---------------------Local Imports----------------------------------#
|
14 |
from FileStream.bot import req_client
|
|
|
15 |
from FileStream.Database import Database
|
16 |
+
from FileStream.config import Telegram, Server
|
17 |
+
from .Functions.downloader import media_streamer
|
18 |
from FileStream.server.exceptions import FIleNotFound, InvalidHash
|
19 |
+
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies
|
20 |
+
|
21 |
+
|
22 |
|
|
|
23 |
|
24 |
CORS_HEADERS = {
|
25 |
"Access-Control-Allow-Origin": "*",
|
|
|
165 |
)})
|
166 |
|
167 |
cors.add(api.router.add_get('/', handle_v2))
|
168 |
+
|
169 |
api.router.add_get('/files', list_all_files_db)
|
170 |
api.router.add_get('/files/mix', list_all_files)
|
171 |
api.router.add_get('/tmdb/mix', list_all_files_tmdb)
|
Unused Codes/{Dockerfile_old → Dockerfile}
RENAMED
File without changes
|
Unused Codes/__init__old.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#FileStream/server/__init__
|
2 |
+
|
3 |
+
from aiohttp import web
|
4 |
+
#import aiohttp_cors
|
5 |
+
from .routes_main import routes
|
6 |
+
from .routes_api import api
|
7 |
+
from .routes_app import sub_app
|
8 |
+
|
9 |
+
|
10 |
+
async def root_route_handler(request):
|
11 |
+
return web.json_response({"status": "alive", "message": "Server is running"})
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def web_server():
|
16 |
+
web_app = web.Application(client_max_size=500)
|
17 |
+
web_app.router.add_get('/', root_route_handler)
|
18 |
+
web_app.add_routes(routes)
|
19 |
+
web_app.add_subapp('/app', sub_app)
|
20 |
+
web_app.add_subapp('/api', api)
|
21 |
+
return web_app
|
22 |
+
|
23 |
+
import asyncio
|
24 |
+
import logging
|
25 |
+
from aiohttp import web
|
26 |
+
|
27 |
+
# Dictionary to store active clients, using client identifier (could be IP or session ID)
|
28 |
+
ACTIVE_CLIENTS = {}
|
29 |
+
|
30 |
+
# Set time to consider a client inactive (e.g., 5 minutes)
|
31 |
+
INACTIVITY_TIMEOUT = 300 # 5 minutes
|
32 |
+
|
33 |
+
async def clear_inactive_clients():
|
34 |
+
"""
|
35 |
+
Periodically checks and clears inactive clients from ACTIVE_CLIENTS.
|
36 |
+
"""
|
37 |
+
while True:
|
38 |
+
await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every 5 minutes
|
39 |
+
|
40 |
+
# Find clients that are inactive and clear them
|
41 |
+
now = asyncio.get_event_loop().time()
|
42 |
+
inactive_clients = [
|
43 |
+
client for client, last_activity in ACTIVE_CLIENTS.items()
|
44 |
+
if now - last_activity > INACTIVITY_TIMEOUT
|
45 |
+
]
|
46 |
+
|
47 |
+
for client in inactive_clients:
|
48 |
+
# Log and clear the inactive client
|
49 |
+
logging.info(f"Clearing inactive client: {client}")
|
50 |
+
del ACTIVE_CLIENTS[client] # Clear the client
|
51 |
+
|
52 |
+
async def root_route_handler(request):
|
53 |
+
client_id = request.remote # You can use the client's IP or any identifier
|
54 |
+
ACTIVE_CLIENTS[client_id] = asyncio.get_event_loop().time() # Update last activity time
|
55 |
+
|
56 |
+
return web.json_response({"status": "alive", "message": "Server is running"})
|
57 |
+
|
58 |
+
def web_server():
|
59 |
+
web_app = web.Application(client_max_size=500)
|
60 |
+
web_app.router.add_get('/', root_route_handler)
|
61 |
+
web_app.add_routes(routes)
|
62 |
+
web_app.add_subapp('/app', sub_app)
|
63 |
+
web_app.add_subapp('/api', api)
|
64 |
+
|
65 |
+
# Start the background task to clear inactive clients
|
66 |
+
asyncio.create_task(clear_inactive_clients())
|
67 |
+
|
68 |
+
return web_app
|
69 |
+
|