+
+ def update_thumbnail(self, audiobook):
+ thumbnail = self.prepare_thumbnail(audiobook)
+ response = youtube_call(
+ "POST",
+ "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
+ params={'videoId': audiobook.youtube_id},
+ data=thumbnail.getvalue(),
+ )
+
+ 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,
+ {
+ "author": ', '.join((a['name'] for a in apidata['authors'])),
+ "title": apidata['title'],
+ },
+ lambda name: Font.objects.get(name=name).truetype.path
+ )
+ buf = io.BytesIO()
+ img.save(buf, format='PNG')
+ return buf
+
+
+class Card(models.Model):
+ youtube = models.ForeignKey(YouTube, models.CASCADE)
+ order = models.SmallIntegerField()
+ image = models.FileField(upload_to='youtube/card')
+ duration = models.FloatField()
+
+ class Meta:
+ ordering = ('order', )
+
+
+class Font(models.Model):
+ name = models.CharField(max_length=255, unique=True)
+ truetype = models.FileField(upload_to='youtube/font')
+
+ def __str__(self):
+ return self.name