1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from collections import OrderedDict
7 from collections import namedtuple
8 from django.db import models
9 from django.utils.translation import ugettext_lazy as _
11 from fnpdjango.utils.text.slughifi import slughifi
12 from mutagen import MutagenError
14 from catalogue.fields import OverwritingFileField
17 def _file_upload_to(i, _n):
18 return 'book/%(ext)s/%(name)s.%(ext)s' % {'ext': i.ext(), 'name': slughifi(i.name)}
21 class BookMedia(models.Model):
22 """Represents media attached to a book."""
23 FileFormat = namedtuple("FileFormat", "name ext")
24 formats = OrderedDict([
25 ('mp3', FileFormat(name='MP3', ext='mp3')),
26 ('ogg', FileFormat(name='Ogg Vorbis', ext='ogg')),
27 ('daisy', FileFormat(name='DAISY', ext='daisy.zip')),
29 format_choices = [(k, _('%s file' % t.name)) for k, t in formats.items()]
31 type = models.CharField(_('type'), db_index=True, choices=format_choices, max_length=20)
32 name = models.CharField(_('name'), max_length=512)
33 part_name = models.CharField(_('part name'), default='', max_length=512)
34 index = models.IntegerField(_('index'), default=0)
35 file = OverwritingFileField(_('file'), max_length=600, upload_to=_file_upload_to)
36 uploaded_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False, db_index=True)
37 extra_info = jsonfield.JSONField(_('extra information'), default={}, editable=False)
38 book = models.ForeignKey('Book', related_name='media')
39 source_sha1 = models.CharField(null=True, blank=True, max_length=40, editable=False)
41 def __unicode__(self):
42 return "%s (%s)" % (self.name, self.file.name.split("/")[-1])
45 ordering = ('type', 'name')
46 verbose_name = _('book media')
47 verbose_name_plural = _('book media')
48 app_label = 'catalogue'
50 def save(self, *args, **kwargs):
51 from catalogue.utils import ExistingFile, remove_zip
53 parts_count = BookMedia.objects.filter(book=self.book, type=self.type).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 slughifi(self.name) != slughifi(old.name):
69 self.file.save(None, ExistingFile(self.file.path), save=False, leave=True)
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, basestring):
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 for t in audio.getall('PRIV')
102 if t.owner == 'wolnelektury.pl?project'])
103 funded_by = ", ".join([
104 t.data 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 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):