Remove BUILD_PATH, that's what TMPDIR is for.
[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         if self.intro_flac:
123             files.append(standardize_audio(self.intro_flac.path))
124         files.append(standardize_audio(input_path, cache=False))
125         if self.outro_flac:
126             files.append(standardize_audio(self.outro_flac.path))
127         output = concat_audio(files)
128         for d in files:
129             unlink(d)
130         return output
131
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         response = youtube_call(
180             "POST",
181             "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
182             params={'videoId': audiobook.youtube_id},
183             data=thumbnail.getvalue(),
184         )
185
186     def prepare_thumbnail(self, audiobook):
187         img = create_thumbnail(
188             self.thumbnail_template.path,
189             self.thumbnail_definition,
190             {
191                 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
192                 "title": audiobook.book['title'],
193             },
194             lambda name: Font.objects.get(name=name).truetype.path
195         )
196         buf = io.BytesIO()
197         img.save(buf, format='PNG')
198         return buf
199         
200
201 class Card(models.Model):
202     youtube = models.ForeignKey(YouTube, models.CASCADE)
203     order = models.SmallIntegerField()
204     image = models.FileField(upload_to='youtube/card')
205     duration = models.FloatField()
206
207     class Meta:
208         ordering = ('order', )
209
210
211 class Font(models.Model):
212     name = models.CharField(max_length=255, unique=True)
213     truetype = models.FileField(upload_to='youtube/font')
214
215     def __str__(self):
216         return self.name