Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,30 +1,32 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.templating import Jinja2Templates
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.responses import HTMLResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
-
import
|
| 8 |
-
import
|
|
|
|
|
|
|
| 9 |
import translator
|
|
|
|
|
|
|
| 10 |
import tmdb
|
| 11 |
import base64
|
| 12 |
|
| 13 |
# Settings
|
| 14 |
translator_version = 'v0.0.6'
|
| 15 |
-
REQUEST_TIMEOUT = 120
|
| 16 |
FORCE_PREFIX = False
|
| 17 |
FORCE_META = False
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
# Cache
|
| 22 |
-
from diskcache import Cache
|
| 23 |
tmp_cache = Cache('/tmp/meta')
|
| 24 |
tmp_cache.clear()
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
# FastAPI app initialization
|
| 28 |
@asynccontextmanager
|
| 29 |
async def lifespan(app: FastAPI):
|
| 30 |
print('Started')
|
|
@@ -33,29 +35,23 @@ async def lifespan(app: FastAPI):
|
|
| 33 |
|
| 34 |
app = FastAPI(lifespan=lifespan)
|
| 35 |
templates = Jinja2Templates(directory="templates")
|
| 36 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 37 |
|
| 38 |
-
#
|
| 39 |
app.add_middleware(
|
| 40 |
CORSMiddleware,
|
| 41 |
-
allow_origins=["*"],
|
| 42 |
allow_credentials=True,
|
| 43 |
-
allow_methods=["*"],
|
| 44 |
-
allow_headers=["*"],
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
# Main route for the translator (simplified)
|
| 49 |
@app.get('/', response_class=HTMLResponse)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
response.headers["Pragma"] = "no-cache"
|
| 54 |
-
response.headers["Expires"] = "0"
|
| 55 |
-
return response
|
| 56 |
-
|
| 57 |
|
| 58 |
-
#
|
| 59 |
@app.get('/{addon_url}/{user_settings}/manifest.json')
|
| 60 |
async def get_manifest(addon_url):
|
| 61 |
addon_url = decode_base64_url(addon_url)
|
|
@@ -63,8 +59,8 @@ async def get_manifest(addon_url):
|
|
| 63 |
response = await client.get(f"{addon_url}/manifest.json")
|
| 64 |
manifest = response.json()
|
| 65 |
|
| 66 |
-
|
| 67 |
-
if not
|
| 68 |
manifest['translated'] = True
|
| 69 |
manifest['t_language'] = 'it-IT'
|
| 70 |
manifest['name'] += ' 🇮🇹'
|
|
@@ -83,93 +79,64 @@ async def get_manifest(addon_url):
|
|
| 83 |
|
| 84 |
return manifest
|
| 85 |
|
| 86 |
-
|
| 87 |
-
# Link generator route for translated URLs
|
| 88 |
-
@app.get('/{addon_url}/{user_settings}/link_generator')
|
| 89 |
-
async def link_generator(addon_url, user_settings: str):
|
| 90 |
-
# Decode the base64 addon URL
|
| 91 |
-
addon_url = decode_base64_url(addon_url)
|
| 92 |
-
user_settings = parse_user_settings(user_settings)
|
| 93 |
-
|
| 94 |
-
# Here you may modify the URL according to the translation or user settings
|
| 95 |
-
translated_addon_url = f"{addon_url}?lang={user_settings.get('language', 'it-IT')}"
|
| 96 |
-
|
| 97 |
-
# Return the translated URL
|
| 98 |
-
return {"translated_url": translated_addon_url}
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# Translation for catalog items
|
| 102 |
@app.get('/{addon_url}/{user_settings}/catalog/{type}/{path:path}')
|
| 103 |
async def get_catalog(addon_url, type: str, user_settings: str, path: str):
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
user_settings = parse_user_settings(user_settings)
|
|
|
|
| 106 |
|
| 107 |
async with httpx.AsyncClient(follow_redirects=True, timeout=REQUEST_TIMEOUT) as client:
|
| 108 |
response = await client.get(f"{addon_url}/catalog/{type}/{path}")
|
|
|
|
| 109 |
try:
|
| 110 |
catalog = response.json()
|
| 111 |
except:
|
|
|
|
| 112 |
return {}
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
tmdb.get_tmdb_data(client, item.get('imdb_id', item.get('id')), "imdb_id")
|
| 124 |
-
for item in catalog['metas']
|
| 125 |
-
]
|
| 126 |
-
tmdb_details = await asyncio.gather(*tasks)
|
| 127 |
-
return tmdb_details
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Meta information for items
|
| 131 |
-
@app.get('/{addon_url}/{user_settings}/meta/{type}/{id}.json')
|
| 132 |
-
async def get_meta(addon_url, type: str, id: str):
|
| 133 |
-
addon_url = decode_base64_url(addon_url)
|
| 134 |
-
async with httpx.AsyncClient(follow_redirects=True, timeout=REQUEST_TIMEOUT) as client:
|
| 135 |
-
# Fetch metadata (cached or fresh)
|
| 136 |
-
meta = tmp_cache.get(id)
|
| 137 |
-
if meta is None:
|
| 138 |
-
meta = await fetch_meta(client, type, id)
|
| 139 |
-
|
| 140 |
-
meta['meta']['id'] = id
|
| 141 |
-
tmp_cache.set(id, meta, expire=cache_expire_time)
|
| 142 |
-
return meta
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# Helper function to fetch metadata
|
| 146 |
-
async def fetch_meta(client, type, id):
|
| 147 |
-
if 'tt' in id:
|
| 148 |
-
response = await client.get(f"https://some-meta-url/{type}/{id}.json")
|
| 149 |
-
meta = response.json()
|
| 150 |
-
return meta
|
| 151 |
-
# Additional logic for other types of IDs
|
| 152 |
-
return {}
|
| 153 |
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
# Utility
|
| 156 |
def decode_base64_url(encoded_url):
|
| 157 |
padding = '=' * (-len(encoded_url) % 4)
|
| 158 |
encoded_url += padding
|
| 159 |
decoded_bytes = base64.b64decode(encoded_url)
|
| 160 |
return decoded_bytes.decode('utf-8')
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
# Utility function to parse user settings
|
| 164 |
def parse_user_settings(user_settings: str) -> dict:
|
| 165 |
settings = user_settings.split(',')
|
| 166 |
-
|
| 167 |
for setting in settings:
|
| 168 |
key, value = setting.split('=')
|
| 169 |
-
|
| 170 |
-
return
|
| 171 |
-
|
| 172 |
|
| 173 |
if __name__ == '__main__':
|
| 174 |
import uvicorn
|
| 175 |
-
uvicorn.run(app, host='0.0.0.0', port=
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.templating import Jinja2Templates
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.responses import RedirectResponse, HTMLResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
+
from datetime import timedelta
|
| 8 |
+
from diskcache import Cache
|
| 9 |
+
from anime import kitsu, mal
|
| 10 |
+
import meta_merger
|
| 11 |
import translator
|
| 12 |
+
import asyncio
|
| 13 |
+
import httpx
|
| 14 |
import tmdb
|
| 15 |
import base64
|
| 16 |
|
| 17 |
# Settings
|
| 18 |
translator_version = 'v0.0.6'
|
|
|
|
| 19 |
FORCE_PREFIX = False
|
| 20 |
FORCE_META = False
|
| 21 |
+
REQUEST_TIMEOUT = 120
|
| 22 |
+
COMPATIBILITY_ID = ['tt', 'kitsu', 'mal']
|
| 23 |
|
| 24 |
+
# Cache set
|
|
|
|
| 25 |
tmp_cache = Cache('/tmp/meta')
|
| 26 |
tmp_cache.clear()
|
| 27 |
+
cache_expire_time = timedelta(hours=12).total_seconds()
|
| 28 |
|
| 29 |
+
# Server start
|
|
|
|
| 30 |
@asynccontextmanager
|
| 31 |
async def lifespan(app: FastAPI):
|
| 32 |
print('Started')
|
|
|
|
| 35 |
|
| 36 |
app = FastAPI(lifespan=lifespan)
|
| 37 |
templates = Jinja2Templates(directory="templates")
|
|
|
|
| 38 |
|
| 39 |
+
# Config CORS
|
| 40 |
app.add_middleware(
|
| 41 |
CORSMiddleware,
|
| 42 |
+
allow_origins=["*"],
|
| 43 |
allow_credentials=True,
|
| 44 |
+
allow_methods=["*"],
|
| 45 |
+
allow_headers=["*"],
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# HTML Configuration Interface
|
|
|
|
| 49 |
@app.get('/', response_class=HTMLResponse)
|
| 50 |
+
@app.get('/configure', response_class=HTMLResponse)
|
| 51 |
+
async def configure(request: Request):
|
| 52 |
+
return templates.TemplateResponse("configure.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Other Routes: Retain Your Existing Logic
|
| 55 |
@app.get('/{addon_url}/{user_settings}/manifest.json')
|
| 56 |
async def get_manifest(addon_url):
|
| 57 |
addon_url = decode_base64_url(addon_url)
|
|
|
|
| 59 |
response = await client.get(f"{addon_url}/manifest.json")
|
| 60 |
manifest = response.json()
|
| 61 |
|
| 62 |
+
is_translated = manifest.get('translated', False)
|
| 63 |
+
if not is_translated:
|
| 64 |
manifest['translated'] = True
|
| 65 |
manifest['t_language'] = 'it-IT'
|
| 66 |
manifest['name'] += ' 🇮🇹'
|
|
|
|
| 79 |
|
| 80 |
return manifest
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
@app.get('/{addon_url}/{user_settings}/catalog/{type}/{path:path}')
|
| 83 |
async def get_catalog(addon_url, type: str, user_settings: str, path: str):
|
| 84 |
+
if 'last-videos' in path:
|
| 85 |
+
return RedirectResponse(f"{cinemeta_url}/catalog/{type}/{path}")
|
| 86 |
+
|
| 87 |
user_settings = parse_user_settings(user_settings)
|
| 88 |
+
addon_url = decode_base64_url(addon_url)
|
| 89 |
|
| 90 |
async with httpx.AsyncClient(follow_redirects=True, timeout=REQUEST_TIMEOUT) as client:
|
| 91 |
response = await client.get(f"{addon_url}/catalog/{type}/{path}")
|
| 92 |
+
|
| 93 |
try:
|
| 94 |
catalog = response.json()
|
| 95 |
except:
|
| 96 |
+
print(response.text)
|
| 97 |
return {}
|
| 98 |
|
| 99 |
+
if 'metas' in catalog:
|
| 100 |
+
if type == 'anime':
|
| 101 |
+
await remove_duplicates(catalog)
|
| 102 |
+
tasks = [
|
| 103 |
+
tmdb.get_tmdb_data(client, item.get('imdb_id', item.get('id')), "imdb_id") for item in catalog['metas']
|
| 104 |
+
]
|
| 105 |
+
tmdb_details = await asyncio.gather(*tasks)
|
| 106 |
+
else:
|
| 107 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
new_catalog = translator.translate_catalog(catalog, tmdb_details, user_settings['sp'], user_settings['tr'])
|
| 110 |
+
return new_catalog
|
| 111 |
|
| 112 |
+
# Utility Functions (Unchanged from your logic)
|
| 113 |
def decode_base64_url(encoded_url):
|
| 114 |
padding = '=' * (-len(encoded_url) % 4)
|
| 115 |
encoded_url += padding
|
| 116 |
decoded_bytes = base64.b64decode(encoded_url)
|
| 117 |
return decoded_bytes.decode('utf-8')
|
| 118 |
|
| 119 |
+
async def remove_duplicates(catalog) -> None:
|
| 120 |
+
unique_items = []
|
| 121 |
+
seen_ids = set()
|
| 122 |
+
for item in catalog['metas']:
|
| 123 |
+
if 'kitsu' in item['id']:
|
| 124 |
+
item['imdb_id'], _ = await kitsu.convert_to_imdb(item['id'], item['type'])
|
| 125 |
+
elif 'mal_' in item['id']:
|
| 126 |
+
item['imdb_id'], _ = await mal.convert_to_imdb(item['id'].replace('_', ':'), item['type'])
|
| 127 |
+
if item['imdb_id'] not in seen_ids:
|
| 128 |
+
unique_items.append(item)
|
| 129 |
+
seen_ids.add(item['imdb_id'])
|
| 130 |
+
catalog['metas'] = unique_items
|
| 131 |
|
|
|
|
| 132 |
def parse_user_settings(user_settings: str) -> dict:
|
| 133 |
settings = user_settings.split(',')
|
| 134 |
+
parsed_settings = {}
|
| 135 |
for setting in settings:
|
| 136 |
key, value = setting.split('=')
|
| 137 |
+
parsed_settings[key] = value
|
| 138 |
+
return parsed_settings
|
|
|
|
| 139 |
|
| 140 |
if __name__ == '__main__':
|
| 141 |
import uvicorn
|
| 142 |
+
uvicorn.run(app, host='0.0.0.0', port=8081)
|