|
|
|
|
|
import os |
|
|
|
import json |
|
import time |
|
import math |
|
import logging |
|
import asyncio |
|
import traceback |
|
import aiohttp_cors |
|
from aiohttp import web |
|
from pyrogram import raw |
|
from bson import ObjectId |
|
from bson.json_util import dumps |
|
from aiohttp.http_exceptions import BadStatusLine |
|
|
|
|
|
from FileStream.bot import req_client |
|
from FileStream.config import Telegram,Server |
|
from FileStream.Database import Database |
|
from FileStream.TMDB.Endpoint import search_tmdb_any,search_tmdb_tv,search_tmdb_movies |
|
from FileStream.server.exceptions import FIleNotFound, InvalidHash |
|
|
|
from .Functions.downloader import media_streamer |
|
|
|
CORS_HEADERS = { |
|
"Access-Control-Allow-Origin": "*", |
|
"Access-Control-Allow-Headers": "*" |
|
} |
|
|
|
async def handle_v2(request): |
|
return web.Response(text="Hello from app api!") |
|
|
|
|
|
async def list_10_all_files_db(request): |
|
|
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files, total_files = await db.find_all_files([1, 10]) |
|
print(files) |
|
return web.json_response([ dict(file) async for file in files], headers=CORS_HEADERS) |
|
|
|
|
|
|
|
|
|
|
|
async def list_all_files_db(request): |
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files= await db.get_all_files_api() |
|
|
|
return web.json_response(json.loads(dumps(files)), headers=CORS_HEADERS) |
|
|
|
|
|
async def list_all_tmdb_movies_from_db(request): |
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files= await db.get_all_files() |
|
|
|
response=[] |
|
async for row in files: |
|
|
|
try : |
|
|
|
resp = search_tmdb_movies( str(row['file']['caption']) if str(row['file']['caption']) else str(row['file']['file_name'])) |
|
if resp != None : |
|
|
|
|
|
response.append(resp) |
|
else: |
|
print("\n * Skipped:",row['file']['caption'],str(row['file']['file_name'])) |
|
continue |
|
except Exception as e: |
|
print("Error ",e) |
|
break |
|
return web.json_response(json.loads(dumps(response)),headers=CORS_HEADERS) |
|
|
|
|
|
|
|
async def list_all_tmdb_tv_from_db(request): |
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files= await db.get_all_files() |
|
|
|
response=[] |
|
async for row in files: |
|
|
|
try : |
|
|
|
resp = search_tmdb_tv( str(row['file']['caption']) if str(row['file']['caption']) else str(row['file']['file_name'])) |
|
if resp != None : |
|
|
|
|
|
response.append(resp) |
|
else: |
|
print("\n * Skipped:",row['file']['caption'],str(row['file']['file_name'])) |
|
continue |
|
except Exception as e: |
|
print("Error ",e) |
|
break |
|
return web.json_response(json.loads(dumps(response)), headers=CORS_HEADERS) |
|
|
|
|
|
async def list_all_files_tmdb(request): |
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files= await db.get_all_files() |
|
async def data_generator(): |
|
async for row in files: |
|
|
|
try : |
|
|
|
resp = search_tmdb_any( str(row['file']['caption']) if str(row['file']['caption']) else str(row['file']['file_name'])) |
|
if resp != None : |
|
|
|
|
|
|
|
yield json.dumps(resp) + + '\n' |
|
|
|
else: |
|
print("\n * Skipped:",row['file']['caption'],str(row['file']['file_name'])) |
|
continue |
|
|
|
except Exception as e: |
|
print("Error ",e) |
|
break |
|
|
|
return web.Response(body=data_generator(), content_type='application/json', headers=CORS_HEADERS) |
|
|
|
|
|
async def list_all_files(request): |
|
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) |
|
files=await db.get_all_files_api() |
|
resp=[{ |
|
"adult": False, |
|
"backdrop_path": "/c1bz69r0v065TGFA5nqBiKzPDys.jpg", |
|
"genre_ids": [ |
|
35, |
|
10751, |
|
10402 |
|
], |
|
"id": f"{row['_id']}", |
|
"original_language": "en-hi", |
|
"original_title": f"{str(row['file']['caption'])}", |
|
"overview": "XRepo Movies", |
|
"popularity": 1710.176, |
|
"poster_path": "/irIS5Tn3TXjNi1R9BpWvGAN4CZ1.jpg", |
|
"release_date": "2022-10-07", |
|
"title": f"{str(row['file']['caption'])}", |
|
"link": f"{Server.URL}api/dl/{row['_id']}", |
|
"vote_average": 7.8, |
|
"vote_count": 122, |
|
} |
|
for row in files] |
|
return web.json_response(json.loads(dumps(resp)), headers=CORS_HEADERS) |
|
|
|
|
|
|
|
async def upload_file(request: web.Request): |
|
|
|
data = await request.post() |
|
file = data.get('file').file |
|
chunk = file.read() |
|
""" |
|
user_id : |
|
file_id :"BAACAgUAAxkBAAJBHGYI_aJSvyL_ijKwrVHQVzFgC1YZAAItDQACOnNAVMIpWwl6b63EHg…" |
|
file_unique_id :"AgADLQ0AAjpzQFQ" |
|
file_name :"Dadur_Kirti_S01_COMBINED_720p_HOICHOI_WEB_DL_Bengali@COOL_MOVIES.mp4" |
|
file_size : 1354816011 |
|
mime_type : "video/mp4" |
|
time : 1711865251.1016757 |
|
user_type: "TELEGRAM"/"WEB" |
|
privacy_type: "PUBLIC"/"PRIVATE" |
|
file_ids : |
|
""" |
|
file_details = dict(user_id="thebinary1", |
|
dropzone_id=str(data["dzuuid"]), |
|
file=dict(file_id=str(data["dzuuid"]), |
|
file_unique_id=str(data["dzuuid"]), |
|
file_name=str(data.get('file').filename), |
|
file_size=int(data["dztotalfilesize"]), |
|
mime_type=mime_identifier(str(data.get('file').filename)), |
|
part_size=int(data["dzchunksize"]), |
|
file_part=int(data["dzchunkindex"]), |
|
total_parts=int(data["dztotalchunkcount"])), |
|
time=Time_ISTKolNow(), |
|
user_type="WEB", |
|
privacy_type="PRIVATE") |
|
|
|
print(file_details) if (file_details["file"]["file_part"] == 0) else None |
|
client_req = await req_client() |
|
|
|
|
|
print("using :", client_req["index"]) |
|
tg_connect = TeleUploader(client_req["client"]) |
|
main = await tg_connect.upload_web_file(file_details, chunk) |
|
|
|
return web.json_response({ |
|
"status": main.get("status"), |
|
"message": main.get("message") |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
async def stream_handler(request: web.Request): |
|
try: |
|
path = request.match_info["path"] |
|
return await media_streamer(request, path, "FAST") |
|
except InvalidHash as e: |
|
raise web.HTTPForbidden(text=e.message) |
|
except FIleNotFound as e: |
|
raise web.HTTPNotFound(text=e.message) |
|
except (AttributeError, BadStatusLine, ConnectionResetError): |
|
pass |
|
except Exception as e: |
|
traceback.print_exc() |
|
logging.critical(e.with_traceback(None)) |
|
logging.debug(traceback.format_exc()) |
|
raise web.HTTPInternalServerError(text=str(e)) |
|
|
|
|
|
api = web.Application() |
|
|
|
cors = aiohttp_cors.setup(api, defaults={ |
|
"*": aiohttp_cors.ResourceOptions( |
|
allow_credentials=False, |
|
expose_headers="*", |
|
allow_headers="*", |
|
allow_methods="*" |
|
) |
|
}) |
|
|
|
cors.add(api.router.add_get('/', handle_v2)) |
|
api.router.add_get('/files', list_all_files_db) |
|
api.router.add_get('/files/mix', list_all_files) |
|
api.router.add_get('/tmdb/mix', list_all_files_tmdb) |
|
api.router.add_get('/10/files', list_10_all_files_db) |
|
api.router.add_get('/tmdb/tv', list_all_tmdb_tv_from_db) |
|
api.router.add_get('/tmdb/movies', list_all_tmdb_movies_from_db) |
|
|
|
api.router.add_get('/upload', upload_file) |
|
api.router.add_get('/dl/{path}', stream_handler) |