3 from django.db import models
4 from django.utils.translation import gettext_lazy as _
5 from django.template import Template, Context
7 from apiclient import youtube_call
19 from .thumbnail import create_thumbnail
22 YOUTUBE_TITLE_LIMIT = 100
25 class YouTube(models.Model):
26 name = models.CharField(max_length=255)
27 title_template = models.CharField(max_length=1024, blank=True)
28 description_template = models.TextField(blank=True)
29 category = models.IntegerField(null=True, blank=True, choices=[
32 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
33 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
34 loop_card = models.FileField(upload_to='youtube/card', blank=True)
35 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
36 privacy_status = models.CharField(max_length=16, choices=[
37 ('public', _('public')),
38 ('unlisted', _('unlisted')),
39 ('private', _('private')),
41 genres = models.CharField(max_length=2048, blank=True)
44 verbose_name = _("YouTube configuration")
45 verbose_name_plural = _("YouTube configurations")
50 def get_context(self, audiobook):
55 def get_description(self, audiobook):
56 return Template(self.description_template).render(self.get_context(audiobook))
58 def get_title(self, audiobook):
59 return Template(self.title_template).render(self.get_context(audiobook))[:YOUTUBE_TITLE_LIMIT]
61 def get_data(self, audiobook):
64 title=self.get_title(audiobook),
65 description=self.get_description(audiobook),
66 categoryId=self.category,
68 defaultAudioLanguage='pl',
71 privacyStatus=self.privacy_status,
75 def publish(self, audiobook, path):
76 data = self.get_data(audiobook)
77 part = ",".join(data.keys())
79 response = youtube_call(
81 "https://www.googleapis.com/upload/youtube/v3/videos",
82 params={'part': part},
84 resumable_file_path=path,
86 data = response.json()
87 audiobook.youtube_id = data['id']
88 audiobook.save(update_fields=['youtube_id'])
90 self.update_thumbnail(audiobook)
93 def update_data(self, audiobook):
94 data = self.get_data(audiobook)
95 data['id'] = audiobook.youtube_id
96 part = ",".join(data.keys())
99 "https://www.googleapis.com/youtube/v3/videos",
100 params={"part": part},
104 def prepare_file(self, input_paths, output_path=None):
105 audio = self.prepare_audio(input_paths)
106 duration = self.get_duration(input_paths)
107 video = self.prepare_video(duration)
108 output = mux([video, audio], output_path=output_path)
113 def get_duration(self, input_paths):
115 for input_path in input_paths:
116 d += get_duration(input_path)
118 d += get_duration(self.intro_flac.path)
120 d += get_duration(self.outro_flac.path)
123 def prepare_audio(self, input_paths):
126 files.append(standardize_audio(self.intro_flac.path))
127 for input_path in input_paths:
128 files.append(standardize_audio(input_path, cache=False))
130 files.append(standardize_audio(self.outro_flac.path))
131 output = concat_audio(files)
136 def prepare_video(self, duration):
142 fps = get_framerate(self.loop_video.path)
143 loop_video = standardize_video(self.loop_video.path)
147 loop_duration = duration
148 for card in self.card_set.filter(duration__gt=0):
149 loop_duration -= card.duration
150 card_video = video_from_image(
151 card.image.path, card.duration, fps=fps
153 (concat if card.order < 0 else outro).append(card_video)
154 delete.append(card_video)
157 loop_video_duration = get_duration(loop_video)
158 times_loop = int(loop_duration // loop_video_duration)
160 leftover_duration = loop_duration % loop_video_duration
161 leftover = cut_video(loop_video, leftover_duration)
162 concat.extend([loop_video] * times_loop + [leftover])
163 delete.append(leftover)
165 leftover = video_from_image(self.loop_card.path, loop_duration)
166 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
167 delete.append(leftover)
170 output = concat_videos(concat)
178 # selfDeclaredMadeForKids
180 def update_thumbnail(self, audiobook):
181 thumbnail = self.prepare_thumbnail(audiobook)
182 if thumbnail is not None:
183 response = youtube_call(
185 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
186 params={'videoId': audiobook.youtube_id},
187 data=thumbnail.getvalue(),
190 def prepare_thumbnail(self, audiobook):
191 for thumbnail_template in ThumbnailTemplate.objects.filter(is_active=True).order_by('order'):
192 if not thumbnail_template.is_for_audiobook(audiobook):
194 thumbnail = thumbnail_template.generate(audiobook)
195 if thumbnail is not None:
199 class Card(models.Model):
200 youtube = models.ForeignKey(YouTube, models.CASCADE)
201 order = models.SmallIntegerField()
202 image = models.FileField(upload_to='youtube/card')
203 duration = models.FloatField()
206 ordering = ('order', )
209 class Font(models.Model):
210 name = models.CharField(max_length=255, unique=True)
211 truetype = models.FileField(upload_to='youtube/font')
217 class ThumbnailTemplate(models.Model):
218 youtube = models.ForeignKey(YouTube, models.CASCADE)
219 order = models.SmallIntegerField()
220 is_active = models.BooleanField()
221 background = models.FileField(upload_to='youtube/thumbnail')
222 definition = models.TextField()
223 authors = models.CharField(max_length=255, blank=True)
224 epochs = models.CharField(max_length=255, blank=True)
225 kinds = models.CharField(max_length=255, blank=True)
226 genres = models.CharField(max_length=255, blank=True)
227 collections = models.CharField(max_length=255, blank=True)
230 ordering = ('order', )
232 def generate(self, audiobook):
234 title = audiobook.book['title']
235 if audiobook.book.get('parent'):
236 parent_title = audiobook.book['parent']['title']
237 if not title.startswith(parent_title):
238 title = ", ".join((parent_title, title))
240 img = create_thumbnail(
241 self.background.path,
244 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
246 "part": (audiobook.youtube_volume or audiobook.part_name).strip() if audiobook.youtube_volume_count > 1 else '',
248 lambda name: Font.objects.get(name=name).truetype.path
250 except Exception as e:
255 img.save(buf, format='PNG')
258 def is_for_audiobook(self, audiobook):
259 for category in 'authors', 'epochs', 'kinds', 'genres':
260 if getattr(self, category):
261 book_slugs = set([g['slug'] for g in audiobook.book[category]])
262 template_slugs = set([g.strip() for g in getattr(self, category).split(',')])
263 if not book_slugs.intersection(template_slugs):
267 template_collections = set([g.strip() for g in self.collections.split(',')])
269 for collection in template_collections:
270 apidata = requests.get(
271 f'https://wolnelektury.pl/api/collections/{collection}/'
273 for book in apidata['books']:
274 if book['slug'] == audiobook.slug: