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