+ 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
+