1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from collections import OrderedDict
6 from collections import namedtuple
7 from django.db import models
8 from django.utils.translation import gettext_lazy as _
9 from slugify import slugify
11 from mutagen import id3
12 from fnpdjango.storage import BofhFileSystemStorage
15 def _file_upload_to(i, _n):
18 name += f'_{i.index:03d}'
20 name += f'_' + slugify(i.part_name)
22 return f'book/{ext}/{name}.{ext}'
25 class BookMedia(models.Model):
26 """Represents media attached to a book."""
27 FileFormat = namedtuple("FileFormat", "name ext")
28 formats = OrderedDict([
29 ('mp3', FileFormat(name='MP3', ext='mp3')),
30 ('ogg', FileFormat(name='Ogg Vorbis', ext='ogg')),
31 ('daisy', FileFormat(name='DAISY', ext='daisy.zip')),
32 ('audio.epub', FileFormat(name='EPUB+audio', ext='audio.epub')),
33 ('sync', FileFormat(name='sync', ext='sync.txt')),
35 format_choices = [(k, _('%s file' % t.name)) for k, t in formats.items()]
37 type = models.CharField(_('type'), db_index=True, choices=format_choices, max_length=20)
38 name = models.CharField(_('name'), max_length=512)
39 part_name = models.CharField(_('part name'), default='', blank=True, max_length=512)
40 index = models.IntegerField(_('index'), default=0)
41 file = models.FileField(_('file'), max_length=600, upload_to=_file_upload_to, storage=BofhFileSystemStorage())
42 duration = models.FloatField(null=True, blank=True)
43 uploaded_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False, db_index=True)
44 project_description = models.CharField(max_length=2048, blank=True)
45 project_icon = models.CharField(max_length=2048, blank=True)
46 extra_info = models.TextField(_('extra information'), default='{}', editable=False)
47 book = models.ForeignKey('Book', models.CASCADE, related_name='media')
48 source_sha1 = models.CharField(null=True, blank=True, max_length=40, editable=False)
51 return self.file.name.split("/")[-1]
54 ordering = ('type', 'index')
55 verbose_name = _('book media')
56 verbose_name_plural = _('book media')
57 app_label = 'catalogue'
59 def get_extra_info_json(self):
60 return json.loads(self.extra_info or '{}')
62 def get_nice_filename(self):
63 parts_count = 1 + type(self).objects.filter(book=self.book, type=self.type).exclude(pk=self.pk).count()
67 name += f'_{self.index:03d}'
69 name += f'_' + slugify(self.part_name)
71 return f'{name}.{ext}'
73 def save(self, parts_count=None, *args, **kwargs):
74 from catalogue.utils import ExistingFile, remove_zip
77 parts_count = 1 + BookMedia.objects.filter(book=self.book, type=self.type).exclude(pk=self.pk).count()
79 self.name = self.book.pretty_title()
81 no = ('%02d' if parts_count < 100 else '%03d') % self.index
82 self.name = '%s. %s' % (no, self.book.pretty_title())
84 self.name += ', ' + self.part_name
87 old = BookMedia.objects.get(pk=self.pk)
88 except BookMedia.DoesNotExist:
91 super(BookMedia, self).save(*args, **kwargs)
93 # remove the zip package for book with modified media
95 remove_zip("%s_%s" % (old.book.slug, old.type))
96 remove_zip("%s_%s" % (self.book.slug, self.type))
98 extra_info = self.get_extra_info_json()
99 extra_info.update(self.read_meta())
100 self.extra_info = json.dumps(extra_info)
101 self.source_sha1 = self.read_source_sha1(self.file.path, self.type)
102 self.duration = self.read_duration()
103 return super(BookMedia, self).save(*args, **kwargs)
105 def read_duration(self):
107 return mutagen.File(self.file.path).info.length
113 Reads some metadata from the audiobook.
115 artist_name = director_name = project = funded_by = license = ''
116 if self.type == 'mp3':
118 audio = id3.ID3(self.file.path)
119 artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1'))
120 director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3'))
121 license = ', '.join(tag.url for tag in audio.getall('WCOP'))
122 project = ", ".join([
123 t.data.decode('utf-8') for t in audio.getall('PRIV')
124 if t.owner == 'wolnelektury.pl?project'])
125 funded_by = ", ".join([
126 t.data.decode('utf-8') for t in audio.getall('PRIV')
127 if t.owner == 'wolnelektury.pl?funded_by'])
128 except mutagen.MutagenError:
130 elif self.type == 'ogg':
132 audio = mutagen.File(self.file.path)
133 artist_name = ', '.join(audio.get('artist', []))
134 director_name = ', '.join(audio.get('conductor', []))
135 license = ', '.join(audio.get('license', []))
136 project = ", ".join(audio.get('project', []))
137 funded_by = ", ".join(audio.get('funded_by', []))
138 except (mutagen.MutagenError, AttributeError):
142 return {'artist_name': artist_name, 'director_name': director_name,
143 'project': project, 'funded_by': funded_by, 'license': license}
146 return self.formats[self.type].ext
149 def read_source_sha1(filepath, filetype):
151 Reads source file SHA1 from audiobok metadata.
153 if filetype == 'mp3':
155 audio = id3.ID3(filepath)
156 return [t.data.decode('utf-8') for t in audio.getall('PRIV')
157 if t.owner == 'wolnelektury.pl?flac_sha1'][0]
158 except (mutagen.MutagenError, IndexError):
160 elif filetype == 'ogg':
162 audio = mutagen.File(filepath)
163 return audio.get('flac_sha1', [None])[0]
164 except (mutagen.MutagenError, AttributeError, IndexError):
171 return self.get_extra_info_json().get('director_name', None)
175 return self.get_extra_info_json().get('artist_name', None)