From 39321603a187473190905febaa936a9ba2f2788c Mon Sep 17 00:00:00 2001 From: Cooper Hammond Date: Sat, 25 May 2019 11:58:25 -0700 Subject: [PATCH] minor fixes --- irs/glue/list.py | 1 + irs/glue/playlist.py | 2 +- irs/glue/song.py | 68 +++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/irs/glue/list.py b/irs/glue/list.py index 09075de..1c730c0 100644 --- a/irs/glue/list.py +++ b/irs/glue/list.py @@ -27,6 +27,7 @@ class SpotifyList(object): song = Song(s["name"], s["artists"][0]["name"]) song.provide_spotify(self.spotify_searcher) song.provide_metadata(self.spotify_searcher.song(s["uri"])) + song.get_relevant_tags() self.__set_organization(index, song) song.grab_it() diff --git a/irs/glue/playlist.py b/irs/glue/playlist.py index a4212e4..1c2695a 100644 --- a/irs/glue/playlist.py +++ b/irs/glue/playlist.py @@ -32,7 +32,7 @@ class Playlist(SpotifyList): elif self.organization == "single-folder": # reindex the file names in order to keep them in alphabetical order song.provide_new_file_name("{} - {}.mp3".format( - song_index, song.tags["title"] + song_index + 1, song.tags["title"] )) song.provide_new_location(os.path.join( os.getcwd(), self.list_title diff --git a/irs/glue/song.py b/irs/glue/song.py index 45c3f8d..77989cf 100644 --- a/irs/glue/song.py +++ b/irs/glue/song.py @@ -18,6 +18,7 @@ class Song(object): self.metadata = None self.tags = {} + self.parsed_tags = False self.file_name = song_title + ".mp3" self.end_file_name = None @@ -35,7 +36,7 @@ class Song(object): self.metadata = self.__parse_data() - self.tags = self.__get_relevant_tags(self.tags, self.metadata) + self.tags = self.get_relevant_tags() print("Searching youtube ...") song_url = youtube.find_url(self.tags["title"], self.tags["artist"]) @@ -109,6 +110,11 @@ class Song(object): return self def set_standard_organization(self): + """Sets standard organization for the file, which is + root-music-dir>artist-folder>album-folder>song + """ + if not self.parsed_tags: + self.tags = self.get_relevant_tags() self.end_location = os.path.join( os.environ.get("irs_music_dir"), self.tags["artist"], self.tags["album"] @@ -117,6 +123,36 @@ class Song(object): self.tags["tracknumber"], self.tags["title"] ) + def get_relevant_tags(self): + """Sorts relevant info from the spotipy metadata. Merges with any + provided tags from provide_tags method. + :rtype: a dict, parsed tags + """ + # TODO: come up with fallback solution if there's no metadata found + # follows this pattern: + # if this does not exist: + # set the thing that doesn't exist to a + # specific value from the metadata dict + tags = self.tags + metadata = self.metadata + + if not tags.get("title"): + tags["title"] = metadata["name"] + if not tags.get("artist"): + tags["artist"] = metadata["artists"][0]["name"] + if not tags.get("album"): + tags["album"] = metadata["album"]["name"] + if not tags.get("tracknumber"): + tags["tracknumber"] = str(metadata["track_number"]) + if not tags.get("albumart"): + tags["albumart"] = metadata["album"]["images"][0]["url"] + if not tags.get("genre") and self.spotify_searcher: + tags["genre"] = string.capwords(self.spotify_searcher.artist( + metadata["artists"][0]["uri"])["genres"][0]) + + self.tags = tags + return self.tags + def __organize(self): """Based off of self.current_location, self.end_location, and self. file_name, this function creates folders for the end location and moves @@ -139,34 +175,6 @@ class Song(object): self.end_location + "/" + self.end_file_name, ) - def __get_relevant_tags(self, tags, metadata): - """Sorts relevant info from the spotipy metadata. Merges with any - provided tags from provide_tags method. - :param tags: any tags that have been already provided - :param metadata: a spotipy dict of info about the song - :rtype: a dict, parsed tags - """ - # TODO: come up with fallback solution if there's no metadata found - # follows this pattern: - # if this does not exist: - # set the thing that doesn't exist to a - # specific value from the metadata dict - if not tags.get("title"): - tags["title"] = metadata["name"] - if not tags.get("artist"): - tags["artist"] = metadata["artists"][0]["name"] - if not tags.get("album"): - tags["album"] = metadata["album"]["name"] - if not tags.get("tracknumber"): - tags["tracknumber"] = str(metadata["track_number"]) - if not tags.get("albumart"): - tags["albumart"] = metadata["album"]["images"][0]["url"] - if not tags.get("genre") and self.spotify_searcher: - tags["genre"] = string.capwords(self.spotify_searcher.artist( - metadata["artists"][0]["uri"])["genres"][0]) - - return tags - def __parse_data(self): """If a spotify searcher has not been provided, create one.""" if not self.spotify_authenticated and not self.metadata: @@ -180,4 +188,4 @@ class Song(object): self.song_title, self.artist_name ) - return self.metadata \ No newline at end of file + return self.metadata