f0ecc6cd47e1f778f361a795cca0277f39aa27a9
[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 archive.settings import LICENSE, LICENSE_NAME
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 class YouTube(models.Model):
24     title_template = models.CharField(max_length=1024, blank=True)
25     description_template = models.TextField(blank=True)
26     category = models.IntegerField(null=True, blank=True, choices=[
27         (27, 'Edukacja'),
28     ])
29     intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
30     outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
31     loop_card = models.FileField(upload_to='youtube/card', blank=True)
32     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
33     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
34     thumbnail_definition = models.TextField(blank=True)
35     privacy_status = models.CharField(max_length=16, choices=[
36         ('public', _('public')),
37         ('unlisted', _('unlisted')),
38         ('private', _('private')),
39     ])
40     genres = models.CharField(max_length=2048, blank=True)
41
42     class Meta:
43         verbose_name = _("YouTube configuration")
44         verbose_name_plural = _("YouTube configurations")
45
46     def get_context(self, audiobook):
47         return Context(dict(
48             audiobook=audiobook,
49             LICENSE=LICENSE,
50             LICENSE_NAME=LICENSE_NAME,
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))
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         with open(path, "rb") as f:
78             response = youtube_call(
79                 "POST",
80                 "https://www.googleapis.com/upload/youtube/v3/videos",
81                 params={'part': part},
82                 json=data,
83                 resumable_data=f.read(),
84             )
85         data = response.json()
86         audiobook.youtube_id = data['id']
87         audiobook.save(update_fields=['youtube_id'])
88
89         self.update_thumbnail(audiobook)
90         return response
91
92     def update_data(self, audiobook):
93         data = self.get_data(audiobook)
94         data['id'] = audiobook.youtube_id
95         part = ",".join(data.keys())
96         youtube_call(
97             "PUT",
98             "https://www.googleapis.com/youtube/v3/videos",
99             params={"part": part},
100             json=data
101         )       
102
103     def prepare_file(self, input_path, output_path=None):
104         audio = self.prepare_audio(input_path)
105         duration = self.get_duration(input_path)
106         video = self.prepare_video(duration)
107         output = mux([video, audio], output_path=output_path)
108         unlink(audio)
109         unlink(video)
110         return output
111
112     def get_duration(self, input_path):
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_path):
121         files = []
122         delete = []
123         if self.intro_flac:
124             files.append(standardize_audio(self.intro_flac.path))
125             delete.append(files[-1])
126         files.append(input_path)
127         if self.outro_flac:
128             files.append(standardize_audio(self.outro_flac.path))
129             delete.append(files[-1])
130         output = concat_audio(files)
131         for d in delete:
132             unlink(d)
133         return output
134
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         response = youtube_call(
183             "POST",
184             "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
185             params={'videoId': audiobook.youtube_id},
186             data=thumbnail.getvalue(),
187         )
188
189     def prepare_thumbnail(self, audiobook):
190         img = create_thumbnail(
191             self.thumbnail_template.path,
192             self.thumbnail_definition,
193             {
194                 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
195                 "title": audiobook.book['title'],
196             },
197             lambda name: Font.objects.get(name=name).truetype.path
198         )
199         buf = io.BytesIO()
200         img.save(buf, format='PNG')
201         return buf
202         
203
204 class Card(models.Model):
205     youtube = models.ForeignKey(YouTube, models.CASCADE)
206     order = models.SmallIntegerField()
207     image = models.FileField(upload_to='youtube/card')
208     duration = models.FloatField()
209
210     class Meta:
211         ordering = ('order', )
212
213
214 class Font(models.Model):
215     name = models.CharField(max_length=255, unique=True)
216     truetype = models.FileField(upload_to='youtube/font')
217
218     def __str__(self):
219         return self.name