From 57a9fff82b94736598a91ca72d404d9b2de1e0b4 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Fri, 19 Aug 2022 12:52:37 +0200 Subject: [PATCH] Add API endpoint for searching (#303) * Add API endpoint for searching + use it in frontend * Fix linting warnings * Remove multisearch API call * Avoid 301 and fix up multiline comment --- tubearchivist/api/urls.py | 6 ++++++ tubearchivist/api/views.py | 20 ++++++++++++++++++++ tubearchivist/home/src/frontend/api_calls.py | 9 --------- tubearchivist/static/script.js | 5 ++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index eedcbe2..3463344 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -12,6 +12,7 @@ from api.views import ( PlaylistApiListView, PlaylistApiVideoView, PlaylistApiView, + SearchView, TaskApiView, VideoApiListView, VideoApiView, @@ -93,4 +94,9 @@ urlpatterns = [ CookieView.as_view(), name="api-cookie", ), + path( + "search/", + SearchView.as_view(), + name="api-search", + ), ] diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index b99ab6a..94e6af6 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -5,6 +5,7 @@ from api.src.task_processor import TaskHandler from home.src.download.queue import PendingInteract from home.src.download.yt_dlp_base import CookieHandler from home.src.es.connect import ElasticWrap +from home.src.frontend.searching import SearchForm from home.src.index.generic import Pagination from home.src.index.video import SponsorBlock from home.src.ta.config import AppConfig @@ -526,3 +527,22 @@ class CookieView(ApiBaseView): message = {"cookie_import": "done", "cookie_validated": validated} return Response(message) + + +class SearchView(ApiBaseView): + """resolves to /api/search/ + GET: run a search with the string in the ?query parameter + """ + + @staticmethod + def get(request): + """handle get request + search through all indexes""" + search_query = request.GET.get("query", None) + if search_query is None: + return Response( + {"message": "no search query specified"}, status=400 + ) + print("searching for: " + search_query) + search_results = SearchForm().multi_search(search_query) + return Response(search_results) diff --git a/tubearchivist/home/src/frontend/api_calls.py b/tubearchivist/home/src/frontend/api_calls.py index 9e213b5..d8325fb 100644 --- a/tubearchivist/home/src/frontend/api_calls.py +++ b/tubearchivist/home/src/frontend/api_calls.py @@ -9,7 +9,6 @@ from home.src.download.subscriptions import ( ChannelSubscription, PlaylistSubscription, ) -from home.src.frontend.searching import SearchForm from home.src.frontend.watched import WatchState from home.src.index.channel import YoutubeChannel from home.src.index.playlist import YoutubePlaylist @@ -74,7 +73,6 @@ class PostData: "db-backup": self._db_backup, "db-restore": self._db_restore, "fs-rescan": self._fs_rescan, - "multi_search": self._multi_search, "delete-video": self._delete_video, "delete-channel": self._delete_channel, "delete-playlist": self._delete_playlist, @@ -286,13 +284,6 @@ class PostData: rescan_filesystem.delay() return {"success": True} - def _multi_search(self): - """search through all indexes""" - search_query = self.exec_val - print("searching for: " + search_query) - search_results = SearchForm().multi_search(search_query) - return search_results - def _delete_video(self): """delete media file, metadata and thumb""" youtube_id = self.exec_val diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js index ddb96db..178ecec 100644 --- a/tubearchivist/static/script.js +++ b/tubearchivist/static/script.js @@ -803,7 +803,6 @@ function searchMulti(query) { clearTimeout(searchTimeout); searchTimeout = setTimeout(function () { if (query.length > 1) { - var payload = JSON.stringify({'multi_search': query}); var http = new XMLHttpRequest(); http.onreadystatechange = function() { if (http.readyState === 4) { @@ -811,10 +810,10 @@ function searchMulti(query) { populateMultiSearchResults(response.results, response.queryType); } }; - http.open("POST", "/process/", true); + http.open("GET", `/api/search/?query=${query}`, true); http.setRequestHeader("X-CSRFToken", getCookie("csrftoken")); http.setRequestHeader("Content-type", "application/json"); - http.send(payload); + http.send(); } }, 500); }