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)
34 verbose_name = _("project")
35 verbose_name_plural = _("projects")
41 def get_description(self):
43 return self.description
44 return "Audiobook nagrany w ramach projektu %s%s." % (
46 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
50 class Piece(models.Model):
51 name = models.CharField(max_length=255)
52 source_file = models.FileField(upload_to='piece')
53 is_outro = models.BooleanField(default=False)
54 min_audiobook_duration = models.IntegerField(default=0)
60 def source_upload_to(intance, filename):
61 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
64 class Audiobook(models.Model):
65 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
66 verbose_name=_('source file'), editable=False)
67 source_sha1 = models.CharField(max_length=40, editable=False)
68 duration = models.FloatField(null=True, editable=False)
70 title = models.CharField(max_length=255, verbose_name=_('title'))
71 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
72 default='', blank=True)
73 index = models.IntegerField(verbose_name=_('index'), default=0, help_text=_('Ordering of parts of a book.'))
74 youtube_volume = models.CharField(
75 _("Volume name for YouTube"),
79 "If set, audiobooks with the save value will be published as single YouTube video."
82 artist = models.CharField(max_length=255, verbose_name=_('artist'))
83 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
84 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
85 date = models.CharField(max_length=255, verbose_name=_('date'))
86 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
87 slug = models.SlugField(max_length=120, blank=True, help_text=_('WL catalogue slug of the book.'))
88 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
89 modified = models.DateTimeField(null=True, editable=False)
90 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
93 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
94 mp3_task = models.CharField(max_length=64, null=True, editable=False)
95 mp3_tags = models.TextField(null=True, editable=False)
96 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
97 mp3_published_tags = models.TextField(null=True, editable=False)
98 mp3_published = models.DateTimeField(null=True, editable=False)
100 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
101 ogg_task = models.CharField(max_length=64, null=True, editable=False)
102 ogg_tags = models.TextField(null=True, editable=False)
103 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
104 ogg_published_tags = models.TextField(null=True, editable=False)
105 ogg_published = models.DateTimeField(null=True, editable=False)
107 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
108 youtube_task = models.CharField(max_length=64, null=True, editable=False)
109 youtube_tags = models.TextField(null=True, editable=False)
110 youtube_published_tags = models.TextField(null=True, editable=False)
111 youtube_published = models.DateTimeField(null=True, editable=False)
112 youtube_id = models.CharField(max_length=255, blank=True, default='')
113 youtube_queued = models.DateTimeField(null=True, blank=True)
116 verbose_name = _("audiobook")
117 verbose_name_plural = _("audiobooks")
118 ordering = ("title",)
125 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
128 def parts_count(self):
129 return type(self).objects.filter(slug=self.slug).count()
132 def youtube_volume_count(self):
135 for a in type(self).objects.filter(slug=self.slug).order_by("index"):
136 if not a.youtube_volume or a.youtube_volume != prev_volume:
138 prev_volume = a.youtube_volume
142 def youtube_volume_index(self):
145 for a in type(self).objects.filter(slug=self.slug, index__lte=self.index).order_by("index"):
146 if not a.youtube_volume or a.youtube_volume != prev_volume:
148 prev_volume = a.youtube_volume
152 def is_youtube_publishable(self):
154 not self.youtube_volume
156 .objects.filter(youtube_volume=self.youtube_volume, index__lt=self.index)
160 def youtube_publish(self):
161 if not self.is_youtube_publishable:
163 self.youtube_status = status.QUEUED
164 self.youtube_queued = now()
165 self.save(update_fields=['youtube_status', 'youtube_queued'])
167 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
168 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
169 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
170 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
171 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
172 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
175 return self.mp3_published and self.ogg_published
177 def get_source_sha1(self):
178 assert self.pk or self.source_sha1
179 if not self.source_sha1:
180 with advisory_lock(f'get_source_sha1_{self.pk}'):
181 with open(self.source_file.path, 'rb') as f:
182 self.source_sha1 = sha1_file(f)
183 self.save(update_fields=['source_sha1'])
184 return self.source_sha1
186 def new_publish_tags(self):
189 title += ' (tłum. %s)' % self.translator
191 copyright = "%s %s." % (
192 self.date, ORGANIZATION)
194 copyright += " Licensed to the public under %s verify at %s" % (
195 self.license.uri, self.url)
197 comment = "\n".join((
198 self.project.get_description(),
204 'albumartist': ORGANIZATION,
205 'artist': self.artist,
207 'conductor': self.conductor,
209 'copyright': copyright,
213 'organization': ORGANIZATION,
215 'project': self.project.name,
218 tags['license'] = self.license.uri
219 if self.project.sponsors:
220 tags['funded_by'] = self.project.sponsors
223 tags['flac_sha1'] = self.source_sha1
229 apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
236 from librarian.document import WLDocument, parser
237 from lxml import etree
239 xml_url = self.book.get('xml', None)
246 requests.get(xml_url).content