Add preview and update for YouTube.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 21 May 2020 12:11:19 +0000 (14:11 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 21 May 2020 12:11:19 +0000 (14:11 +0200)
src/archive/templates/archive/file_managed.html
src/youtube/templates/youtube/preview.html [new file with mode: 0644]
src/youtube/templatetags/youtube.py [new file with mode: 0644]
src/youtube/thumbnail.py
src/youtube/urls.py
src/youtube/views.py

index f7992e4..33a6646 100644 (file)
               {% endif %}
             </div>
             <div class="col-md-6">
+              <p>
+               <a href="{% url 'youtube_preview' audiobook.id %}">
+                  {% trans "Preview YouTube metadata" %}
+                </a>
+              </p>
               <form method="post" action="{% url 'youtube_publish' audiobook.id %}">
                 {% csrf_token %}
                 <input class="btn btn-primary" type="submit" value="{% trans "Publish on YouTube" %}" />
@@ -78,7 +83,6 @@
                 {% csrf_token %}
                 <input class="btn btn-secondary" type="submit" value="{% trans "Convert without publishing" %}" />
               </form>
-
             </div>
           </div>
         </td></tr>
         <h2>{% trans "YouTube" %}</h2>
       </div>
       <div class="card-body">
-        {% if audiobook.youtube_file %}
-          <p><a href="{% url 'download' audiobook.id 'mkv' %}">{% trans "Download YouTube file." %}</a></p>
+        {% if audiobook.youtube_id %}
+          {% if audiobook.youtube_id %}
+            <p>
+              <a href="https://youtu.be/{{ audiobook.youtube_id }}" target="_blank">{% trans "See on YouTube" %}</a>
+            </p>
+            <form method="post" action="{% url 'youtube_update' audiobook.id %}">
+              {% csrf_token %}
+              <input class="btn btn-secondary" type="submit" value="{% trans "Update YouTube metadata" %}">
+            </form>
+          {% endif %}
           {% if audiobook.youtube_published %}
             <p>{% trans "Published:" %} {{ audiobook.youtube_published }}</a></p>
             {% if audiobook.get_youtube_published_tags.tags %}
diff --git a/src/youtube/templates/youtube/preview.html b/src/youtube/templates/youtube/preview.html
new file mode 100644 (file)
index 0000000..c017265
--- /dev/null
@@ -0,0 +1,27 @@
+{% extends "archive/base.html" %}
+
+
+{% block content %}
+  <div class="card mt-4">
+    <div class="card-header">
+      <h2>
+        <a href="{% url 'file' object.id %}">
+          &larr; {{ object.title }}
+        </a>
+      </h2>
+    </div>
+    <div class="card-body">
+      <img src="{% url 'youtube_thumbnail' object.id %}" style="width:100%">
+    </div>
+  </div>
+
+
+  <div class="card mt-4 mb-4">
+    <div class="card-header">
+      <h2>{{ title }}</h2>
+    </div>
+    <div class="card-body">
+      {{ description|linebreaksbr }}
+    </div>
+  </div>
+{% endblock %}
diff --git a/src/youtube/templatetags/youtube.py b/src/youtube/templatetags/youtube.py
new file mode 100644 (file)
index 0000000..bb5b34b
--- /dev/null
@@ -0,0 +1,15 @@
+import django.template
+
+
+register = django.template.Library()
+
+
+@register.simple_tag
+def last_name(author):
+    # TODO: we should reference the Catalogue to get proper last name.
+    if author.lower() == 'autor nieznany':
+        return
+    parts = author.split(' ')
+    for part in reversed(parts):
+        if part.startswith('('): continue
+        return part
index 3f67cac..a6cd9c5 100644 (file)
@@ -53,7 +53,6 @@ def draw_version(img, d, context, get_font_path):
             realheight = draw.textsize(line, font=font)[1]
             if cursor + realheight > newimg.size[1]:
                 return False
-            print('x', draw.text((0, cursor), line, font=font, fill=item.get('color')))
             cursor += item['line-height']
 
     img.paste(newimg, (d.get('x', 0), d.get('y', 0)), newimg)
index 8dbd3d8..58071e5 100644 (file)
@@ -6,4 +6,6 @@ urlpatterns = [
     url(r'^publish/(\d+)/$', views.publish, name="youtube_publish"),
     url(r'^convert/(\d+)/$', views.publish, {'publish': False}, name="youtube_convert"),
     path('thumbnail/<int:aid>/', views.thumbnail, name='youtube_thumbnail'),
+    path('preview/<int:pk>/', views.Preview.as_view(), name="youtube_preview"),
+    path('update/<int:pk>/', views.Update.as_view(), name="youtube_update"),
 ]
index 4e59f5f..73eecd3 100644 (file)
@@ -2,7 +2,11 @@ from django.contrib.auth.decorators import permission_required
 from django.http import HttpResponse
 from django.shortcuts import redirect, get_object_or_404
 from django.urls import reverse
+from django.utils.decorators import method_decorator
+from django.views import View
 from django.views.decorators.http import require_POST
+from django.views.generic import DetailView
+from django.views.generic.detail import SingleObjectMixin
 from archive.constants import status
 from archive.models import Audiobook
 from . import models, tasks
@@ -27,3 +31,26 @@ def thumbnail(request, aid):
     yt = models.YouTube.objects.first()
     buf = yt.prepare_thumbnail(audiobook)
     return HttpResponse(buf.getvalue(), content_type='image/png')
+
+
+class Preview(DetailView):
+    model = Audiobook
+    template_name = 'youtube/preview.html'
+
+    def get_context_data(self, **kwargs):
+        ctx = super().get_context_data(**kwargs)
+        yt = models.YouTube.objects.first()
+        ctx['data'] = yt.get_data(ctx['object'])
+        ctx['title'] = yt.get_title(ctx['object'])
+        ctx['description'] = yt.get_description(ctx['object'])
+        return ctx
+
+
+@method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
+class Update(SingleObjectMixin, View):
+    model = Audiobook
+
+    def post(self, request, pk):
+        yt = models.YouTube.objects.first()
+        yt.update_data(self.get_object())
+        return redirect(reverse('file', args=[pk]))