def _compile_album_artists(self, files: List[Path]) -> str:
        try:
            artists = [e for f in files for e in utils.Audio(f)["artist"][0].split(", ")]
        except KeyError:
            try:
                artists = [e for f in files for e in utils.Audio(f)["albumartist"][0].split(", ")]
            except KeyError:
                raise ValueError("Could not find artists.")

        if len(set(artists)) != 1:
            return "Various Artists"
        else:
            audio_file = utils.Audio(files[0])
            try:
                return audio_file["albumartist"][0]
            except Exception:
                return audio_file["artist"][0]
 def _filter_general_music(self, files: List[Path]) -> None:
     try:
         genres = [" ".join(utils.Audio(f)["genre"]).upper() for f in files]
         if not any([g in genre for g in ELECTRO_GENRES for genre in genres]):
             export_path = self.export_general_path / files[0].parent.parent.name
             exporter = utils.Exporter(export_path)
             exporter.export(files[0].parent)
             raise ValueError("Album is moved to general music.")
     except KeyError:
         pass
    def _create_request(self, files: List[Path], id3_data: ID3Data) -> str:
        audio_file = utils.Audio(files[0])
        req_album = urllib.parse.quote_plus(id3_data.album)
        if id3_data.albumartist == "Various Artists":
            req_artist = audio_file["artist"][0].split(",")[0]
        else:
            req_artist = urllib.parse.quote_plus(id3_data.albumartist)

        try:
            req_year = audio_file["year"][0].split("-")[0]
        except Exception:
            req_year = ""
        return f"https://www.beatport.com/search?q={req_album}+{req_artist}+{req_year}"
 def _get_id3_from_beatport(self, files: List[Path], id3_data: ID3Data) -> ID3Data:
     req_str = self._create_request(files=files, id3_data=id3_data)
     try:
         ids = requests.get(req_str).text.split('href="/release/')[1:]
         ids = [i.split('"')[0] for i in ids]
         ids = list(set(ids))
         album = id3_data.album.upper()
         albums = [album, utils.replace_all(album)]
         albumartist = id3_data.albumartist.upper()
         if albumartist == "VARIOUS ARTISTS":
             audio_file = utils.Audio(files[0])
             albumartist = audio_file["artist"][0].split(",")[0].upper()
         albumartists = [albumartist, utils.replace_all(albumartist)]
         label = id3_data.label.split()[0].upper()
         labels = [label, utils.replace_all(label)]
         for id in ids:
             r = requests.get("https://www.beatport.com/release/" + id)
             rdata = utils.split_from_to(r.text, ['<script type="application/ld+json">'], "</script>")
             data = json.loads(rdata)
             data = [d for d in data if d["@type"] == "MusicRelease"]
             data = data[0]
             bp_album = data["name"]
             bp_albumartist = [x["name"] for x in data["@producer"]]
             bp_label = data["recordLabel"]["name"].split()[0]
             bp_albums = [bp_album.upper(), utils.replace_all(bp_album.upper())]
             bp_albumartists = [a.upper() for a in bp_albumartist] + [
                 utils.replace_all(a.upper()) for a in bp_albumartist
             ]
             bp_labels = [bp_label.upper(), utils.replace_all(bp_label.upper())]
             conditions = (
                 any(i in bp_albums for i in albums)
                 and any(i in bp_labels for i in labels)
                 and any(i in bp_albumartists for i in albumartists)
             )
             if conditions:
                 isrc_str = data["catalogNumber"]
                 try:
                     isrc = re.split(r"(\d+)", isrc_str)[:3]
                     if isrc[2] in ["DIG", "CD", "DIGITAL"]:
                         isrc[2] = ""
                     id3_data.isrc = "".join(isrc)
                 except Exception:
                     id3_data.isrc = isrc_str
                 id3_data.genre = utils.split_from_to(r.text, ['data-ec-d3="'], '">').replace("&amp;", "&")
                 return id3_data
         raise IndexError
     except Exception:
         id3_data.isrc = "TODO"
         return id3_data
Beispiel #5
0
 def update_labels(self) -> None:
     albums = self.collection.albums()
     for alb in albums:
         if alb.studio is None:
             plex_path = alb.tracks()[0].media[0].parts[0].file
             real_path = plex_path.replace("/music_el", self.base_path)
             try:
                 x = utils.Audio(pathlib.Path(real_path))
             except utils.AudioError as e:
                 logger.error(e)
                 continue
             try:
                 label = x["label"][0]
             except KeyError:
                 try:
                     label = x["publisher"][0]
                 except KeyError:
                     try:
                         label = x["organization"][0]
                     except KeyError:
                         pass
             logger.info(f"Updating label {label} for album {alb.title}.")
             alb.edit(**{"studio.value": label})
from pathlib import Path

from music_library_tools import utils

import_path = Path("/data/export_electro")

artist_dirs = [d for d in import_path.iterdir() if d.is_dir()]
for art_dir in artist_dirs:
    album_dirs = [d for d in art_dir.iterdir() if d.is_dir()]
    for d in album_dirs:
        files = [
            f for f in d.iterdir()
            if (f.suffix == ".flac" or f.suffix == ".mp3")
        ]
        for f in files:
            audio_file = utils.Audio(f)
            audio_file["genre"] = audio_file["genre"][0].replace("/", "-")
            audio_file.save()
 def _check_track_numbers(self, files: List[Path]) -> None:
     track_nos = [int(utils.Audio(f)["tracknumber"][0]) for f in files]
     if not max(track_nos) == len(track_nos):
         raise ValueError("Less audio files than tracks in album.")
 def _finalize_file(self, file: Path, id3_data: ID3Data) -> None:
     audio_file = utils.Audio(file)
     audio_file = self._apply_id3_data(audio_file, id3_data)
     audio_file = self._fix_title(audio_file)
     audio_file.save()