diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md new file mode 100644 index 0000000..16092fd --- /dev/null +++ b/tubearchivist/api/README.md @@ -0,0 +1,14 @@ +# TubeArchivist API endpoints +Documentation of available API endpoints + +## Videos +/api/video/\/ + +## Channels +/api/channel/\/ + +## Playlists +/api/playlist/\/ + +## Download Queue +/api/download/\/ diff --git a/tubearchivist/api/__init__.py b/tubearchivist/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tubearchivist/api/admin.py b/tubearchivist/api/admin.py new file mode 100644 index 0000000..4fd5490 --- /dev/null +++ b/tubearchivist/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin # noqa: F401 + +# Register your models here. diff --git a/tubearchivist/api/apps.py b/tubearchivist/api/apps.py new file mode 100644 index 0000000..9acf5fb --- /dev/null +++ b/tubearchivist/api/apps.py @@ -0,0 +1,10 @@ +"""apps file for api package""" + +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + """app config""" + + default_auto_field = "django.db.models.BigAutoField" + name = "api" diff --git a/tubearchivist/api/migrations/__init__.py b/tubearchivist/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tubearchivist/api/models.py b/tubearchivist/api/models.py new file mode 100644 index 0000000..500340a --- /dev/null +++ b/tubearchivist/api/models.py @@ -0,0 +1,5 @@ +"""api models""" + +from django.db import models # noqa: F401 + +# Create your models here. diff --git a/tubearchivist/api/serializers.py b/tubearchivist/api/serializers.py new file mode 100644 index 0000000..e69de29 diff --git a/tubearchivist/api/tests.py b/tubearchivist/api/tests.py new file mode 100644 index 0000000..e55d689 --- /dev/null +++ b/tubearchivist/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase # noqa: F401 + +# Create your tests here. diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py new file mode 100644 index 0000000..7f48576 --- /dev/null +++ b/tubearchivist/api/urls.py @@ -0,0 +1,33 @@ +"""all api urls""" + +from api.views import ( + ChannelApiView, + DownloadApiView, + PlaylistApiView, + VideoApiView, +) +from django.contrib.auth.decorators import login_required +from django.urls import path + +urlpatterns = [ + path( + "video//", + login_required(VideoApiView.as_view()), + name="api-video", + ), + path( + "channel//", + login_required(ChannelApiView.as_view()), + name="api-channel", + ), + path( + "playlist//", + login_required(PlaylistApiView.as_view()), + name="api-playlist", + ), + path( + "download//", + login_required(DownloadApiView.as_view()), + name="api-download", + ), +] diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py new file mode 100644 index 0000000..f250bd3 --- /dev/null +++ b/tubearchivist/api/views.py @@ -0,0 +1,95 @@ +"""all API views""" + +import requests +from home.src.config import AppConfig +from rest_framework.response import Response +from rest_framework.views import APIView + + +class ApiBaseView(APIView): + """base view to inherit from""" + + search_base = False + + def __init__(self): + super().__init__() + self.response = False + self.status_code = False + self.context = False + + def config_builder(self): + """build confic context""" + default_conf = AppConfig().config + self.context = { + "es_url": default_conf["application"]["es_url"], + "es_auth": default_conf["application"]["es_auth"], + } + + def get_document(self, document_id): + """get single document from es""" + es_url = self.context["es_url"] + url = f"{es_url}{self.search_base}{document_id}" + print(url) + response = requests.get(url, auth=self.context["es_auth"]) + self.response = response.json()["_source"] + self.status_code = response.status_code + + +class VideoApiView(ApiBaseView): + """resolves to /api/video// + GET: returns metadata dict of video + """ + + search_base = "/ta_video/_doc/" + + def get(self, request, video_id): + # pylint: disable=unused-argument + """get request""" + self.config_builder() + self.get_document(video_id) + return Response(self.response) + + +class ChannelApiView(ApiBaseView): + """resolves to /api/channel// + GET: returns metadata dict of channel + """ + + search_base = "/ta_channel/_doc/" + + def get(self, request, channel_id): + # pylint: disable=unused-argument + """get request""" + self.config_builder() + self.get_document(channel_id) + return Response(self.response) + + +class PlaylistApiView(ApiBaseView): + """resolves to /api/playlist// + GET: returns metadata dict of playlist + """ + + search_base = "/ta_playlist/_doc/" + + def get(self, request, playlist_id): + # pylint: disable=unused-argument + """get request""" + self.config_builder() + self.get_document(playlist_id) + return Response(self.response) + + +class DownloadApiView(ApiBaseView): + """resolves to /api/download// + GET: returns metadata dict of an item in the download queue + """ + + search_base = "/ta_download/_doc/" + + def get(self, request, video_id): + # pylint: disable=unused-argument + """get request""" + self.config_builder() + self.get_document(video_id) + return Response(self.response) diff --git a/tubearchivist/config/settings.py b/tubearchivist/config/settings.py index 8324c89..21074b3 100644 --- a/tubearchivist/config/settings.py +++ b/tubearchivist/config/settings.py @@ -44,6 +44,8 @@ INSTALLED_APPS = [ "whitenoise.runserver_nostatic", "django.contrib.staticfiles", "django.contrib.humanize", + "rest_framework", + "api", ] MIDDLEWARE = [ diff --git a/tubearchivist/config/urls.py b/tubearchivist/config/urls.py index b857f01..11a1ed7 100644 --- a/tubearchivist/config/urls.py +++ b/tubearchivist/config/urls.py @@ -18,5 +18,6 @@ from django.urls import include, path urlpatterns = [ path("", include("home.urls")), + path("api/", include("api.urls")), path("admin/", admin.site.urls), ] diff --git a/tubearchivist/requirements.txt b/tubearchivist/requirements.txt index a3bc158..3ebbf26 100644 --- a/tubearchivist/requirements.txt +++ b/tubearchivist/requirements.txt @@ -1,6 +1,7 @@ beautifulsoup4==4.10.0 celery==5.2.3 Django==4.0.1 +djangorestframework==3.13.1 Pillow==9.0.0 redis==4.1.0 requests==2.27.1