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
This commit is contained in:
PrivateGER 2022-08-19 12:52:37 +02:00 committed by GitHub
parent 25877cf016
commit 57a9fff82b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 12 deletions

View File

@ -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",
),
]

View File

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

View File

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

View File

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