created basic search endpoint

This commit is contained in:
Francesco Grazioso 2024-04-16 00:07:16 +02:00
parent 9b7598bbe2
commit 031371897d
6 changed files with 48 additions and 4 deletions

View File

@ -37,6 +37,8 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"endpoints",
"rest_framework",
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -15,8 +15,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("api/", include("endpoints.urls")),
] ]

9
api/endpoints/urls.py Normal file
View File

@ -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

View File

@ -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"})

View File

@ -2,6 +2,7 @@
"""Django's command-line utility for administrative tasks.""" """Django's command-line utility for administrative tasks."""
import os import os
import sys import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
def main(): def main():

View File

@ -7,3 +7,4 @@ pycryptodome
m3u8 m3u8
lxml lxml
django==4.2.11 django==4.2.11
djangorestframework==3.15.1