From 031371897d6986fedf019ebec8df7c2c28849e08 Mon Sep 17 00:00:00 2001 From: Francesco Grazioso Date: Tue, 16 Apr 2024 00:07:16 +0200 Subject: [PATCH] created basic search endpoint --- api/api/settings.py | 2 ++ api/api/urls.py | 3 ++- api/endpoints/urls.py | 9 +++++++++ api/endpoints/views.py | 34 ++++++++++++++++++++++++++++++++-- api/manage.py | 1 + requirements.txt | 3 ++- 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 api/endpoints/urls.py diff --git a/api/api/settings.py b/api/api/settings.py index 7a085db..b7dd350 100644 --- a/api/api/settings.py +++ b/api/api/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "endpoints", + "rest_framework", ] MIDDLEWARE = [ diff --git a/api/api/urls.py b/api/api/urls.py index 1fde757..32879db 100644 --- a/api/api/urls.py +++ b/api/api/urls.py @@ -15,8 +15,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), + path("api/", include("endpoints.urls")), ] diff --git a/api/endpoints/urls.py b/api/endpoints/urls.py new file mode 100644 index 0000000..091d6ec --- /dev/null +++ b/api/endpoints/urls.py @@ -0,0 +1,9 @@ +from rest_framework import routers + +from .views import SearchView + +router = routers.DefaultRouter() + +router.register(r"search", SearchView, basename="search") + +urlpatterns = router.urls diff --git a/api/endpoints/views.py b/api/endpoints/views.py index 91ea44a..374cf77 100644 --- a/api/endpoints/views.py +++ b/api/endpoints/views.py @@ -1,3 +1,33 @@ -from django.shortcuts import render +from rest_framework import viewsets +from rest_framework.response import Response -# Create your views here. +from Src.Api import search, get_version_and_domain +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") + + 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 + 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 + }) + + return Response({"media": data_to_return}) + + return Response({"error": "No media found with that search query"}) diff --git a/api/manage.py b/api/manage.py index 7fbe893..50801f0 100755 --- a/api/manage.py +++ b/api/manage.py @@ -2,6 +2,7 @@ """Django's command-line utility for administrative tasks.""" import os import sys +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) def main(): diff --git a/requirements.txt b/requirements.txt index 87b1173..dbc42c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ ffmpeg-python pycryptodome m3u8 lxml -django==4.2.11 \ No newline at end of file +django==4.2.11 +djangorestframework==3.15.1 \ No newline at end of file