5 from django.db import models
7 from django.utils.functional import cached_property
8 from django.utils.timezone import now
9 from django.utils.translation import gettext_lazy as _
10 from django_pglocks import advisory_lock
12 from archive.constants import status
13 from archive.settings import FILES_SAVE_PATH, ADVERT, ORGANIZATION, PROJECT
14 from archive.utils import OverwriteStorage, sha1_file
17 class License(models.Model):
18 uri = models.CharField(max_length=255, unique=True)
19 name = models.CharField(max_length=255)
25 class Project(models.Model):
26 """ an audiobook project, needed for specyfing sponsors """
28 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
29 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
30 description = models.TextField(blank=True, verbose_name="Opis")
31 youtube = models.ForeignKey('youtube.YouTube', models.PROTECT)
32 icon = models.FileField(upload_to='archive/project', blank=True, null=True)
35 verbose_name = _("project")
36 verbose_name_plural = _("projects")
42 def get_description(self):
44 return self.description
45 return "Audiobook nagrany w ramach projektu %s%s." % (
47 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
51 def source_upload_to(intance, filename):
52 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
55 class Audiobook(models.Model):
56 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
57 verbose_name=_('source file'), editable=False)
58 source_sha1 = models.CharField(max_length=40, editable=False)
59 duration = models.FloatField(null=True, editable=False)
61 title = models.CharField(max_length=255, verbose_name=_('title'))
62 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
63 default='', blank=True)
64 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
65 youtube_volume = models.CharField(
66 _("Volume name for YouTube"),
70 "If set, audiobooks with the save value will be published as single YouTube video."
73 artist = models.CharField(max_length=255, verbose_name=_('artist'))
74 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
75 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
76 date = models.CharField(max_length=255, verbose_name=_('date'))
77 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
78 slug = models.SlugField(max_length=120, blank=True, help_text=_('WL catalogue slug of the book.'))
79 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
80 modified = models.DateTimeField(null=True, editable=False)
81 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
84 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
85 mp3_task = models.CharField(max_length=64, null=True, editable=False)
86 mp3_tags = models.TextField(null=True, editable=False)
87 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
88 mp3_published_tags = models.TextField(null=True, editable=False)
89 mp3_published = models.DateTimeField(null=True, editable=False)
91 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
92 ogg_task = models.CharField(max_length=64, null=True, editable=False)
93 ogg_tags = models.TextField(null=True, editable=False)
94 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
95 ogg_published_tags = models.TextField(null=True, editable=False)
96 ogg_published = models.DateTimeField(null=True, editable=False)
98 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
99 youtube_task = models.CharField(max_length=64, null=True, editable=False)
100 youtube_tags = models.TextField(null=True, editable=False)
101 youtube_published_tags = models.TextField(null=True, editable=False)
102 youtube_published = models.DateTimeField(null=True, editable=False)
103 youtube_id = models.CharField(max_length=255, blank=True, default='')
104 youtube_queued = models.DateTimeField(null=True, blank=True)
107 verbose_name = _("audiobook")
108 verbose_name_plural = _("audiobooks")
109 ordering = ("title",)
116 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
119 def parts_count(self):
120 return type(self).objects.filter(slug=self.slug).count()
123 def youtube_volume_count(self):
126 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
127 if not a.youtube_volume or a.youtube_volume != prev_volume:
129 prev_volume = a.youtube_volume
133 def youtube_volume_index(self):
136 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
137 if not a.youtube_volume or a.youtube_volume != prev_volume:
139 prev_volume = a.youtube_volume
143 def is_youtube_publishable(self):
145 not self.youtube_volume
147 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
151 def youtube_publish(self):
152 if not self.is_youtube_publishable:
154 self.youtube_status = status.QUEUED
155 self.youtube_queued = now()
156 self.save(update_fields=['youtube_status', 'youtube_queued'])
158 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
159 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
160 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
161 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
162 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
163 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
166 return self.mp3_published and self.ogg_published
168 def get_source_sha1(self):
169 assert self.pk or self.source_sha1
170 if not self.source_sha1:
171 with advisory_lock(f'get_source_sha1_{self.pk}'):
172 with open(self.source_file.path, 'rb') as f:
173 self.source_sha1 = sha1_file(f)
174 self.save(update_fields=['source_sha1'])
175 return self.source_sha1
177 def new_publish_tags(self):
180 title += ' (tłum. %s)' % self.translator
182 copyright = "%s %s." % (
183 self.date, ORGANIZATION)
185 copyright += " Licensed to the public under %s verify at %s" % (
186 self.license.uri, self.url)
188 comment = "\n".join((
189 self.project.get_description(),
195 'albumartist': ORGANIZATION,
196 'artist': self.artist,
198 'conductor': self.conductor,
200 'copyright': copyright,
204 'organization': ORGANIZATION,
206 'project': self.project.name,
209 tags['license'] = self.license.uri
210 if self.project.sponsors:
211 tags['funded_by'] = self.project.sponsors
214 tags['flac_sha1'] = self.source_sha1
220 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
227 from librarian.document import WLDocument, parser
228 from lxml import etree
230 xml_url = self.book.get('xml', None)
237 requests.get(xml_url).content