More YT details.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 20 May 2020 11:54:14 +0000 (13:54 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 20 May 2020 11:54:14 +0000 (13:54 +0200)
src/archive/models.py
src/youtube/migrations/0010_auto_20200520_1353.py [new file with mode: 0644]
src/youtube/models.py

index 34fc1f4..4a805f2 100644 (file)
@@ -3,8 +3,10 @@ import os.path
 
 from django.db import models
 from time import sleep
 
 from django.db import models
 from time import sleep
+from django.utils.functional import cached_property
 from django.utils.translation import gettext_lazy as _
 from django_pglocks import advisory_lock
 from django.utils.translation import gettext_lazy as _
 from django_pglocks import advisory_lock
+import requests
 from archive.constants import status
 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
 from archive.utils import OverwriteStorage, sha1_file
 from archive.constants import status
 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
 from archive.utils import OverwriteStorage, sha1_file
@@ -153,3 +155,9 @@ class Audiobook(models.Model):
             tags['flac_sha1'] = self.source_sha1
         return tags
 
             tags['flac_sha1'] = self.source_sha1
         return tags
 
+    @cached_property
+    def book(self):
+        slug = self.url.rstrip('/').rsplit('/', 1)[-1]
+        apidata = requests.get(f'https://wolnelektury.pl/api/books/{slug}/').json()
+        return apidata
+
diff --git a/src/youtube/migrations/0010_auto_20200520_1353.py b/src/youtube/migrations/0010_auto_20200520_1353.py
new file mode 100644 (file)
index 0000000..adc1bae
--- /dev/null
@@ -0,0 +1,24 @@
+# Generated by Django 3.0.4 on 2020-05-20 13:53
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('youtube', '0009_auto_20200518_1656'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='youtube',
+            name='privacy_status',
+            field=models.CharField(choices=[('public', 'public'), ('unlisted', 'unlisted'), ('private', 'private')], default='unlisted', max_length=16),
+            preserve_default=False,
+        ),
+        migrations.AlterField(
+            model_name='youtube',
+            name='category',
+            field=models.IntegerField(blank=True, choices=[(27, 'Edukacja')], null=True),
+        ),
+    ]
index 6306a49..ecc05e4 100644 (file)
@@ -4,7 +4,6 @@ from tempfile import NamedTemporaryFile
 from django.db import models
 from django.utils.translation import gettext_lazy as _
 from django.template import Template, Context
 from django.db import models
 from django.utils.translation import gettext_lazy as _
 from django.template import Template, Context
-import requests
 from apiclient import youtube_call
 from .utils import (
     concat_audio,
 from apiclient import youtube_call
 from .utils import (
     concat_audio,
@@ -22,42 +21,50 @@ from .thumbnail import create_thumbnail
 class YouTube(models.Model):
     title_template = models.CharField(max_length=1024, blank=True)
     description_template = models.TextField(blank=True)
 class YouTube(models.Model):
     title_template = models.CharField(max_length=1024, blank=True)
     description_template = models.TextField(blank=True)
-    category = models.IntegerField(null=True, blank=True)  # get categories
+    category = models.IntegerField(null=True, blank=True, choices=[
+        (27, 'Edukacja'),
+    ])
     intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
     outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
     loop_card = models.FileField(upload_to='youtube/card', blank=True)
     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
     thumbnail_definition = models.TextField(blank=True)
     intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
     outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
     loop_card = models.FileField(upload_to='youtube/card', blank=True)
     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
     thumbnail_definition = models.TextField(blank=True)
+    privacy_status = models.CharField(max_length=16, choices=[
+        ('public', _('public')),
+        ('unlisted', _('unlisted')),
+        ('private', _('private')),
+    ])
     genres = models.CharField(max_length=2048, blank=True)
 
     class Meta:
         verbose_name = _("YouTube configuration")
         verbose_name_plural = _("YouTube configurations")
 
     genres = models.CharField(max_length=2048, blank=True)
 
     class Meta:
         verbose_name = _("YouTube configuration")
         verbose_name_plural = _("YouTube configurations")
 
-    def publish(self, audiobook, path):
-        ctx = Context(dict(audiobook=audiobook))
-        description = Template(self.description_template).render(ctx)
-        title = Template(self.title_template).render(ctx)
-        privacy = 'private'
+    def get_context(self, audiobook):
+        return Context(dict(audiobook=audiobook))
+
+    def get_description(self, audiobook):
+        return Template(self.description_template).render(self.get_context(audiobook))
 
 
-        data = dict(
+    def get_title(self, audiobook):
+        return Template(self.title_template).render(self.get_context(audiobook))
+
+    def get_data(self, audiobook):
+        return dict(
             snippet=dict(
             snippet=dict(
-                title=title,
-                description=description,
-                # tags=tags,
-                # categoryId=category,
-                # defaultLanguage
+                title=self.get_title(audiobook),
+                description=self.get_description(audiobook),
+                categoryId=self.category,
+                defaultLanguage='pl'
             ),
             status=dict(
             ),
             status=dict(
-                privacyStatus=privacy,
-                # license
-                # selfDeclaredMadeForKids
+                privacyStatus=self.privacy_status,
             ),
             ),
-            # recordingDetails=dict(
-            # recordingDate
-            # ),
         )
         )
+
+    def publish(self, audiobook, path):
+        data = self.get_data()
         part = ",".join(data.keys())
 
         with open(path, "rb") as f:
         part = ",".join(data.keys())
 
         with open(path, "rb") as f:
@@ -75,6 +82,17 @@ class YouTube(models.Model):
         self.update_thumbnail(audiobook)
         return response
 
         self.update_thumbnail(audiobook)
         return response
 
+    def update_data(self, audiobook):
+        data = self.get_data()
+        data['id'] = audiobook.youtube_id
+        part = ",".join(data.keys())
+        youtube_call(
+            "PUT",
+            "https://www.googleapis.com/youtube/v3/videos",
+            params={"part": part},
+            json=data
+        )       
+
     def prepare_file(self, input_path, output_path=None):
         audio = self.prepare_audio(input_path)
         duration = self.get_duration(input_path)
     def prepare_file(self, input_path, output_path=None):
         audio = self.prepare_audio(input_path)
         duration = self.get_duration(input_path)
@@ -155,14 +173,12 @@ class YouTube(models.Model):
         )
 
     def prepare_thumbnail(self, audiobook):
         )
 
     def prepare_thumbnail(self, audiobook):
-        slug = audiobook.url.rstrip('/').rsplit('/', 1)[-1]
-        apidata = requests.get(f'https://wolnelektury.pl/api/books/{slug}/').json()
         img = create_thumbnail(
             self.thumbnail_template.path,
             self.thumbnail_definition,
             {
         img = create_thumbnail(
             self.thumbnail_template.path,
             self.thumbnail_definition,
             {
-                "author": ', '.join((a['name'] for a in apidata['authors'])),
-                "title": apidata['title'],
+                "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
+                "title": audiobook.book['title'],
             },
             lambda name: Font.objects.get(name=name).truetype.path
         )
             },
             lambda name: Font.objects.get(name=name).truetype.path
         )