diff --git a/Src/Api/Streamingcommunity/Core/Class/SearchType.py b/Src/Api/Streamingcommunity/Core/Class/SearchType.py index 115677d..682aeaa 100644 --- a/Src/Api/Streamingcommunity/Core/Class/SearchType.py +++ b/Src/Api/Streamingcommunity/Core/Class/SearchType.py @@ -41,6 +41,37 @@ class MediaItem: def __str__(self): return f"MediaItem(id={self.id}, slug='{self.slug}', name='{self.name}', type='{self.type}', score='{self.score}', sub_ita={self.sub_ita}, last_air_date='{self.last_air_date}', seasons_count={self.seasons_count}, images={self.images})" + @property + def to_dict(self) -> dict: + """ + Convert the MediaItem to a dictionary. + + Returns: + dict: The MediaItem as a dictionary. + """ + return { + "id": self.id, + "slug": self.slug, + "name": self.name, + "type": self.type, + "score": self.score, + "sub_ita": self.sub_ita, + "last_air_date": self.last_air_date, + "seasons_count": self.seasons_count, + "images": [image.__dict__ for image in self.images], + "comment": self.comment + } + + @property + def get_site_id(self) -> str: + """ + Get the site ID of the media item. + + Returns: + int: The site ID of the media item. + """ + return f"{self.id}-{self.slug}" + class MediaManager: def __init__(self): diff --git a/api/endpoints/views.py b/api/endpoints/views.py index 374cf77..3a90c3c 100644 --- a/api/endpoints/views.py +++ b/api/endpoints/views.py @@ -1,3 +1,5 @@ +import json + from rest_framework import viewsets from rest_framework.response import Response @@ -6,28 +8,44 @@ from Src.Api.site import media_search_manager, anime_search class SearchView(viewsets.ViewSet): - def list(self, request): - search_query = request.query_params.get("search_terms") - type_search = request.query_params.get("type") + def get_queryset(self): + # Questa funzione viene chiamata prima di list e retrieve + self.search_query = self.request.query_params.get("search_terms") + self.type_search = self.request.query_params.get("type") media_search_manager.media_list = [] - site_version, domain = get_version_and_domain() - if type_search == "film": - len_database = search(search_query, domain) - elif type_search == "anime": - len_database = anime_search(search_query) - if len_database != 0: - media_list = media_search_manager.media_list + self.site_version, self.domain = get_version_and_domain() + self.len_database = 0 + if self.type_search == "film": + self.len_database = search(self.search_query, self.domain) + elif self.type_search == "anime": + self.len_database = anime_search(self.search_query) + + return media_search_manager.media_list + + def list(self, request): + # Ottieni il queryset dalle impostazioni comuni + media_list = self.get_queryset() + + if self.len_database != 0: data_to_return = [] - for i, media in enumerate(media_list): - data_to_return.append({ - "id": i, - "name": media.name, - "type": media.type, - "score": media.score, - "last_air_date": media.last_air_date - }) + for _, media in enumerate(media_list): + data_to_return.append(media.to_dict) return Response({"media": data_to_return}) return Response({"error": "No media found with that search query"}) + + def retrieve(self, request, pk=None): + # Ottieni il queryset dalle impostazioni comuni + media_list = self.get_queryset() + + # FIXME: Non funziona perchè si aspetta un termine di ricerca da usare nel queryset, + # implementare una ricerca per ID nell'api per permettere questo metodo + + # Cerca il media in base allo site_id + media = next((m for m in media_list if m.get_site_id == pk), None) + if media: + return Response(media.to_dict) + else: + return Response({"error": "Media not found"}, status=404)