File size: 4,388 Bytes
e566133
 
 
 
 
6c92dfc
e566133
 
 
 
 
 
26741bb
e566133
12e4bed
e566133
97f9e03
a9690e7
97f9e03
 
 
e566133
4a17347
4abcb4b
 
 
 
4131df3
12e4bed
e566133
26741bb
5c35400
26741bb
 
 
4a17347
26741bb
e566133
26741bb
 
 
e566133
26741bb
87159d4
26741bb
 
 
 
 
 
 
 
 
 
 
 
 
87159d4
26741bb
 
 
 
 
 
 
 
 
 
 
 
 
e566133
26741bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e566133
26741bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e4bed
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import json
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

#---------------------Local Imports----------------------------------#
from FileStream.bot import req_client
from ..Functions import media_streamer
from FileStream.Database import Database
from FileStream.config import Telegram, Server
from FileStream.Exceptions import FIleNotFound, InvalidHash
from FileStream.TMDB.Endpoint import search_tmdb_any, search_tmdb_tv, search_tmdb_movies




CORS_HEADERS = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "*"
}



# API endpoint to list 10 files (pagination optimization)
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])
    return web.json_response([dict(file) async for file in files], headers=CORS_HEADERS)

# API endpoint to list all files in database
async def list_all_files_db(request):
    db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
    files = await db.get_all_files_api()  # Ensure `get_all_files_api` is optimized for performance
    return web.json_response(json.loads(dumps(files)), headers=CORS_HEADERS)

# API endpoint to list all TMDB movies related to files in the database
async def list_all_tmdb_movies_from_db(request):
    db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
    files = await db.get_all_files()
    response = []
    for row in files:
        try:
            resp = search_tmdb_movies(str(row['file']['caption']) if row['file']['caption'] else str(row['file']['file_name']))
            if resp:
                response.append(resp)
        except Exception as e:
            logging.error(f"Error while fetching TMDB movie for {row['file']['caption']}: {e}")
    return web.json_response(json.loads(dumps(response)), headers=CORS_HEADERS)

# API endpoint to list all TMDB TV shows related to files in the database
async def list_all_tmdb_tv_from_db(request):
    db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
    files = await db.get_all_files()
    response = []
    for row in files:
        try:
            resp = search_tmdb_tv(str(row['file']['caption']) if row['file']['caption'] else str(row['file']['file_name']))
            if resp:
                response.append(resp)
        except Exception as e:
            logging.error(f"Error while fetching TMDB TV show for {row['file']['caption']}: {e}")
    return web.json_response(json.loads(dumps(response)), headers=CORS_HEADERS)

# API endpoint to list all TMDB results, streaming them to the client
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():
        for row in files:
            try:
                resp = search_tmdb_any(str(row['file']['caption']) if row['file']['caption'] else str(row['file']['file_name']))
                if resp:
                    yield json.dumps(resp) + '\n'  # Streaming data to client in chunks
            except Exception as e:
                logging.error(f"Error while fetching TMDB data for {row['file']['caption']}: {e}")
                
    return web.Response(body=data_generator(), content_type='application/json', headers=CORS_HEADERS)

# API endpoint to list all files for a simple response
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)