1a3fd01a15c8e4a3196623a597f109d96ce903f0
[audio.git] / src / youtube / models.py
1 import io
2 from os import unlink
3 from tempfile import NamedTemporaryFile
4 from django.conf import settings
5 from django.db import models
6 from django.utils.translation import gettext_lazy as _
7 from django.template import Template, Context
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_video,
17     video_from_image,
18 )
19 from .thumbnail import create_thumbnail
20
21
22 class YouTube(models.Model):
23     title_template = models.CharField(max_length=1024, blank=True)
24     description_template = models.TextField(blank=True)
25     category = models.IntegerField(null=True, blank=True, choices=[
26         (27, 'Edukacja'),
27     ])
28     intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
29     outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
30     loop_card = models.FileField(upload_to='youtube/card', blank=True)
31     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
32     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
33     thumbnail_definition = models.TextField(blank=True)
34     privacy_status = models.CharField(max_length=16, choices=[
35         ('public', _('public')),
36         ('unlisted', _('unlisted')),
37         ('private', _('private')),
38     ])
39     genres = models.CharField(max_length=2048, blank=True)
40
41     class Meta:
42         verbose_name = _("YouTube configuration")
43         verbose_name_plural = _("YouTube configurations")
44
45     def get_context(self, audiobook):
46         return Context(dict(
47             audiobook=audiobook,
48             LICENSE=settings.LICENSE,
49             LICENSE_NAME=settings.LICENSE_NAME,
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))
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         with open(path, "rb") as f:
77             response = youtube_call(
78                 "POST",
79                 "https://www.googleapis.com/upload/youtube/v3/videos",
80                 params={'part': part},
81                 json=data,
82                 resumable_data=f.read(),
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_path, output_path=None):
103         audio = self.prepare_audio(input_path)
104         duration = self.get_duration(input_path)
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_path):
112         d = get_duration(input_path)
113         if self.intro_flac:
114             d += get_duration(self.intro_flac.path)
115         if self.outro_flac:
116             d += get_duration(self.outro_flac.path)
117         return d
118     
119     def prepare_audio(self, input_path):
120         files = []
121         if self.intro_flac:
122             files.append(self.intro_flac.path)
123         files.append(input_path)
124         if self.outro_flac:
125             files.append(self.outro_flac.path)
126         return concat_audio(files)
127     
128     def prepare_video(self, duration):
129         concat = []
130         outro = []
131         delete = []
132
133         if self.loop_video:
134             fps = get_framerate(self.loop_video.path)
135             loop_video = standardize_video(self.loop_video.path)
136         else:
137             fps = 25
138
139         loop_duration = duration
140         for card in self.card_set.filter(duration__gt=0):
141             loop_duration -= card.duration
142             card_video = video_from_image(
143                 card.image.path, card.duration, fps=fps
144             )
145             (concat if card.order < 0 else outro).append(card_video)
146             delete.append(card_video)
147
148         if self.loop_video:
149             loop_video_duration = get_duration(loop_video)
150             times_loop = int(loop_duration // loop_video_duration)
151
152             leftover_duration = loop_duration % loop_video_duration
153             leftover = cut_video(loop_video, leftover_duration)
154             concat.extend([loop_video] * times_loop + [leftover])
155             delete.append(leftover)
156         else:
157             leftover = video_from_image(self.loop_card.path, loop_duration)
158             concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
159             delete.append(leftover)
160         concat.extend(outro)
161
162         output = concat_videos(concat)
163         for p in delete:
164             unlink(p)
165         unlink(loop_video)
166         return output
167
168     # tags
169     # license
170     # selfDeclaredMadeForKids
171
172     def update_thumbnail(self, audiobook):
173         thumbnail = self.prepare_thumbnail(audiobook)
174         response = youtube_call(
175             "POST",
176             "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
177             params={'videoId': audiobook.youtube_id},
178             data=thumbnail.getvalue(),
179         )
180
181     def prepare_thumbnail(self, audiobook):
182         img = create_thumbnail(
183             self.thumbnail_template.path,
184             self.thumbnail_definition,
185             {
186                 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
187                 "title": audiobook.book['title'],
188             },
189             lambda name: Font.objects.get(name=name).truetype.path
190         )
191         buf = io.BytesIO()
192         img.save(buf, format='PNG')
193         return buf
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