Thumbnail fixes.
[audio.git] / src / youtube / models.py
1 import io
2 from os import unlink
3 from tempfile import NamedTemporaryFile
4 from django.db import models
5 from django.utils.translation import gettext_lazy as _
6 from django.template import Template, Context
7 import requests
8 from apiclient import youtube_call
9 from .utils import (
10     concat_audio,
11     concat_videos,
12     cut_video,
13     get_duration,
14     get_framerate,
15     mux,
16     standardize_audio,
17     standardize_video,
18     video_from_image,
19 )
20 from .thumbnail import create_thumbnail
21
22
23 YOUTUBE_TITLE_LIMIT = 100
24
25
26 class YouTube(models.Model):
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=[
30         (27, 'Edukacja'),
31     ])
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')),
40     ])
41     genres = models.CharField(max_length=2048, blank=True)
42
43     class Meta:
44         verbose_name = _("YouTube configuration")
45         verbose_name_plural = _("YouTube configurations")
46
47     def get_context(self, audiobook):
48         return Context(dict(
49             audiobook=audiobook,
50         ))
51
52     def get_description(self, audiobook):
53         return Template(self.description_template).render(self.get_context(audiobook))
54
55     def get_title(self, audiobook):
56         return Template(self.title_template).render(self.get_context(audiobook))[:YOUTUBE_TITLE_LIMIT]
57
58     def get_data(self, audiobook):
59         return dict(
60             snippet=dict(
61                 title=self.get_title(audiobook),
62                 description=self.get_description(audiobook),
63                 categoryId=self.category,
64                 defaultLanguage='pl',
65                 defaultAudioLanguage='pl',
66             ),
67             status=dict(
68                 privacyStatus=self.privacy_status,
69             ),
70         )
71
72     def publish(self, audiobook, path):
73         data = self.get_data(audiobook)
74         part = ",".join(data.keys())
75
76         response = youtube_call(
77             "POST",
78             "https://www.googleapis.com/upload/youtube/v3/videos",
79             params={'part': part},
80             json=data,
81             resumable_file_path=path,
82         )
83         data = response.json()
84         audiobook.youtube_id = data['id']
85         audiobook.save(update_fields=['youtube_id'])
86
87         self.update_thumbnail(audiobook)
88         return response
89
90     def update_data(self, audiobook):
91         data = self.get_data(audiobook)
92         data['id'] = audiobook.youtube_id
93         part = ",".join(data.keys())
94         youtube_call(
95             "PUT",
96             "https://www.googleapis.com/youtube/v3/videos",
97             params={"part": part},
98             json=data
99         )
100
101     def prepare_file(self, input_paths, output_path=None):
102         audio = self.prepare_audio(input_paths)
103         duration = self.get_duration(input_paths)
104         video = self.prepare_video(duration)
105         output = mux([video, audio], output_path=output_path)
106         unlink(audio)
107         unlink(video)
108         return output
109
110     def get_duration(self, input_paths):
111         d = 0
112         for input_path in input_paths:
113             d += get_duration(input_path)
114         if self.intro_flac:
115             d += get_duration(self.intro_flac.path)
116         if self.outro_flac:
117             d += get_duration(self.outro_flac.path)
118         return d
119
120     def prepare_audio(self, input_paths):
121         files = []
122         if self.intro_flac:
123             files.append(standardize_audio(self.intro_flac.path))
124         for input_path in input_paths:
125             files.append(standardize_audio(input_path, cache=False))
126         if self.outro_flac:
127             files.append(standardize_audio(self.outro_flac.path))
128         output = concat_audio(files)
129         for d in files:
130             unlink(d)
131         return output
132
133     def prepare_video(self, duration):
134         concat = []
135         outro = []
136         delete = []
137
138         if self.loop_video:
139             fps = get_framerate(self.loop_video.path)
140             loop_video = standardize_video(self.loop_video.path)
141         else:
142             fps = 25
143
144         loop_duration = duration
145         for card in self.card_set.filter(duration__gt=0):
146             loop_duration -= card.duration
147             card_video = video_from_image(
148                 card.image.path, card.duration, fps=fps
149             )
150             (concat if card.order < 0 else outro).append(card_video)
151             delete.append(card_video)
152
153         if self.loop_video:
154             loop_video_duration = get_duration(loop_video)
155             times_loop = int(loop_duration // loop_video_duration)
156
157             leftover_duration = loop_duration % loop_video_duration
158             leftover = cut_video(loop_video, leftover_duration)
159             concat.extend([loop_video] * times_loop + [leftover])
160             delete.append(leftover)
161         else:
162             leftover = video_from_image(self.loop_card.path, loop_duration)
163             concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
164             delete.append(leftover)
165         concat.extend(outro)
166
167         output = concat_videos(concat)
168         for p in delete:
169             unlink(p)
170         unlink(loop_video)
171         return output
172
173     # tags
174     # license
175     # selfDeclaredMadeForKids
176
177     def update_thumbnail(self, audiobook):
178         thumbnail = self.prepare_thumbnail(audiobook)
179         if thumbnail is not None:
180             response = youtube_call(
181                 "POST",
182                 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
183                 params={'videoId': audiobook.youtube_id},
184                 data=thumbnail.getvalue(),
185             )
186
187     def prepare_thumbnail(self, audiobook):
188         for thumbnail_template in ThumbnailTemplate.objects.filter(is_active=True).order_by('order'):
189             if not thumbnail_template.is_for_audiobook(audiobook):
190                 continue
191             thumbnail = thumbnail_template.generate(audiobook)
192             if thumbnail is not None:
193                 return thumbnail
194
195
196 class Card(models.Model):
197     youtube = models.ForeignKey(YouTube, models.CASCADE)
198     order = models.SmallIntegerField()
199     image = models.FileField(upload_to='youtube/card')
200     duration = models.FloatField()
201
202     class Meta:
203         ordering = ('order', )
204
205
206 class Font(models.Model):
207     name = models.CharField(max_length=255, unique=True)
208     truetype = models.FileField(upload_to='youtube/font')
209
210     def __str__(self):
211         return self.name
212
213
214 class ThumbnailTemplate(models.Model):
215     order = models.SmallIntegerField()
216     is_active = models.BooleanField()
217     background = models.FileField(upload_to='youtube/thumbnail')
218     definition = models.TextField()
219     authors = models.CharField(max_length=255, blank=True)
220     epochs = models.CharField(max_length=255, blank=True)
221     kinds = models.CharField(max_length=255, blank=True)
222     genres = models.CharField(max_length=255, blank=True)
223     collections = models.CharField(max_length=255, blank=True)
224
225     class Meta:
226         ordering = ('order', )
227
228     def generate(self, audiobook):
229         try:
230             img = create_thumbnail(
231                 self.background.path,
232                 self.definition,
233                 {
234                     "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
235                     "title": audiobook.book['title'],
236                     "part": (audiobook.youtube_volume or audiobook.part_name).strip(),
237                 },
238                 lambda name: Font.objects.get(name=name).truetype.path
239             )
240         except Exception as e:
241             print(e)
242             return
243         else:
244             buf = io.BytesIO()
245             img.save(buf, format='PNG')
246             return buf
247
248     def is_for_audiobook(self, audiobook):
249         for category in 'authors', 'epochs', 'kinds', 'genres':
250             if getattr(self, category):
251                 book_slugs = set([g['slug'] for g in audiobook.book[category]])
252                 template_slugs = set([g.strip() for g in getattr(self, category).split(',')])
253                 if not book_slugs.intersection(template_slugs):
254                     return False
255
256         if self.collections:
257             template_collections = set([g.strip() for g in self.collections.split(',')])
258             in_any = False
259             for collection in template_collections:
260                 apidata = requests.get(
261                     f'https://wolnelektury.pl/api/collections/{collection}/'
262                 ).json()
263                 for book in apidata['books']:
264                     if book['slug'] == audiobook.slug:
265                         in_any = True
266                         break
267                 if in_any:
268                     break
269             if not in_any:
270                 return False
271         
272         return True