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)
35 info_flac = models.FileField(upload_to='archive/info_flac', blank=True)
38 verbose_name = _("project")
39 verbose_name_plural = _("projects")
45 def get_description(self):
47 return self.description
48 return "Audiobook nagrany w ramach projektu %s%s." % (
50 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
53 def get_icon_url(self):
57 'https://' + Site.objects.get_current().domain,
62 def source_upload_to(intance, filename):
63 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
66 class Audiobook(models.Model):
67 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
68 verbose_name=_('source file'), editable=False)
69 source_sha1 = models.CharField(max_length=40, editable=False)
70 duration = models.FloatField(null=True, editable=False)
72 title = models.CharField(max_length=255, verbose_name=_('title'))
73 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
74 default='', blank=True)
75 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
76 youtube_volume = models.CharField(
77 _("Volume name for YouTube"),
81 "If set, audiobooks with the save value will be published as single YouTube video."
84 artist = models.CharField(max_length=255, verbose_name=_('artist'))
85 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
86 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
87 date = models.CharField(max_length=255, verbose_name=_('date'))
88 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
89 slug = models.SlugField(max_length=120, blank=True, help_text=_('WL catalogue slug of the book.'))
90 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
91 modified = models.DateTimeField(null=True, editable=False)
92 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
95 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
96 mp3_task = models.CharField(max_length=64, null=True, editable=False)
97 mp3_tags = models.TextField(null=True, editable=False)
98 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
99 mp3_published_tags = models.TextField(null=True, editable=False)
100 mp3_published = models.DateTimeField(null=True, editable=False)
102 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
103 ogg_task = models.CharField(max_length=64, null=True, editable=False)
104 ogg_tags = models.TextField(null=True, editable=False)
105 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
106 ogg_published_tags = models.TextField(null=True, editable=False)
107 ogg_published = models.DateTimeField(null=True, editable=False)
109 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
110 youtube_task = models.CharField(max_length=64, null=True, editable=False)
111 youtube_tags = models.TextField(null=True, editable=False)
112 youtube_published_tags = models.TextField(null=True, editable=False)
113 youtube_published = models.DateTimeField(null=True, editable=False)
114 youtube_id = models.CharField(max_length=255, blank=True, default='')
115 youtube_queued = models.DateTimeField(null=True, blank=True)
118 verbose_name = _("audiobook")
119 verbose_name_plural = _("audiobooks")
120 ordering = ("title",)
127 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
130 def parts_count(self):
131 return type(self).objects.filter(slug=self.slug).count()
134 def youtube_volume_count(self):
137 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
138 if not a.youtube_volume or a.youtube_volume != prev_volume:
140 prev_volume = a.youtube_volume
144 def youtube_volume_index(self):
147 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
148 if not a.youtube_volume or a.youtube_volume != prev_volume:
150 prev_volume = a.youtube_volume
154 def is_youtube_publishable(self):
156 not self.youtube_volume
158 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
162 def youtube_publish(self):
163 if not self.is_youtube_publishable:
165 self.youtube_status = status.QUEUED
166 self.youtube_queued = now()
167 self.save(update_fields=['youtube_status', 'youtube_queued'])
169 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
170 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
171 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
172 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
173 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
174 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
177 return self.mp3_published and self.ogg_published
179 def get_source_sha1(self):
180 assert self.pk or self.source_sha1
181 if not self.source_sha1:
182 with advisory_lock(f'get_source_sha1_{self.pk}'):
183 with open(self.source_file.path, 'rb') as f:
184 self.source_sha1 = sha1_file(f)
185 self.save(update_fields=['source_sha1'])
186 return self.source_sha1
188 def new_publish_tags(self):
191 title += ' (tłum. %s)' % self.translator
193 copyright = "%s %s." % (
194 self.date, ORGANIZATION)
196 copyright += " Licensed to the public under %s verify at %s" % (
197 self.license.uri, self.url)
199 comment = "\n".join((
200 self.project.get_description(),
206 'albumartist': ORGANIZATION,
207 'artist': self.artist,
209 'conductor': self.conductor,
211 'copyright': copyright,
215 'organization': ORGANIZATION,
217 'project': self.project.name,
220 tags['license'] = self.license.uri
221 if self.project.sponsors:
222 tags['funded_by'] = self.project.sponsors
225 tags['flac_sha1'] = self.source_sha1
231 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
238 from librarian.document import WLDocument, parser
239 from lxml import etree
241 xml_url = self.book.get('xml', None)
248 requests.get(xml_url).content