Spaces:
Running
Running
Update search_by_name.py
Browse files- search_by_name.py +76 -76
search_by_name.py
CHANGED
|
@@ -1,77 +1,77 @@
|
|
| 1 |
-
from fastapi import APIRouter
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
import os
|
| 4 |
-
from typing import Optional
|
| 5 |
-
from
|
| 6 |
-
import requests
|
| 7 |
-
from
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
tmdb_api_key=os.getenv('tmdb_api_key')
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def search_it(q:str):
|
| 15 |
-
data_list=[]
|
| 16 |
-
url="https://api.themoviedb.org/3/search/multi"
|
| 17 |
-
response=requests.get(
|
| 18 |
-
url=url,
|
| 19 |
-
params={
|
| 20 |
-
'api_key':tmdb_api_key,
|
| 21 |
-
'query':q
|
| 22 |
-
|
| 23 |
-
}
|
| 24 |
-
)
|
| 25 |
-
data=response.json()
|
| 26 |
-
result=data['results']
|
| 27 |
-
|
| 28 |
-
length_total=len(result)
|
| 29 |
-
if length_total>0:
|
| 30 |
-
if length_total>3:
|
| 31 |
-
length_total=3
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
data_list=[
|
| 38 |
-
{
|
| 39 |
-
'Id':result[i]['id'],
|
| 40 |
-
'Title':result[i].get('title' ) or result[i].get('name'),
|
| 41 |
-
'media_type':result[i].get('media_type',[]),
|
| 42 |
-
'Release_date':result[i].get('release_date') or result[i].get('first_air_date'),
|
| 43 |
-
'Genre':'|'.join([
|
| 44 |
-
tmdb_movie_genres.get(genre_id, tmdb_tv_genres.get(genre_id, 'Unknown'))
|
| 45 |
-
for genre_id in (result[i].get('genre_ids') or [])
|
| 46 |
-
]) or 'None'
|
| 47 |
-
,
|
| 48 |
-
'Popularity':result[i]['popularity'],
|
| 49 |
-
'Poster_path':(
|
| 50 |
-
f"https://image.tmdb.org/t/p/w500{result[i]['poster_path']}"
|
| 51 |
-
if result[i].get('poster_path')
|
| 52 |
-
else "https://znuiuhxyhpgqmdftansc.supabase.co/storage/v1/object/public/user_files/images/no_image_avail.png"
|
| 53 |
-
)
|
| 54 |
-
}
|
| 55 |
-
for i in range(0,length_total)]
|
| 56 |
-
else:
|
| 57 |
-
data_list=['No Movie Found']
|
| 58 |
-
return data_list
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
search_any_app=APIRouter()
|
| 68 |
-
@search_any_app.get('/MovieSphere/Search')
|
| 69 |
-
|
| 70 |
-
def search_movie(q:Optional[str]=None,text:Optional[str]=None):
|
| 71 |
-
if text is not None and q is None:
|
| 72 |
-
q=ask_ai_movie(text)
|
| 73 |
-
|
| 74 |
-
result=search_it(q)
|
| 75 |
-
return{
|
| 76 |
-
'MovieSphere':result
|
| 77 |
}
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
from typing import Optional
|
| 5 |
+
from ask_ai import ask_ai_movie
|
| 6 |
+
import requests
|
| 7 |
+
from genre_dict import tmdb_movie_genres,tmdb_tv_genres
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
tmdb_api_key=os.getenv('tmdb_api_key')
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def search_it(q:str):
|
| 15 |
+
data_list=[]
|
| 16 |
+
url="https://api.themoviedb.org/3/search/multi"
|
| 17 |
+
response=requests.get(
|
| 18 |
+
url=url,
|
| 19 |
+
params={
|
| 20 |
+
'api_key':tmdb_api_key,
|
| 21 |
+
'query':q
|
| 22 |
+
|
| 23 |
+
}
|
| 24 |
+
)
|
| 25 |
+
data=response.json()
|
| 26 |
+
result=data['results']
|
| 27 |
+
|
| 28 |
+
length_total=len(result)
|
| 29 |
+
if length_total>0:
|
| 30 |
+
if length_total>3:
|
| 31 |
+
length_total=3
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
data_list=[
|
| 38 |
+
{
|
| 39 |
+
'Id':result[i]['id'],
|
| 40 |
+
'Title':result[i].get('title' ) or result[i].get('name'),
|
| 41 |
+
'media_type':result[i].get('media_type',[]),
|
| 42 |
+
'Release_date':result[i].get('release_date') or result[i].get('first_air_date'),
|
| 43 |
+
'Genre':'|'.join([
|
| 44 |
+
tmdb_movie_genres.get(genre_id, tmdb_tv_genres.get(genre_id, 'Unknown'))
|
| 45 |
+
for genre_id in (result[i].get('genre_ids') or [])
|
| 46 |
+
]) or 'None'
|
| 47 |
+
,
|
| 48 |
+
'Popularity':result[i]['popularity'],
|
| 49 |
+
'Poster_path':(
|
| 50 |
+
f"https://image.tmdb.org/t/p/w500{result[i]['poster_path']}"
|
| 51 |
+
if result[i].get('poster_path')
|
| 52 |
+
else "https://znuiuhxyhpgqmdftansc.supabase.co/storage/v1/object/public/user_files/images/no_image_avail.png"
|
| 53 |
+
)
|
| 54 |
+
}
|
| 55 |
+
for i in range(0,length_total)]
|
| 56 |
+
else:
|
| 57 |
+
data_list=['No Movie Found']
|
| 58 |
+
return data_list
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
search_any_app=APIRouter()
|
| 68 |
+
@search_any_app.get('/MovieSphere/Search')
|
| 69 |
+
|
| 70 |
+
def search_movie(q:Optional[str]=None,text:Optional[str]=None):
|
| 71 |
+
if text is not None and q is None:
|
| 72 |
+
q=ask_ai_movie(text)
|
| 73 |
+
|
| 74 |
+
result=search_it(q)
|
| 75 |
+
return{
|
| 76 |
+
'MovieSphere':result
|
| 77 |
}
|