fix
[audio.git] / src / youtube / models.py
1 import io
2 from os import unlink
3 from django.db import models
4 from django.utils.translation import gettext_lazy as _
5 from django.template import Template, Context
6 import requests
7 from apiclient import youtube_call
8 from .utils import (
9     concat_audio,
10     concat_videos,
11     cut_video,
12     get_duration,
13     get_framerate,
14     mux,
15     standardize_audio,
16     standardize_video,
17     video_from_image,
18 )
19 from .thumbnail import create_thumbnail
20
21
22 YOUTUBE_TITLE_LIMIT = 100
23
24
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=[
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 __str__(self):
48         return self.name
49
50     def get_context(self, audiobook):
51         return Context(dict(
52             audiobook=audiobook,
53         ))
54
55     def get_description(self, audiobook):
56         return Template(self.description_template).render(self.get_context(audiobook))
57
58     def get_title(self, audiobook):
59         return Template(self.title_template).render(self.get_context(audiobook))[:YOUTUBE_TITLE_LIMIT]
60
61     def get_data(self, audiobook):
62         return dict(
63             snippet=dict(
64                 title=self.get_title(audiobook),
65                 description=self.get_description(audiobook),
66                 categoryId=self.category,
67                 defaultLanguage='pl',
68                 defaultAudioLanguage='pl',
69             ),
70             status=dict(
71                 privacyStatus=self.privacy_status,
72             ),
73         )
74
75     def publish(self, audiobook, path):
76         data = self.get_data(audiobook)
77         part = ",".join(data.keys())
78
79         response = youtube_call(
80             "POST",
81             "https://www.googleapis.com/upload/youtube/v3/videos",
82             params={'part': part},
83             json=data,
84             resumable_file_path=path,
85         )
86         data = response.json()
87         audiobook.youtube_id = data['id']
88         audiobook.save(update_fields=['youtube_id'])
89
90         self.update_thumbnail(audiobook)
91         return response
92
93     def update_data(self, audiobook):
94         data = self.get_data(audiobook)
95         data['id'] = audiobook.youtube_id
96         part = ",".join(data.keys())
97         youtube_call(
98             "PUT",
99             "https://www.googleapis.com/youtube/v3/videos",
100             params={"part": part},
101             json=data
102         )
103
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)
109         unlink(audio)
110         unlink(video)
111         return output
112
113     def get_duration(self, input_paths):
114         d = 0
115         for input_path in input_paths:
116             d += get_duration(input_path)
117         if self.intro_flac:
118             d += get_duration(self.intro_flac.path)
119         if self.outro_flac:
120             d += get_duration(self.outro_flac.path)
121         return d
122
123     def prepare_audio(self, input_paths):
124         files = []
125         if self.intro_flac:
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))
129         if self.outro_flac:
130             files.append(standardize_audio(self.outro_flac.path))
131         output = concat_audio(files)
132         for d in files:
133             unlink(d)
134         return output
135
136     def prepare_video(self, duration):
137         concat = []
138         outro = []
139         delete = []
140
141         if self.loop_video:
142             fps = get_framerate(self.loop_video.path)
143             loop_video = standardize_video(self.loop_video.path)
144         else:
145             fps = 25
146
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
152             )
153             (concat if card.order < 0 else outro).append(card_video)
154             delete.append(card_video)
155
156         if self.loop_video:
157             loop_video_duration = get_duration(loop_video)
158             times_loop = int(loop_duration // loop_video_duration)
159
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)
164         else:
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)
168         concat.extend(outro)
169
170         output = concat_videos(concat)
171         for p in delete:
172             unlink(p)
173         unlink(loop_video)
174         return output
175
176     # tags
177     # license
178     # selfDeclaredMadeForKids
179
180     def update_thumbnail(self, audiobook):
181         thumbnail = self.prepare_thumbnail(audiobook)
182         if thumbnail is not None:
183             response = youtube_call(
184                 "POST",
185                 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
186                 params={'videoId': audiobook.youtube_id},
187                 data=thumbnail.getvalue(),
188             )
189
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):
193                 continue
194             thumbnail = thumbnail_template.generate(audiobook)
195             if thumbnail is not None:
196                 return thumbnail
197
198
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()
204
205     class Meta:
206         ordering = ('order', )
207
208
209 class Font(models.Model):
210     name = models.CharField(max_length=255, unique=True)
211     truetype = models.FileField(upload_to='youtube/font')
212
213     def __str__(self):
214         return self.name
215
216
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)
228
229     class Meta:
230         ordering = ('order', )
231
232     def generate(self, audiobook):
233         try:
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))
239
240             img = create_thumbnail(
241                 self.background.path,
242                 self.definition,
243                 {
244                     "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
245                     "title": title,
246                     "part": (audiobook.youtube_volume or audiobook.part_name).strip() if audiobook.youtube_volume_count > 1 else '',
247                 },
248                 lambda name: Font.objects.get(name=name).truetype.path
249             )
250         except Exception as e:
251             print(e)
252             return
253         else:
254             buf = io.BytesIO()
255             img.save(buf, format='PNG')
256             return buf
257
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):
264                     return False
265
266         if self.collections:
267             template_collections = set([g.strip() for g in self.collections.split(',')])
268             in_any = False
269             for collection in template_collections:
270                 apidata = requests.get(
271                     f'https://wolnelektury.pl/api/collections/{collection}/'
272                 ).json()
273                 for book in apidata['books']:
274                     if book['slug'] == audiobook.slug:
275                         in_any = True
276                         break
277                 if in_any:
278                     break
279             if not in_any:
280                 return False
281         
282         return True