~hristoast/mousikofidi

c0046f773214e0ab0a3b6b3c885a25df48edc9d7 — Hristos N. Triantafillou 7 months ago 95561a3
An mp3 might not have tags available, prevent a crash
1 files changed, 39 insertions(+), 37 deletions(-)

M mousikofidi/mousikofidi.py
M mousikofidi/mousikofidi.py => mousikofidi/mousikofidi.py +39 -37
@@ 1,5 1,5 @@
# MousikóFídi
# Copyright (C) 2019,2020  Hristos N. Triantafillou
# Copyright (C) 2019-2021  Hristos N. Triantafillou
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by


@@ 27,8 27,8 @@ from flask import (
    abort,
    flash,
    jsonify,
    render_template,
    redirect,
    render_template,
    request,
    send_from_directory,
    session,


@@ 203,42 203,44 @@ def file_metadata(file_path: str) -> dict:
            data_string = mp3.pprint()
            ft = "mp3"

            # Try to get cover art
            for t in mp3.tags:
                if t.startswith("APIC"):
                    # Is there a better way than all this splitting?!
                    mimetype = (
                        mp3.tags.pprint()
                        .split("\n")[0]
                        .split("=")[1]
                        .split(", ")[1]
                        .split(" ")[1]
                        .strip("(")
                    )
                    filename = (
                        mp3.tags.pprint()
                        .split("\n")[0]
                        .split("=")[1]
                        .split(", ")[1]
                        .split(" ")[0]
                    )
                    try:
                        data_dict.update(
                            {
                                "cover_art": "data:{m};base64,{b}".format(
                                    m=mimetype,
                                    b=codecs.encode(
                                        mp3.tags["APIC:{}".format(filename)].data,
                                        "base64",
                                    ).decode(),
                                ).rstrip("\n")
                            }
            # Try to get cover art, but first make sure this
            # file has .tags that can be iterated over.
            if hasattr(mp3, "tags") and mp3.tags:
                for t in mp3.tags:
                    if t.startswith("APIC"):
                        # Is there a better way than all this splitting?!
                        mimetype = (
                            mp3.tags.pprint()
                            .split("\n")[0]
                            .split("=")[1]
                            .split(", ")[1]
                            .split(" ")[1]
                            .strip("(")
                        )
                    except KeyError:
                        wrn("Could not encode image to base64 for: " + file_path)
                        # There was some error getting the image in base64,
                        # so just forget about it.
                        pass
                        filename = (
                            mp3.tags.pprint()
                            .split("\n")[0]
                            .split("=")[1]
                            .split(", ")[1]
                            .split(" ")[0]
                        )
                        try:
                            data_dict.update(
                                {
                                    "cover_art": "data:{m};base64,{b}".format(
                                        m=mimetype,
                                        b=codecs.encode(
                                            mp3.tags["APIC:{}".format(filename)].data,
                                            "base64",
                                        ).decode(),
                                    ).rstrip("\n")
                                }
                            )
                        except KeyError:
                            wrn("Could not encode image to base64 for: " + file_path)
                            # There was some error getting the image in base64,
                            # so just forget about it.
                            pass

        except MP3HeaderNotFoundError:
            wrn("This file is not a valid mp3 file: " + file_path)