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 ugettext_lazy as _
10 from slugify import slugify
11 from mutagen import MutagenError
13 from catalogue.fields import OverwriteStorage
16 def _file_upload_to(i, _n):
17 return 'book/%(ext)s/%(name)s.%(ext)s' % {'ext': i.ext(), 'name': slugify(i.name)}
20 class BookMedia(models.Model):
21 """Represents media attached to a book."""
22 FileFormat = namedtuple("FileFormat", "name ext")
23 formats = OrderedDict([
24 ('mp3', FileFormat(name='MP3', ext='mp3')),
25 ('ogg', FileFormat(name='Ogg Vorbis', ext='ogg')),
26 ('daisy', FileFormat(name='DAISY', ext='daisy.zip')),
28 format_choices = [(k, _('%s file' % t.name)) for k, t in formats.items()]
30 type = models.CharField(_('type'), db_index=True, choices=format_choices, max_length=20)
31 name = models.CharField(_('name'), max_length=512)
32 part_name = models.CharField(_('part name'), default='', blank=True, max_length=512)
33 index = models.IntegerField(_('index'), default=0)
34 file = models.FileField(_('file'), max_length=600, upload_to=_file_upload_to, storage=OverwriteStorage())
35 uploaded_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False, db_index=True)
36 extra_info = jsonfield.JSONField(_('extra information'), default={}, editable=False)
37 book = models.ForeignKey('Book', models.CASCADE, related_name='media')
38 source_sha1 = models.CharField(null=True, blank=True, max_length=40, editable=False)
41 return "%s (%s)" % (self.name, self.file.name.split("/")[-1])
44 ordering = ('type', 'name')
45 verbose_name = _('book media')
46 verbose_name_plural = _('book media')
47 app_label = 'catalogue'
49 def save(self, parts_count=None, *args, **kwargs):
50 from catalogue.utils import ExistingFile, remove_zip
53 parts_count = 1 + BookMedia.objects.filter(book=self.book, type=self.type).exclude(pk=self.pk).count()
55 self.name = self.book.pretty_title()
57 no = ('%02d' if parts_count < 100 else '%03d') % self.index
58 self.name = '%s. %s' % (no, self.book.pretty_title())
60 self.name += ', ' + self.part_name
63 old = BookMedia.objects.get(pk=self.pk)
64 except BookMedia.DoesNotExist:
67 # if name changed, change the file name, too
68 if slugify(self.name) != slugify(old.name):
69 self.file.save(None, ExistingFile(self.file.path), save=False)
71 super(BookMedia, self).save(*args, **kwargs)
73 # remove the zip package for book with modified media
75 remove_zip("%s_%s" % (old.book.slug, old.type))
76 remove_zip("%s_%s" % (self.book.slug, self.type))
78 extra_info = self.extra_info
79 if isinstance(extra_info, str):
80 # Walkaround for weird jsonfield 'no-decode' optimization.
81 extra_info = json.loads(extra_info)
82 extra_info.update(self.read_meta())
83 self.extra_info = extra_info
84 self.source_sha1 = self.read_source_sha1(self.file.path, self.type)
85 return super(BookMedia, self).save(*args, **kwargs)
89 Reads some metadata from the audiobook.
92 from mutagen import id3
94 artist_name = director_name = project = funded_by = ''
95 if self.type == 'mp3':
97 audio = id3.ID3(self.file.path)
98 artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1'))
99 director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3'))
100 project = ", ".join([
101 t.data.decode('utf-8') for t in audio.getall('PRIV')
102 if t.owner == 'wolnelektury.pl?project'])
103 funded_by = ", ".join([
104 t.data.decode('utf-8') for t in audio.getall('PRIV')
105 if t.owner == 'wolnelektury.pl?funded_by'])
108 elif self.type == 'ogg':
110 audio = mutagen.File(self.file.path)
111 artist_name = ', '.join(audio.get('artist', []))
112 director_name = ', '.join(audio.get('conductor', []))
113 project = ", ".join(audio.get('project', []))
114 funded_by = ", ".join(audio.get('funded_by', []))
115 except (MutagenError, AttributeError):
119 return {'artist_name': artist_name, 'director_name': director_name,
120 'project': project, 'funded_by': funded_by}
123 return self.formats[self.type].ext
126 def read_source_sha1(filepath, filetype):
128 Reads source file SHA1 from audiobok metadata.
131 from mutagen import id3
133 if filetype == 'mp3':
135 audio = id3.ID3(filepath)
136 return [t.data.decode('utf-8') for t in audio.getall('PRIV')
137 if t.owner == 'wolnelektury.pl?flac_sha1'][0]
138 except (MutagenError, IndexError):
140 elif filetype == 'ogg':
142 audio = mutagen.File(filepath)
143 return audio.get('flac_sha1', [None])[0]
144 except (MutagenError, AttributeError, IndexError):
151 return self.extra_info.get('director_name', None)
155 return self.extra_info.get('artist_name', None)