minor fixes

This commit is contained in:
Cooper Hammond 2019-05-25 11:58:25 -07:00
parent 72d7108d48
commit ffa85fd190
3 changed files with 40 additions and 31 deletions

View file

@ -27,6 +27,7 @@ class SpotifyList(object):
song = Song(s["name"], s["artists"][0]["name"]) song = Song(s["name"], s["artists"][0]["name"])
song.provide_spotify(self.spotify_searcher) song.provide_spotify(self.spotify_searcher)
song.provide_metadata(self.spotify_searcher.song(s["uri"])) song.provide_metadata(self.spotify_searcher.song(s["uri"]))
song.get_relevant_tags()
self.__set_organization(index, song) self.__set_organization(index, song)
song.grab_it() song.grab_it()

View file

@ -32,7 +32,7 @@ class Playlist(SpotifyList):
elif self.organization == "single-folder": elif self.organization == "single-folder":
# reindex the file names in order to keep them in alphabetical order # reindex the file names in order to keep them in alphabetical order
song.provide_new_file_name("{} - {}.mp3".format( 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( song.provide_new_location(os.path.join(
os.getcwd(), self.list_title os.getcwd(), self.list_title

View file

@ -18,6 +18,7 @@ class Song(object):
self.metadata = None self.metadata = None
self.tags = {} self.tags = {}
self.parsed_tags = False
self.file_name = song_title + ".mp3" self.file_name = song_title + ".mp3"
self.end_file_name = None self.end_file_name = None
@ -35,7 +36,7 @@ class Song(object):
self.metadata = self.__parse_data() self.metadata = self.__parse_data()
self.tags = self.__get_relevant_tags(self.tags, self.metadata) self.tags = self.get_relevant_tags()
print("Searching youtube ...") print("Searching youtube ...")
song_url = youtube.find_url(self.tags["title"], self.tags["artist"]) song_url = youtube.find_url(self.tags["title"], self.tags["artist"])
@ -109,6 +110,11 @@ class Song(object):
return self return self
def set_standard_organization(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( self.end_location = os.path.join(
os.environ.get("irs_music_dir"), self.tags["artist"], os.environ.get("irs_music_dir"), self.tags["artist"],
self.tags["album"] self.tags["album"]
@ -117,6 +123,36 @@ class Song(object):
self.tags["tracknumber"], self.tags["title"] 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): def __organize(self):
"""Based off of self.current_location, self.end_location, and self. """Based off of self.current_location, self.end_location, and self.
file_name, this function creates folders for the end location and moves 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, 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): def __parse_data(self):
"""If a spotify searcher has not been provided, create one.""" """If a spotify searcher has not been provided, create one."""
if not self.spotify_authenticated and not self.metadata: if not self.spotify_authenticated and not self.metadata:
@ -180,4 +188,4 @@ class Song(object):
self.song_title, self.artist_name self.song_title, self.artist_name
) )
return self.metadata return self.metadata