Easier cutting.
[audio.git] / src / archive / tasks.py
index 1eeeb28..3538a73 100644 (file)
@@ -10,6 +10,7 @@ from tempfile import NamedTemporaryFile
 from time import sleep
 
 from celery.task import Task
+from django.conf import settings
 from django.db.models import F
 from django.contrib.auth.models import User
 from mutagen import File
@@ -18,7 +19,7 @@ from mutagen import id3
 from apiclient import api_call
 from archive.constants import status
 from archive.models import Audiobook
-from archive.settings import BUILD_PATH, COVER_IMAGE, UPLOAD_URL
+from archive.settings import COVER_IMAGE, UPLOAD_URL
 from archive.utils import ExistingFile
 
 
@@ -31,7 +32,7 @@ class AudioFormatTask(Task):
     @classmethod
     def set_status(cls, aid, status):
         Audiobook.objects.filter(pk=aid).update(
-            **{'%s_status' % cls.ext: status})
+            **{'%s_status' % cls.prefix: status})
 
     @staticmethod
     def encode(in_path, out_path):
@@ -39,7 +40,7 @@ class AudioFormatTask(Task):
 
     @classmethod
     def set_tags(cls, audiobook, file_name):
-        tags = getattr(audiobook, "get_%s_tags" % cls.ext)()['tags']
+        tags = getattr(audiobook, "get_%s_tags" % cls.prefix)()['tags']
         if not tags.get('flac_sha1'):
             tags['flac_sha1'] = audiobook.get_source_sha1()
         audio = File(file_name)
@@ -49,7 +50,7 @@ class AudioFormatTask(Task):
 
     @classmethod
     def save(cls, audiobook, file_name):
-        field = "%s_file" % cls.ext
+        field = "%s_file" % cls.prefix
         getattr(audiobook, field).save(
             "%d.%s" % (audiobook.pk, cls.ext),
             ExistingFile(file_name),
@@ -62,16 +63,16 @@ class AudioFormatTask(Task):
     @classmethod
     def published(cls, aid):
         kwargs = {
-            "%s_published_tags" % cls.ext: F("%s_tags" % cls.ext),
-            "%s_tags" % cls.ext: None,
-            "%s_published" % cls.ext: datetime.now(),
-            '%s_status' % cls.ext: None,
+            "%s_published_tags" % cls.prefix: F("%s_tags" % cls.prefix),
+            "%s_tags" % cls.prefix: None,
+            "%s_published" % cls.prefix: datetime.now(),
+            '%s_status' % cls.prefix: None,
         }
         Audiobook.objects.filter(pk=aid).update(**kwargs)
 
     @classmethod
     def put(cls, user, audiobook, path):
-        tags = getattr(audiobook, "get_%s_tags" % cls.ext)()
+        tags = getattr(audiobook, "get_%s_tags" % cls.prefix)()
         data = {
             'book': tags['url'],
             'type': cls.ext,
@@ -80,29 +81,32 @@ class AudioFormatTask(Task):
             'part_index': audiobook.index,
             'parts_count': audiobook.parts_count,
             'source_sha1': audiobook.source_sha1,
+
+            'project_description': audiobook.project.get_description(),
+            'project_icon': audiobook.project.get_icon_url(),
         }
-        api_call(user, UPLOAD_URL, data=data, files={
-            "file": open(path, 'rb'),
-        })
+        with open(path, 'rb') as f:
+            api_call(user, UPLOAD_URL, data=data, files={
+                "file": f,
+            })
 
     def run(self, uid, aid, publish=True):
         aid = int(aid)
         audiobook = Audiobook.objects.get(id=aid)
+        self.audiobook = audiobook
         self.set_status(aid, status.ENCODING)
 
-        user = User.objects.get(id=uid)
-
-        try:
-            os.makedirs(BUILD_PATH)
-        except OSError as e:
-            if e.errno == errno.EEXIST:
-                pass
-            else:
-                raise
+        if uid:
+            user = User.objects.get(id=uid)
+        else:
+            user = None
 
-        out_file = NamedTemporaryFile(delete=False, prefix='%d-' % aid, suffix='.%s' % self.ext, dir=BUILD_PATH)
+        out_file = NamedTemporaryFile(
+            delete=False, prefix='%d-' % aid, suffix='.%s' % self.ext,
+            dir=settings.FILE_UPLOAD_TEMP_DIR
+        )
         out_file.close()
-        self.encode(audiobook.source_file.path, out_file.name)
+        self.encode(self.get_source_file_paths(audiobook), out_file.name)
         self.set_status(aid, status.TAGGING)
         self.set_tags(audiobook, out_file.name)
         self.set_status(aid, status.SENDING)
@@ -115,22 +119,25 @@ class AudioFormatTask(Task):
 
         self.save(audiobook, out_file.name)
 
+    def get_source_file_paths(self, audiobook):
+        return [audiobook.source_file.path]
+
     def on_failure(self, exc, task_id, args, kwargs, einfo):
         aid = (args[0], kwargs.get('aid'))[0]
         self.set_status(aid, None)
 
 
 class Mp3Task(AudioFormatTask):
-    ext = 'mp3'
+    prefix = ext = 'mp3'
 
     # these shouldn't be staticmethods
     def id3_text(tag, text):
-        return tag(encoding=1, text=text)
+        return tag(encoding=3, text=text)
     def id3_url(tag, text):
         return tag(url=text)
-    def id3_comment(tag, text, lang=u'pol'):
-        return tag(encoding=1, lang=lang, desc=u'', text=text)
-    def id3_priv(tag, text, what=u''):
+    def id3_comment(tag, text, lang='pol'):
+        return tag(encoding=3, lang=lang, desc='', text=text)
+    def id3_priv(tag, text, what=''):
         return tag(owner='wolnelektury.pl?%s' % what, data=text.encode('utf-8'))
 
     TAG_MAP = {
@@ -153,7 +160,9 @@ class Mp3Task(AudioFormatTask):
     }
 
     @staticmethod
-    def encode(in_path, out_path):
+    def encode(in_paths, out_path):
+        assert len(in_paths) == 1
+        in_path = in_paths[0]
         # 44.1kHz 64kbps mono MP3
         subprocess.check_call(['ffmpeg', 
             '-i', in_path.encode('utf-8'),
@@ -178,18 +187,19 @@ class Mp3Task(AudioFormatTask):
 
         if COVER_IMAGE:
             mime = mimetypes.guess_type(COVER_IMAGE)
-            f = open(COVER_IMAGE)
-            audio.add(id3.APIC(encoding=0, mime=mime, type=3, desc=u'', data=f.read()))
-            f.close()
+            with open(COVER_IMAGE, 'rb') as f:
+                audio.add(id3.APIC(encoding=0, mime=mime, type=3, desc='', data=f.read()))
 
         audio.save()
 
 
 class OggTask(AudioFormatTask):
-    ext = 'ogg'
+    prefix = ext = 'ogg'
 
     @staticmethod
-    def encode(in_path, out_path):
+    def encode(in_paths, out_path):
+        assert len(in_paths) == 1
+        in_path = in_paths[0]
         # 44.1kHz 64kbps mono Ogg Vorbis
         subprocess.check_call(['ffmpeg', 
             '-i', in_path.encode('utf-8'),