~hristoast/mousikofidi

6efab153e03b419f89ce5eddbb27456ab6cb0c97 — Hristos N. Triantafillou 1 year, 6 months ago 00265fa
Don't blow up when an image can't be encoded
1 files changed, 17 insertions(+), 10 deletions(-)

M mousikofidi/mousikofidi.py
M mousikofidi/mousikofidi.py => mousikofidi/mousikofidi.py +17 -10
@@ 219,16 219,23 @@ def file_metadata(file_path: str) -> dict:
                        .split(", ")[1]
                        .split(" ")[0]
                    )
                    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:
                        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)