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