Beispiel #1
0
 def get_users_comments(takes):
     comment_hash = []
     user_hash = []
     for take in takes:
         chapter_comments = Comment.get_comments(chapter_id=take.chunk.chapter.id)
         chunk_comments = Comment.get_comments(chunk_id=take.chunk.id)
         take_comments = Comment.get_comments(take_id=take.id)
         comments = list(chain(chapter_comments, chunk_comments, take_comments))
         for comment in comments:
             user = comment.owner
             if user:
                 user_hash.append(
                     {ZipIt.get_local_file_hash(user.name_audio): FileUtility.file_name(user.name_audio)})
             comment_hash.append(
                 {ZipIt.get_local_file_hash(comment.location): FileUtility.file_name(comment.location)})
     return comment_hash + user_hash
Beispiel #2
0
 def local_file_hash_list(take_list):
     hashes = []
     for take in take_list:
         hashes.append({
             ZipIt.get_local_file_hash(take.location):
             FileUtility.file_name(take.location)
         })
     return hashes
Beispiel #3
0
    def post(self, request):
        data = request.data
        if all(k in data["project"] for k in ('language', 'version', 'book')):
            takes_name_and_locations = []
            response_array = {
                "en-x-demo2_ulb_b42_mrk_c07_v31-32_t03.wav":
                "6d6f8d635297adb9b8e83f38e0634er4",
                "en-x-demo2_ulb_b42_mrk_c07_v14-16_t02.wav":
                "d51605fc59e04feb8db2045735f12800",
                "en-x-demo2_ulb_b42_mrk_c07_v02-04_t07.wav":
                "e80c058c73b83a76bb9a60c781c21d91",
                "en-x-demo2_ulb_b42_mrk_c07_v24-26_t02.wav":
                "2ab809fa13b5182de67b9892162ab670",
                "en-x-demo2_ulb_b42_mrk_c07_v17-19_t03.wav":
                "b5c7aacdcafca203ded43e130151bece",
                "en-x-demo2_ulb_b42_mrk_c07_v33-35_t04.wav":
                "c811f1fc8c2e4f0e9a3023117788f8f1",
                "en-x-demo2_ulb_b42_mrk_c07_v05_t02.wav":
                "82f7d1a6fa392b2100f90dbde1602f2e",
                "en-x-demo2_ulb_b42_mrk_c07_v11-13_t02.wav":
                "883c8e628f0bff7f7f60e16667b587a4",
                "en-x-demo2_ulb_b42_mrk_c07_v36-37_t02.wav":
                "84697692eef682f3537a87e58393e465",
                "en-x-demo2_ulb_b42_mrk_c07_v20-23_t10.wav":
                "c6832e0a287f94a47918c02e5a826832",
                "en-x-demo2_ulb_b42_mrk_c07_v27-28_t02.wav":
                "81cd316c41b9c7d6ad3bc8b4d3d1b5c2",
                "en-x-demo2_ulb_b42_mrk_c07_v29-30_t02.wav":
                "db3ce227b34a54bfaddaca42c2d33c6e",
                # "chapter.wav": "0a4af2fc1f922b083ab84ce695c30904",
                # "en-x-demo2_ulb_b42_mrk_c07_v01_t05.wav": "6be47559aeb09c0526fe5c9bf415d726",
                # "en-x-demo2_ulb_b42_mrk_c07_v06-07_t04.wav": "f2ebbcc59319b16b67f07b055ed5cc9b",
                # "en-x-demo2_ulb_b42_mrk_c07_v08-10_t04.wav": "f92d2bc9d8611b3dc7a8d720a71f0873"
            }

            project = Chunk.getChunksWithTakesByProject(data["project"])
            for chunk in project["chunks"]:
                for take in chunk['takes']:
                    location = os.path.join(settings.BASE_DIR,
                                            take['take']['location'])
                    file_name = getFileName(location)
                    file_hash = md5Hash(location)
                    if file_name not in response_array:
                        takes_name_and_locations.append(location)
                    elif file_hash != response_array[file_name]:
                        takes_name_and_locations.append(location)

            mf = io.StringIO()
            with zipfile.ZipFile(mf, 'w') as zipped_f:
                for audio in takes_name_and_locations:
                    zipped_f.write(
                        audio, getFilePath(FileUtility.relative_path(audio)))
            response = HttpResponse(mf.getvalue(),
                                    content_type='application/zip')
            response['Content-Disposition'] = 'attachment; filename=file.zip'
            return response
        else:
            return Response({"error": "not_enough_parameters"}, status=400)
Beispiel #4
0
    def get(self, request):
        user_data = {
            "icon_hash": "system",  # request.user.icon_hash,
            "name_audio": "system"  # request.user.name_audio
        }

        task = cleanup_orphan_files.delay(FileUtility(),
                                          title='Clean orphan files',
                                          started=datetime.datetime.now(),
                                          user=user_data)

        return Response({
            "response": "processing",
            "task_id": task.id
        },
                        status=202)
Beispiel #5
0
    def post(self, request):
        data = request.data
        if "language" in data and "version" in data and "book" in data and "chapter" in data:
            chunks = Take.stitchSource(data)
            # chunks = list(chunks)
            chunks = sorted(chunks, key=lambda x: x.startv)
            loclst = []
            for chunk in chunks:
                takes = chunk.take_set.all()
                for take in takes:
                    # do not include takes that are not published
                    if take.is_publish is not True:
                        continue
                    loclst.append(
                        os.path.join(settings.BASE_DIR, take.location))

            if len(loclst) > 0:
                stitchedSource = AudioSegment.from_mp3(loclst[0])
                loclst.pop(0)
                for item in loclst:
                    stitchedSource = stitchedSource +\
                        AudioSegment.from_mp3(item)

                stitch_folder = os.path.join(settings.BASE_DIR, 'media/source')
                if not os.path.exists(stitch_folder):
                    os.makedirs(stitch_folder)

                project_name = data["language"] + "_" + data[
                    "version"] + "_" + data["book"] + "_" + str(
                        data["chapter"])

                stitchedSource.export(os.path.join(stitch_folder,
                                                   project_name + ".mp3"),
                                      format="mp3")
            else:
                return Response({"error": "no_published_takes"}, status=400)
        else:
            return Response({"error": "not_enough_parameters"}, status=400)

        return Response(
            {
                "location":
                FileUtility.relative_path(
                    os.path.join(stitch_folder, project_name + ".mp3"))
            },
            status=200)
    def post(self, request):
        data = request.data
        if all(k in data["project"] for k in ('language', 'version', 'book')):
            takes_name_and_locations = []
            response_array = {
                "en-x-demo2_ulb_b42_mrk_c07_v31-32_t03.wav": "6d6f8d635297adb9b8e83f38e0634er4",
                "en-x-demo2_ulb_b42_mrk_c07_v14-16_t02.wav": "d51605fc59e04feb8db2045735f12800",
                "en-x-demo2_ulb_b42_mrk_c07_v02-04_t07.wav": "e80c058c73b83a76bb9a60c781c21d91",
                "en-x-demo2_ulb_b42_mrk_c07_v24-26_t02.wav": "2ab809fa13b5182de67b9892162ab670",
                "en-x-demo2_ulb_b42_mrk_c07_v17-19_t03.wav": "b5c7aacdcafca203ded43e130151bece",
                "en-x-demo2_ulb_b42_mrk_c07_v33-35_t04.wav": "c811f1fc8c2e4f0e9a3023117788f8f1",
                "en-x-demo2_ulb_b42_mrk_c07_v05_t02.wav": "82f7d1a6fa392b2100f90dbde1602f2e",
                "en-x-demo2_ulb_b42_mrk_c07_v11-13_t02.wav": "883c8e628f0bff7f7f60e16667b587a4",
                "en-x-demo2_ulb_b42_mrk_c07_v36-37_t02.wav": "84697692eef682f3537a87e58393e465",
                "en-x-demo2_ulb_b42_mrk_c07_v20-23_t10.wav": "c6832e0a287f94a47918c02e5a826832",
                "en-x-demo2_ulb_b42_mrk_c07_v27-28_t02.wav": "81cd316c41b9c7d6ad3bc8b4d3d1b5c2",
                "en-x-demo2_ulb_b42_mrk_c07_v29-30_t02.wav": "db3ce227b34a54bfaddaca42c2d33c6e",
                # "chapter.wav": "0a4af2fc1f922b083ab84ce695c30904",
                # "en-x-demo2_ulb_b42_mrk_c07_v01_t05.wav": "6be47559aeb09c0526fe5c9bf415d726",
                # "en-x-demo2_ulb_b42_mrk_c07_v06-07_t04.wav": "f2ebbcc59319b16b67f07b055ed5cc9b",
                # "en-x-demo2_ulb_b42_mrk_c07_v08-10_t04.wav": "f92d2bc9d8611b3dc7a8d720a71f0873"
            }

            project = Chunk.getChunksWithTakesByProject(data["project"])
            for chunk in project["chunks"]:
                for take in chunk['takes']:
                    location = os.path.join(settings.BASE_DIR, take['take']['location'])
                    file_name = getFileName(location)
                    file_hash = md5Hash(location)
                    if file_name not in response_array:
                        takes_name_and_locations.append(location)
                    elif file_hash != response_array[file_name]:
                        takes_name_and_locations.append(location)
                
            mf = io.StringIO()
            with zipfile.ZipFile(mf, 'w') as zipped_f:
                for audio in takes_name_and_locations:
                    zipped_f.write(audio, getFilePath(FileUtility.relative_path(audio)))
            response = HttpResponse(mf.getvalue(), content_type='application/zip')
            response['Content-Disposition'] = 'attachment; filename=file.zip'
            return response
        else:
            return Response({"error": "not_enough_parameters"}, status=400)
Beispiel #7
0
    def create_name_audio(self, name_audio, uuid_name):
        nameaudios_folder = os.path.join(
            settings.BASE_DIR, "media", "dump", "name_audios")
        nameaudio_location = os.path.join(nameaudios_folder, uuid_name)
        relpath = FileUtility.relative_path(nameaudio_location)

        if not os.path.exists(nameaudios_folder):
            os.makedirs(nameaudios_folder)

        try:
            name = self.get_blob_from_base64(name_audio)
            with open(nameaudio_location + '.webm', 'wb') as audio_file:
                audio_file.write(name)
            sound = pydub.AudioSegment.from_file(nameaudio_location + '.webm')
            sound.export(nameaudio_location, format='mp3')
            os.remove(nameaudio_location + ".webm")
        except Exception:
            if os.path.isfile(nameaudio_location + '.webm'):
                os.remove(nameaudio_location + '.webm')
            return None

        return relpath
Beispiel #8
0
    def create_name_audio(self, name_audio, uuid_name):
        nameaudios_folder = os.path.join(settings.BASE_DIR, "media", "dump",
                                         "name_audios")
        nameaudio_location = os.path.join(nameaudios_folder, uuid_name)
        relpath = FileUtility.relative_path(nameaudio_location)

        if not os.path.exists(nameaudios_folder):
            os.makedirs(nameaudios_folder)

        try:
            name = self.get_blob_from_base64(name_audio)
            with open(nameaudio_location + '.webm', 'wb') as audio_file:
                audio_file.write(name)
            sound = pydub.AudioSegment.from_file(nameaudio_location + '.webm')
            sound.export(nameaudio_location, format='mp3')
            os.remove(nameaudio_location + ".webm")
        except Exception:
            if os.path.isfile(nameaudio_location + '.webm'):
                os.remove(nameaudio_location + '.webm')
            return None

        return relpath
Beispiel #9
0
    def create(self, request):

        data = request.data

        if "comment" not in data \
                or "object" not in data or "type" not in data:
            return Response({"error": "not_enough_parameters"},
                            status=status.HTTP_400_BAD_REQUEST)

        comment = data["comment"]
        obj = data["object"]
        obj_type = data["type"]

        try:
            if obj_type == 'chapter':
                q_obj = Chapter.objects.get(pk=obj)
            elif obj_type == 'chunk':
                q_obj = Chunk.objects.get(pk=obj)
            elif obj_type == 'take':
                q_obj = Take.objects.get(pk=obj)
            else:
                raise ValueError("bad_object")

        except Exception as e:
            return Response({"error": str(e)},
                            status=status.HTTP_400_BAD_REQUEST)

        uuid_name = str(time.time()) + str(uuid.uuid4())
        comments_folder = os.path.join(settings.BASE_DIR, "media", "dump",
                                       "comments")
        comment_location = os.path.join(comments_folder, uuid_name)
        relpath = FileUtility.relative_path(comment_location)

        if not os.path.exists(comments_folder):
            os.makedirs(comments_folder)

        try:
            comment = self.get_blob_from_base64(comment)
            with open(comment_location + '.webm', 'wb') as audio_file:
                audio_file.write(comment)
            if os.path.isfile(comment_location + '.webm'):
                print(comment_location + '.webm')
                print("file exists")
            else:
                print("file doesnt exist?")
            sound = pydub.AudioSegment.from_file(comment_location + '.webm')
            sound.export(comment_location + ".mp3", format='mp3')
            os.remove(comment_location + ".webm")
        except Exception as e:
            print(e)
            if os.path.isfile(comment_location + '.webm'):
                os.remove(comment_location + '.webm')
            return Response({"error": "bad_audio"},
                            status=status.HTTP_400_BAD_REQUEST)

        c = Comment.objects.create(location=relpath + ".mp3",
                                   content_object=q_obj,
                                   owner=request.user)
        c.save()

        serializer = self.get_serializer(c)
        return Response(serializer.data, status=status.HTTP_200_OK)
Beispiel #10
0
    def post(self, request, filename, format='tr'):
        if request.method == 'POST' and request.data['upload']:
            # TODO: remove this functionality
            response = {}
            uuid_name = str(time.time()) + str(uuid.uuid4())
            tempFolder = os.path.join(settings.BASE_DIR,
                                      "media/dump/" + uuid_name)
            if not os.path.exists(tempFolder):
                os.makedirs(tempFolder)
                data = request.data['upload']
                with open(os.path.join(tempFolder, "source.tr"),
                          'w') as temp_file:
                    for line in data:
                        temp_file.write(line)
        try:
            FNULL = open(os.devnull, 'w')
            out = subprocess.check_output([
                'java', '-jar',
                os.path.join(settings.BASE_DIR, 'aoh/aoh.jar'), '-x',
                tempFolder + "/source.tr"
            ],
                                          stderr=subprocess.STDOUT)
            os.remove(os.path.join(tempFolder, "source.tr"))
            FNULL.close()

            bookname = ''
            bookcode = ''
            langname = ''
            langcode = ''

            for root, dirs, files in os.walk(tempFolder):
                for f in files:
                    abpath = os.path.join(root, os.path.basename(f))
                    relpath = FileUtility.relative_path(abpath)
                    meta = TinyTag.get(abpath)

                    if meta and meta.artist:
                        a = meta.artist
                        lastindex = a.rfind("}") + 1
                        substr = a[:lastindex]
                        pls = json.loads(substr)

                        if bookcode != pls['slug']:
                            bookcode = pls['slug']
                            bookname = Book.getBookByCode(bookcode)
                        if langcode != pls['language']:
                            langcode = pls['language']
                            langname = Language.getLanguageByCode(langcode)

                        data = {
                            "langname": langname,
                            "bookname": bookname,
                            "duration": meta.duration
                        }

                        # highPassFilter(abpath)
                        saved = Take.prepareDataToSave(pls, relpath, data,
                                                       True)
                        if "language" in saved and "language" not in response:
                            response["language"] = saved["language"]
                        if "book" in saved and "book" not in response:
                            response["book"] = saved["book"]
                    else:
                        return Response({"error": "bad_wave_file"}, status=400)
            return Response(response, status=200)
        except Exception as e:
            shutil.rmtree(tempFolder)
            return Response({"error": str(e)}, status=400)
    def post(self, request, filename, format='tr'):
        if request.method == 'POST' and request.data['upload']:
            # TODO: remove this functionality
            response = {}
            uuid_name = str(time.time()) + str(uuid.uuid4())
            tempFolder = os.path.join(
                settings.BASE_DIR, "media/dump/" + uuid_name)
            if not os.path.exists(tempFolder):
                os.makedirs(tempFolder)
                data = request.data['upload']
                with open(os.path.join(tempFolder, "source.tr"), 'w') as temp_file:
                    for line in data:
                        temp_file.write(line)
        try:
            FNULL = open(os.devnull, 'w')
            out = subprocess.check_output(
                ['java', '-jar', os.path.join(
                    settings.BASE_DIR, 'aoh/aoh.jar'), '-x', tempFolder + "/source.tr"],
                stderr=subprocess.STDOUT
            )
            os.remove(os.path.join(tempFolder, "source.tr"))
            FNULL.close()

            bookname = ''
            bookcode = ''
            langname = ''
            langcode = ''

            for root, dirs, files in os.walk(tempFolder):
                for f in files:
                    abpath = os.path.join(root, os.path.basename(f))
                    relpath = FileUtility.relative_path(abpath)
                    meta = TinyTag.get(abpath)

                    if meta and meta.artist:
                        a = meta.artist
                        lastindex = a.rfind("}") + 1
                        substr = a[:lastindex]
                        pls = json.loads(substr)

                        if bookcode != pls['slug']:
                            bookcode = pls['slug']
                            bookname = Book.getBookByCode(bookcode)
                        if langcode != pls['language']:
                            langcode = pls['language']
                            langname = Language.getLanguageByCode(langcode)

                        data = {
                            "langname": langname,
                            "bookname": bookname,
                            "duration": meta.duration
                        }

                        # highPassFilter(abpath)
                        saved = Take.prepareDataToSave(
                            pls, relpath, data, True)
                        if "language" in saved and "language" not in response:
                            response["language"] = saved["language"]
                        if "book" in saved and "book" not in response:
                            response["book"] = saved["book"]
                    else:
                        return Response({"error": "bad_wave_file"}, status=400)
            return Response(response, status=200)
        except Exception as e:
            shutil.rmtree(tempFolder)
            return Response({"error": str(e)}, status=400)
    def create(self, request):

        data = request.data

        if "comment" not in data \
                or "object" not in data or "type" not in data:
            return Response({"error": "not_enough_parameters"}, status=status.HTTP_400_BAD_REQUEST)

        comment = data["comment"]
        obj = data["object"]
        obj_type = data["type"]

        try:
            if obj_type == 'chapter':
                q_obj = Chapter.objects.get(pk=obj)
            elif obj_type == 'chunk':
                q_obj = Chunk.objects.get(pk=obj)
            elif obj_type == 'take':
                q_obj = Take.objects.get(pk=obj)
            else:
                raise ValueError("bad_object")

        except Exception as e:
            return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)

        uuid_name = str(time.time()) + str(uuid.uuid4())
        comments_folder = os.path.join(
            settings.BASE_DIR, "media", "dump", "comments")
        comment_location = os.path.join(comments_folder, uuid_name)
        relpath = FileUtility.relative_path(comment_location)

        if not os.path.exists(comments_folder):
            os.makedirs(comments_folder)

        try:
            comment = self.get_blob_from_base64(comment)
            with open(comment_location + '.webm', 'wb') as audio_file:
                audio_file.write(comment)
            if os.path.isfile(comment_location + '.webm'):
                print(comment_location + '.webm')
                print("file exists")
            else:
                print("file doesnt exist?")
            sound = pydub.AudioSegment.from_file(comment_location + '.webm')
            sound.export(comment_location + ".mp3", format='mp3')
            os.remove(comment_location + ".webm")
        except Exception as e:
            print(e)
            if os.path.isfile(comment_location + '.webm'):
                os.remove(comment_location + '.webm')
            return Response({"error": "bad_audio"}, status=status.HTTP_400_BAD_REQUEST)

        c = Comment.objects.create(
            location=relpath + ".mp3",
            content_object=q_obj,
            owner=request.user
        )
        c.save()

        serializer = self.get_serializer(c)
        return Response(serializer.data, status=status.HTTP_200_OK)