Badges on audiobook list.
[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 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     title_template = models.CharField(max_length=1024, blank=True)
27     description_template = models.TextField(blank=True)
28     category = models.IntegerField(null=True, blank=True, choices=[
29         (27, 'Edukacja'),
30     ])
31     intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
32     outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
33     loop_card = models.FileField(upload_to='youtube/card', blank=True)
34     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
35     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
36     thumbnail_definition = models.TextField(blank=True)
37     privacy_status = models.CharField(max_length=16, choices=[
38         ('public', _('public')),
39         ('unlisted', _('unlisted')),
40         ('private', _('private')),
41     ])
42     genres = models.CharField(max_length=2048, blank=True)
43
44     class Meta:
45         verbose_name = _("YouTube configuration")
46         verbose_name_plural = _("YouTube configurations")
47
48     def get_context(self, audiobook):
49         return Context(dict(
50             audiobook=audiobook,
51         ))
52
53     def get_description(self, audiobook):
54         return Template(self.description_template).render(self.get_context(audiobook))
55
56     def get_title(self, audiobook):
57         return Template(self.title_template).render(self.get_context(audiobook))[:YOUTUBE_TITLE_LIMIT]
58
59     def get_data(self, audiobook):
60         return dict(
61             snippet=dict(
62                 title=self.get_title(audiobook),
63                 description=self.get_description(audiobook),
64                 categoryId=self.category,
65                 defaultLanguage='pl',
66                 defaultAudioLanguage='pl',
67             ),
68             status=dict(
69                 privacyStatus=self.privacy_status,
70             ),
71         )
72
73     def publish(self, audiobook, path):
74         data = self.get_data(audiobook)
75         part = ",".join(data.keys())
76
77         response = youtube_call(
78             "POST",
79             "https://www.googleapis.com/upload/youtube/v3/videos",
80             params={'part': part},
81             json=data,
82             resumable_file_path=path,
83         )
84         data = response.json()
85         audiobook.youtube_id = data['id']
86         audiobook.save(update_fields=['youtube_id'])
87
88         self.update_thumbnail(audiobook)
89         return response
90
91     def update_data(self, audiobook):
92         data = self.get_data(audiobook)
93         data['id'] = audiobook.youtube_id
94         part = ",".join(data.keys())
95         youtube_call(
96             "PUT",
97             "https://www.googleapis.com/youtube/v3/videos",
98             params={"part": part},
99             json=data
100         )
101
102     def prepare_file(self, input_paths, output_path=None):
103         audio = self.prepare_audio(input_paths)
104         duration = self.get_duration(input_paths)
105         video = self.prepare_video(duration)
106         output = mux([video, audio], output_path=output_path)
107         unlink(audio)
108         unlink(video)
109         return output
110
111     def get_duration(self, input_paths):
112         d = 0
113         for input_path in input_paths:
114             d += get_duration(input_path)
115         if self.intro_flac:
116             d += get_duration(self.intro_flac.path)
117         if self.outro_flac:
118             d += get_duration(self.outro_flac.path)
119         return d
120
121     def prepare_audio(self, input_paths):
122         files = []
123         if self.intro_flac:
124             files.append(standardize_audio(self.intro_flac.path))
125         for input_path in input_paths:
126             files.append(standardize_audio(input_path, cache=False))
127         if self.outro_flac:
128             files.append(standardize_audio(self.outro_flac.path))
129         output = concat_audio(files)
130         for d in files:
131             unlink(d)
132         return output
133
134     def prepare_video(self, duration):
135         concat = []
136         outro = []
137         delete = []
138
139         if self.loop_video:
140             fps = get_framerate(self.loop_video.path)
141             loop_video = standardize_video(self.loop_video.path)
142         else:
143             fps = 25
144
145         loop_duration = duration
146         for card in self.card_set.filter(duration__gt=0):
147             loop_duration -= card.duration
148             card_video = video_from_image(
149                 card.image.path, card.duration, fps=fps
150             )
151             (concat if card.order < 0 else outro).append(card_video)
152             delete.append(card_video)
153
154         if self.loop_video:
155             loop_video_duration = get_duration(loop_video)
156             times_loop = int(loop_duration // loop_video_duration)
157
158             leftover_duration = loop_duration % loop_video_duration
159             leftover = cut_video(loop_video, leftover_duration)
160             concat.extend([loop_video] * times_loop + [leftover])
161             delete.append(leftover)
162         else:
163             leftover = video_from_image(self.loop_card.path, loop_duration)
164             concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
165             delete.append(leftover)
166         concat.extend(outro)
167
168         output = concat_videos(concat)
169         for p in delete:
170             unlink(p)
171         unlink(loop_video)
172         return output
173
174     # tags
175     # license
176     # selfDeclaredMadeForKids
177
178     def update_thumbnail(self, audiobook):
179         thumbnail = self.prepare_thumbnail(audiobook)
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         img = create_thumbnail(
189             self.thumbnail_template.path,
190             self.thumbnail_definition,
191             {
192                 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
193                 "title": audiobook.book['title'],
194                 "part": (audiobook.youtube_volume or audiobook.part_name).strip(),
195             },
196             lambda name: Font.objects.get(name=name).truetype.path
197         )
198         buf = io.BytesIO()
199         img.save(buf, format='PNG')
200         return buf
201
202
203 class Card(models.Model):
204     youtube = models.ForeignKey(YouTube, models.CASCADE)
205     order = models.SmallIntegerField()
206     image = models.FileField(upload_to='youtube/card')
207     duration = models.FloatField()
208
209     class Meta:
210         ordering = ('order', )
211
212
213 class Font(models.Model):
214     name = models.CharField(max_length=255, unique=True)
215     truetype = models.FileField(upload_to='youtube/font')
216
217     def __str__(self):
218         return self.name