4 from urllib.parse import urljoin
6 from django.db import models
8 from django.contrib.sites.models import Site
9 from django.utils.functional import cached_property
10 from django.utils.timezone import now
11 from django.utils.translation import gettext_lazy as _
12 from django_pglocks import advisory_lock
14 from archive.constants import status
15 from archive.settings import FILES_SAVE_PATH, ADVERT, ORGANIZATION, PROJECT
16 from archive.utils import OverwriteStorage, sha1_file
19 class License(models.Model):
20 uri = models.CharField(max_length=255, unique=True)
21 name = models.CharField(max_length=255)
27 class Project(models.Model):
28 """ an audiobook project, needed for specyfing sponsors """
30 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
31 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
32 description = models.TextField(blank=True, verbose_name="Opis")
33 youtube = models.ForeignKey('youtube.YouTube', models.PROTECT)
34 icon = models.FileField(upload_to='archive/project', blank=True, null=True)
37 verbose_name = _("project")
38 verbose_name_plural = _("projects")
44 def get_description(self):
46 return self.description
47 return "Audiobook nagrany w ramach projektu %s%s." % (
49 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
52 def get_icon_url(self):
56 'https://' + Site.objects.get_current().domain,
61 def source_upload_to(intance, filename):
62 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
65 class Audiobook(models.Model):
66 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
67 verbose_name=_('source file'), editable=False)
68 source_sha1 = models.CharField(max_length=40, editable=False)
69 duration = models.FloatField(null=True, editable=False)
71 title = models.CharField(max_length=255, verbose_name=_('title'))
72 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
73 default='', blank=True)
74 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
75 youtube_volume = models.CharField(
76 _("Volume name for YouTube"),
80 "If set, audiobooks with the save value will be published as single YouTube video."
83 artist = models.CharField(max_length=255, verbose_name=_('artist'))
84 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
85 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
86 date = models.CharField(max_length=255, verbose_name=_('date'))
87 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
88 slug = models.SlugField(max_length=120, blank=True, help_text=_('WL catalogue slug of the book.'))
89 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
90 modified = models.DateTimeField(null=True, editable=False)
91 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
94 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
95 mp3_task = models.CharField(max_length=64, null=True, editable=False)
96 mp3_tags = models.TextField(null=True, editable=False)
97 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
98 mp3_published_tags = models.TextField(null=True, editable=False)
99 mp3_published = models.DateTimeField(null=True, editable=False)
101 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
102 ogg_task = models.CharField(max_length=64, null=True, editable=False)
103 ogg_tags = models.TextField(null=True, editable=False)
104 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
105 ogg_published_tags = models.TextField(null=True, editable=False)
106 ogg_published = models.DateTimeField(null=True, editable=False)
108 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
109 youtube_task = models.CharField(max_length=64, null=True, editable=False)
110 youtube_tags = models.TextField(null=True, editable=False)
111 youtube_published_tags = models.TextField(null=True, editable=False)
112 youtube_published = models.DateTimeField(null=True, editable=False)
113 youtube_id = models.CharField(max_length=255, blank=True, default='')
114 youtube_queued = models.DateTimeField(null=True, blank=True)
117 verbose_name = _("audiobook")
118 verbose_name_plural = _("audiobooks")
119 ordering = ("title",)
126 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
129 def parts_count(self):
130 return type(self).objects.filter(slug=self.slug).count()
133 def youtube_volume_count(self):
136 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
137 if not a.youtube_volume or a.youtube_volume != prev_volume:
139 prev_volume = a.youtube_volume
143 def youtube_volume_index(self):
146 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
147 if not a.youtube_volume or a.youtube_volume != prev_volume:
149 prev_volume = a.youtube_volume
153 def is_youtube_publishable(self):
155 not self.youtube_volume
157 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
161 def youtube_publish(self):
162 if not self.is_youtube_publishable:
164 self.youtube_status = status.QUEUED
165 self.youtube_queued = now()
166 self.save(update_fields=['youtube_status', 'youtube_queued'])
168 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
169 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
170 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
171 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
172 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
173 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
176 return self.mp3_published and self.ogg_published
178 def get_source_sha1(self):
179 assert self.pk or self.source_sha1
180 if not self.source_sha1:
181 with advisory_lock(f'get_source_sha1_{self.pk}'):
182 with open(self.source_file.path, 'rb') as f:
183 self.source_sha1 = sha1_file(f)
184 self.save(update_fields=['source_sha1'])
185 return self.source_sha1
187 def new_publish_tags(self):
190 title += ' (tłum. %s)' % self.translator
192 copyright = "%s %s." % (
193 self.date, ORGANIZATION)
195 copyright += " Licensed to the public under %s verify at %s" % (
196 self.license.uri, self.url)
198 comment = "\n".join((
199 self.project.get_description(),
205 'albumartist': ORGANIZATION,
206 'artist': self.artist,
208 'conductor': self.conductor,
210 'copyright': copyright,
214 'organization': ORGANIZATION,
216 'project': self.project.name,
219 tags['license'] = self.license.uri
220 if self.project.sponsors:
221 tags['funded_by'] = self.project.sponsors
224 tags['flac_sha1'] = self.source_sha1
230 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
237 from librarian.document import WLDocument, parser
238 from lxml import etree
240 xml_url = self.book.get('xml', None)
247 requests.get(xml_url).content