4 from django.db import models
6 from django.utils.functional import cached_property
7 from django.utils.timezone import now
8 from django.utils.translation import gettext_lazy as _
9 from django_pglocks import advisory_lock
11 from archive.constants import status
12 from archive.settings import FILES_SAVE_PATH, ADVERT, ORGANIZATION, PROJECT
13 from archive.utils import OverwriteStorage, sha1_file
16 class License(models.Model):
17 uri = models.CharField(max_length=255, unique=True)
18 name = models.CharField(max_length=255)
24 class Project(models.Model):
25 """ an audiobook project, needed for specyfing sponsors """
27 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
28 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
29 description = models.TextField(blank=True, verbose_name="Opis")
32 verbose_name = _("project")
33 verbose_name_plural = _("projects")
39 def get_description(self):
41 return self.description
42 return "Audiobook nagrany w ramach projektu %s%s." % (
44 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
48 class Piece(models.Model):
49 name = models.CharField(max_length=255)
50 source_file = models.FileField(upload_to='piece')
51 is_outro = models.BooleanField(default=False)
52 min_audiobook_duration = models.IntegerField(default=0)
58 def source_upload_to(intance, filename):
59 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
62 class Audiobook(models.Model):
63 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
64 verbose_name=_('source file'), editable=False)
65 source_sha1 = models.CharField(max_length=40, editable=False)
67 title = models.CharField(max_length=255, verbose_name=_('title'))
68 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
69 default='', blank=True)
70 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
71 youtube_volume = models.CharField(
72 _("Volume name for YouTube"),
76 "If set, audiobooks with the save value will be published as single YouTube video."
79 artist = models.CharField(max_length=255, verbose_name=_('artist'))
80 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
81 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
82 date = models.CharField(max_length=255, verbose_name=_('date'))
83 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
84 slug = models.SlugField(max_length=120, help_text=_('WL catalogue slug of the book.'))
85 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
86 modified = models.DateTimeField(null=True, editable=False)
87 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
90 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
91 mp3_task = models.CharField(max_length=64, null=True, editable=False)
92 mp3_tags = models.TextField(null=True, editable=False)
93 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
94 mp3_published_tags = models.TextField(null=True, editable=False)
95 mp3_published = models.DateTimeField(null=True, editable=False)
97 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
98 ogg_task = models.CharField(max_length=64, null=True, editable=False)
99 ogg_tags = models.TextField(null=True, editable=False)
100 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
101 ogg_published_tags = models.TextField(null=True, editable=False)
102 ogg_published = models.DateTimeField(null=True, editable=False)
104 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
105 youtube_task = models.CharField(max_length=64, null=True, editable=False)
106 youtube_tags = models.TextField(null=True, editable=False)
107 youtube_published_tags = models.TextField(null=True, editable=False)
108 youtube_published = models.DateTimeField(null=True, editable=False)
109 youtube_id = models.CharField(max_length=255, blank=True, default='')
110 youtube_queued = models.DateTimeField(null=True, blank=True)
113 verbose_name = _("audiobook")
114 verbose_name_plural = _("audiobooks")
115 ordering = ("title",)
122 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
125 def parts_count(self):
126 return type(self).objects.filter(slug=self.slug).count()
129 def youtube_volume_count(self):
132 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
133 if not a.youtube_volume or a.youtube_volume != prev_volume:
135 prev_volume = a.youtube_volume
139 def youtube_volume_index(self):
142 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
143 if not a.youtube_volume or a.youtube_volume != prev_volume:
145 prev_volume = a.youtube_volume
149 def is_youtube_publishable(self):
151 not self.youtube_volume
153 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
157 def youtube_publish(self):
158 if not self.is_youtube_publishable:
160 self.youtube_status = status.QUEUED
161 self.youtube_queued = now()
162 self.save(update_fields=['youtube_status', 'youtube_queued'])
164 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
165 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
166 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
167 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
168 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
169 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
172 return self.mp3_published and self.ogg_published
174 def get_source_sha1(self):
175 assert self.pk or self.source_sha1
176 if not self.source_sha1:
177 with advisory_lock(f'get_source_sha1_{self.pk}'):
178 with open(self.source_file.path, 'rb') as f:
179 self.source_sha1 = sha1_file(f)
180 self.save(update_fields=['source_sha1'])
181 return self.source_sha1
183 def new_publish_tags(self):
186 title += ' (tłum. %s)' % self.translator
188 copyright = "%s %s." % (
189 self.date, ORGANIZATION)
191 copyright += " Licensed to the public under %s verify at %s" % (
192 self.license.uri, self.url)
194 comment = "\n".join((
195 self.project.get_description(),
201 'albumartist': ORGANIZATION,
202 'artist': self.artist,
204 'conductor': self.conductor,
206 'copyright': copyright,
210 'organization': ORGANIZATION,
212 'project': self.project.name,
215 tags['license'] = self.license.uri
216 if self.project.sponsors:
217 tags['funded_by'] = self.project.sponsors
220 tags['flac_sha1'] = self.source_sha1
225 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()