1 from datetime import datetime
9 from tempfile import NamedTemporaryFile
10 from time import sleep
12 from celery.task import Task
13 from django.db.models import F
14 from django.contrib.auth.models import User
15 from mutagen import File
16 from mutagen import id3
18 from apiclient import api_call
19 from archive.constants import status
20 from archive.models import Audiobook
21 from archive.settings import COVER_IMAGE, UPLOAD_URL
22 from archive.utils import ExistingFile
25 class AudioFormatTask(Task):
28 class RemoteOperationError(BaseException):
32 def set_status(cls, aid, status):
33 Audiobook.objects.filter(pk=aid).update(
34 **{'%s_status' % cls.prefix: status})
37 def encode(in_path, out_path):
41 def set_tags(cls, audiobook, file_name):
42 tags = getattr(audiobook, "get_%s_tags" % cls.prefix)()['tags']
43 if not tags.get('flac_sha1'):
44 tags['flac_sha1'] = audiobook.get_source_sha1()
45 audio = File(file_name)
46 for k, v in tags.items():
51 def save(cls, audiobook, file_name):
52 field = "%s_file" % cls.prefix
53 getattr(audiobook, field).save(
54 "%d.%s" % (audiobook.pk, cls.ext),
55 ExistingFile(file_name),
58 os.chmod(getattr(audiobook, field).path, stat.S_IREAD|stat.S_IWRITE|stat.S_IRGRP|stat.S_IROTH)
59 Audiobook.objects.filter(pk=audiobook.pk).update(
60 **{field: getattr(audiobook, field)})
63 def published(cls, aid):
65 "%s_published_tags" % cls.prefix: F("%s_tags" % cls.prefix),
66 "%s_tags" % cls.prefix: None,
67 "%s_published" % cls.prefix: datetime.now(),
68 '%s_status' % cls.prefix: None,
70 Audiobook.objects.filter(pk=aid).update(**kwargs)
73 def put(cls, user, audiobook, path):
74 tags = getattr(audiobook, "get_%s_tags" % cls.prefix)()
79 'part_name': audiobook.part_name,
80 'part_index': audiobook.index,
81 'parts_count': audiobook.parts_count,
82 'source_sha1': audiobook.source_sha1,
85 'description': audiobook.project.get_description(),
86 'icon': audiobook.project.get_icon_url(),
89 with open(path, 'rb') as f:
90 api_call(user, UPLOAD_URL, data=data, files={
94 def run(self, uid, aid, publish=True):
96 audiobook = Audiobook.objects.get(id=aid)
97 self.audiobook = audiobook
98 self.set_status(aid, status.ENCODING)
101 user = User.objects.get(id=uid)
105 out_file = NamedTemporaryFile(delete=False, prefix='%d-' % aid, suffix='.%s' % self.ext)
107 self.encode(self.get_source_file_paths(audiobook), out_file.name)
108 self.set_status(aid, status.TAGGING)
109 self.set_tags(audiobook, out_file.name)
110 self.set_status(aid, status.SENDING)
113 self.put(user, audiobook, out_file.name)
116 self.set_status(aid, None)
118 self.save(audiobook, out_file.name)
120 def get_source_file_paths(self, audiobook):
121 return [audiobook.source_file.path]
123 def on_failure(self, exc, task_id, args, kwargs, einfo):
124 aid = (args[0], kwargs.get('aid'))[0]
125 self.set_status(aid, None)
128 class Mp3Task(AudioFormatTask):
131 # these shouldn't be staticmethods
132 def id3_text(tag, text):
133 return tag(encoding=3, text=text)
134 def id3_url(tag, text):
136 def id3_comment(tag, text, lang='pol'):
137 return tag(encoding=3, lang=lang, desc='', text=text)
138 def id3_priv(tag, text, what=''):
139 return tag(owner='wolnelektury.pl?%s' % what, data=text.encode('utf-8'))
142 'album': (id3_text, id3.TALB),
143 'albumartist': (id3_text, id3.TPE2),
144 'artist': (id3_text, id3.TPE1),
145 'conductor': (id3_text, id3.TPE3),
146 'copyright': (id3_text, id3.TCOP),
147 'date': (id3_text, id3.TDRC),
148 'genre': (id3_text, id3.TCON),
149 'language': (id3_text, id3.TLAN),
150 'organization': (id3_text, id3.TPUB),
151 'title': (id3_text, id3.TIT2),
152 'comment': (id3_comment, id3.COMM, 'pol'),
153 'contact': (id3_url, id3.WOAF),
154 'license': (id3_url, id3.WCOP),
155 'flac_sha1': (id3_priv, id3.PRIV, 'flac_sha1'),
156 'project': (id3_priv, id3.PRIV, 'project'),
157 'funded_by': (id3_priv, id3.PRIV, 'funded_by'),
161 def encode(in_paths, out_path):
162 assert len(in_paths) == 1
163 in_path = in_paths[0]
164 # 44.1kHz 64kbps mono MP3
165 subprocess.check_call(['ffmpeg',
166 '-i', in_path.encode('utf-8'),
171 '-acodec', 'libmp3lame',
172 out_path.encode('utf-8')
176 def set_tags(cls, audiobook, file_name):
177 mp3_tags = audiobook.get_mp3_tags()['tags']
178 if not mp3_tags.get('flac_sha1'):
179 mp3_tags['flac_sha1'] = audiobook.get_source_sha1()
180 audio = id3.ID3(file_name)
181 for k, v in mp3_tags.items():
182 factory_tuple = cls.TAG_MAP[k]
183 factory, tagtype = factory_tuple[:2]
184 audio.add(factory(tagtype, v, *factory_tuple[2:]))
187 mime = mimetypes.guess_type(COVER_IMAGE)
188 with open(COVER_IMAGE, 'rb') as f:
189 audio.add(id3.APIC(encoding=0, mime=mime, type=3, desc='', data=f.read()))
194 class OggTask(AudioFormatTask):
198 def encode(in_paths, out_path):
199 assert len(in_paths) == 1
200 in_path = in_paths[0]
201 # 44.1kHz 64kbps mono Ogg Vorbis
202 subprocess.check_call(['ffmpeg',
203 '-i', in_path.encode('utf-8'),
208 '-acodec', 'libvorbis',
209 out_path.encode('utf-8')