4 from django.db import models
6 from django.utils.functional import cached_property
7 from django.utils.translation import gettext_lazy as _
8 from django_pglocks import advisory_lock
10 from archive.constants import status
11 from archive.settings import FILES_SAVE_PATH, ADVERT, ORGANIZATION, PROJECT
12 from archive.utils import OverwriteStorage, sha1_file
15 class License(models.Model):
16 uri = models.CharField(max_length=255, unique=True)
17 name = models.CharField(max_length=255)
23 class Project(models.Model):
24 """ an audiobook project, needed for specyfing sponsors """
26 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
27 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
28 description = models.TextField(blank=True, verbose_name="Opis")
31 verbose_name = _("project")
32 verbose_name_plural = _("projects")
38 def get_description(self):
40 return self.description
41 return "Audiobook nagrany w ramach projektu %s%s." % (
43 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
47 class Piece(models.Model):
48 name = models.CharField(max_length=255)
49 source_file = models.FileField(upload_to='piece')
50 is_outro = models.BooleanField(default=False)
51 min_audiobook_duration = models.IntegerField(default=0)
57 def source_upload_to(intance, filename):
58 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
61 class Audiobook(models.Model):
62 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
63 verbose_name=_('source file'), editable=False)
64 source_sha1 = models.CharField(max_length=40, editable=False)
66 title = models.CharField(max_length=255, verbose_name=_('title'))
67 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
68 default='', blank=True)
69 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
70 youtube_volume = models.CharField(
71 _("Volume name for YouTube"),
75 "If set, audiobooks with the save value will be published as single YouTube video."
78 artist = models.CharField(max_length=255, verbose_name=_('artist'))
79 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
80 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
81 date = models.CharField(max_length=255, verbose_name=_('date'))
82 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
83 slug = models.SlugField(max_length=120, help_text=_('WL catalogue slug of the book.'))
84 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
85 modified = models.DateTimeField(null=True, editable=False)
86 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
89 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
90 mp3_task = models.CharField(max_length=64, null=True, editable=False)
91 mp3_tags = models.TextField(null=True, editable=False)
92 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
93 mp3_published_tags = models.TextField(null=True, editable=False)
94 mp3_published = models.DateTimeField(null=True, editable=False)
96 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
97 ogg_task = models.CharField(max_length=64, null=True, editable=False)
98 ogg_tags = models.TextField(null=True, editable=False)
99 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
100 ogg_published_tags = models.TextField(null=True, editable=False)
101 ogg_published = models.DateTimeField(null=True, editable=False)
103 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
104 youtube_task = models.CharField(max_length=64, null=True, editable=False)
105 youtube_tags = models.TextField(null=True, editable=False)
106 youtube_published_tags = models.TextField(null=True, editable=False)
107 youtube_published = models.DateTimeField(null=True, editable=False)
108 youtube_id = models.CharField(max_length=255, blank=True, default='')
109 youtube_queued = models.DateTimeField(null=True, blank=True)
112 verbose_name = _("audiobook")
113 verbose_name_plural = _("audiobooks")
114 ordering = ("title",)
121 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
124 def parts_count(self):
125 return type(self).objects.filter(slug=self.slug).count()
128 def youtube_volume_count(self):
131 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
132 if not a.youtube_volume or a.youtube_volume != prev_volume:
134 prev_volume = a.youtube_volume
138 def youtube_volume_index(self):
141 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
142 if not a.youtube_volume or a.youtube_volume != prev_volume:
144 prev_volume = a.youtube_volume
148 def is_youtube_publishable(self):
150 not self.youtube_volume
152 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
156 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
157 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
158 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
159 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
160 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
161 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
164 return self.mp3_published and self.ogg_published
166 def get_source_sha1(self):
167 assert self.pk or self.source_sha1
168 if not self.source_sha1:
169 with advisory_lock(f'get_source_sha1_{self.pk}'):
170 with open(self.source_file.path, 'rb') as f:
171 self.source_sha1 = sha1_file(f)
172 self.save(update_fields=['source_sha1'])
173 return self.source_sha1
175 def new_publish_tags(self):
178 title += ' (tłum. %s)' % self.translator
180 copyright = "%s %s." % (
181 self.date, ORGANIZATION)
183 copyright += " Licensed to the public under %s verify at %s" % (
184 self.license.uri, self.url)
186 comment = "\n".join((
187 self.project.get_description(),
193 'albumartist': ORGANIZATION,
194 'artist': self.artist,
196 'conductor': self.conductor,
198 'copyright': copyright,
202 'organization': ORGANIZATION,
204 'project': self.project.name,
207 tags['license'] = self.license.uri
208 if self.project.sponsors:
209 tags['funded_by'] = self.project.sponsors
212 tags['flac_sha1'] = self.source_sha1
217 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()