30b300642f666185f15202ec2129fef78e36f793
[audio.git] / src / youtube / models.py
1 from os import unlink
2 from tempfile import NamedTemporaryFile
3 from django.db import models
4 from django.utils.translation import gettext_lazy as _
5 from django.template import Template, Context
6 from apiclient import youtube_call
7 from .utils import (
8     video_from_image,
9     cut_video,
10     concat_videos,
11     get_duration,
12     get_framerate,
13     mux,
14 )
15
16
17 class YouTube(models.Model):
18     title_template = models.CharField(max_length=1024, blank=True)
19     description_template = models.TextField(blank=True)
20     category = models.IntegerField(null=True, blank=True)  # get categories
21     loop_card = models.FileField(upload_to='youtube/card', blank=True)
22     loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
23     thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
24     thumbnail_definition = models.TextField(blank=True)
25     genres = models.CharField(max_length=2048, blank=True)
26
27     class Meta:
28         verbose_name = _("YouTube configuration")
29         verbose_name_plural = _("YouTube configurations")
30
31     def publish(self, audiobook, path):
32         ctx = Context(dict(audiobook=audiobook))
33         description = Template(self.description_template).render(ctx)
34         title = Template(self.title_template).render(ctx)
35         privacy = 'private'
36
37         data = dict(
38             snippet=dict(
39                 title=title,
40                 description=description,
41                 # tags=tags,
42                 # categoryId=category,
43                 # defaultLanguage
44             ),
45             status=dict(
46                 privacyStatus=privacy,
47                 # license
48                 # selfDeclaredMadeForKids
49             ),
50             # recordingDetails=dict(
51             # recordingDate
52             # ),
53         )
54         part = ",".join(data.keys())
55
56         with open(path, "rb") as f:
57             response = youtube_call(
58                 "POST",
59                 "https://www.googleapis.com/upload/youtube/v3/videos",
60                 params={'part': part},
61                 data=data,
62                 media_data=f.read(),
63             )
64         data = response.json()
65         audiobook.youtube_id = data['id']
66         audiobook.save(update_fields=['youtube_id'])
67         return response
68
69     def prepare_file(self, input_path, output_path=None):
70         duration = get_duration(input_path)
71         video = self.prepare_video(duration)
72         output = mux([video, input_path], output_path=output_path)
73         unlink(video)
74         return output
75
76     def prepare_video(self, duration):
77         concat = []
78         outro = []
79         delete = []
80
81         if self.loop_video:
82             fps = get_framerate(self.loop_video.path)
83         else:
84             fps = 25
85
86         loop_duration = duration
87         for card in self.card_set.filter(order__lt=0, duration__gt=0):
88             loop_duration -= card.duration
89             card_video = video_from_image(
90                 card.image.path, card.duration, fps=fps
91             )
92             (concat if card.order < 0 else outro).append(card_video)
93             delete.append(intro)
94
95         if self.loop_video:
96             loop_video_duration = get_duration(self.loop_video.path)
97             times_loop = int(loop_duration // loop_video_duration)
98
99             leftover_duration = loop_duration % loop_video_duration
100             leftover = cut_video(self.loop_video.path, leftover_duration)
101             concat.extend([self.loop_video.path] * times_loop + [leftover])
102             delete.append(leftover)
103         else:
104             leftover = video_from_image(self.loop_card.path, loop_duration)
105             concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
106             delete.append(leftover)
107         concat.extend(outro)
108
109         output = concat_videos(concat)
110         for p in delete:
111             unlink(p)
112         return output
113
114     # tags
115     # license
116     # selfDeclaredMadeForKids
117
118
119 class Card(models.Model):
120     youtube = models.ForeignKey(YouTube, models.CASCADE)
121     order = models.SmallIntegerField()
122     image = models.FileField(upload_to='youtube/card')
123     duration = models.FloatField()
124
125     class Meta:
126         ordering = ('order', )