+
+ 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):
+ img = create_thumbnail(
+ self.thumbnail_template.path,
+ self.thumbnail_definition,
+ {
+ "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
+ "title": audiobook.book['title'],
+ "part": (audiobook.youtube_volume or audiobook.part_name).strip(),
+ },
+ 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