mirror of
https://github.com/cooperhammond/irs.git
synced 2025-07-22 20:08:29 +00:00
Compilations metadata works as well as album downloading.
This commit is contained in:
parent
3ccc7d50e9
commit
e24246d99b
|
@ -58,12 +58,15 @@ def find_album_and_track(song, artist):
|
||||||
if utils.blank_include(track["name"], song):
|
if utils.blank_include(track["name"], song):
|
||||||
if utils.blank_include(track["artists"][0]["name"], artist):
|
if utils.blank_include(track["artists"][0]["name"], artist):
|
||||||
return track["album"], track
|
return track["album"], track
|
||||||
|
return False, False
|
||||||
|
|
||||||
def parse_genre(genres):
|
def parse_genre(genres):
|
||||||
if genres != []:
|
if genres != []:
|
||||||
genres.reverse()
|
genres.reverse()
|
||||||
genres = list(map(lambda x: x.replace("-", " "), genres))
|
genres = list(map(lambda x: x.replace("-", " "), genres))
|
||||||
genres.sort(key=lambda x: len(x.split()))
|
genres.sort(key=lambda x: len(x.split()))
|
||||||
return genres[0]
|
return genres[0].title()
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
#Metadata("Da Frame 2R.mp3").add_album_art("https://i.scdn.co/image/c8931dc7eb717531de93ae0d4626362875c51ba9")
|
128
irs/ripper.py
128
irs/ripper.py
|
@ -88,8 +88,9 @@ class Ripper:
|
||||||
try:
|
try:
|
||||||
if not type: type = self.args["type"]
|
if not type: type = self.args["type"]
|
||||||
if not title: title = self.args["list_title"]
|
if not title: title = self.args["list_title"]
|
||||||
if not username: username = self.args["username"]
|
if not username and type == "playlist":
|
||||||
except ValueError:
|
username = self.args["username"]
|
||||||
|
except KeyError:
|
||||||
raise ValueError("Must specify type/title/username in `args` with init, or in method arguments.")
|
raise ValueError("Must specify type/title/username in `args` with init, or in method arguments.")
|
||||||
|
|
||||||
if type == "album":
|
if type == "album":
|
||||||
|
@ -103,43 +104,45 @@ class Ripper:
|
||||||
if len(list_of_lists) > 0:
|
if len(list_of_lists) > 0:
|
||||||
the_list = None
|
the_list = None
|
||||||
for list_ in list_of_lists:
|
for list_ in list_of_lists:
|
||||||
if utils.blank_include(list_.name, title):
|
if utils.blank_include(list_["name"], title):
|
||||||
if "artist" in self.args:
|
if "artist" in self.args:
|
||||||
if utils.blank_include(list_["artists"][0]["name"], self.args["artist"]):
|
if utils.blank_include(list_["artists"][0]["name"], self.args["artist"]):
|
||||||
the_list = list_
|
the_list = list_
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
the_list = list_
|
if type == "album":
|
||||||
|
the_list = self.spotify.album(list_["uri"])
|
||||||
|
else:
|
||||||
|
the_list = self.spotify.playlist(list_["uri"])
|
||||||
break
|
break
|
||||||
if the_list != None:
|
if the_list != None:
|
||||||
print ('"%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
print ('"%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
|
||||||
compilation = ""
|
compilation = ""
|
||||||
if type == "album":
|
if type == "album":
|
||||||
tmp_albums = []
|
|
||||||
tmp_artists = []
|
tmp_artists = []
|
||||||
for track in the_list["tracks"]:
|
for track in the_list["tracks"]["items"]:
|
||||||
tmp_albums.append(track["album"]["name"])
|
|
||||||
tmp_artists.append(track["artists"][0]["name"])
|
tmp_artists.append(track["artists"][0]["name"])
|
||||||
tmp_albums = list(set(tmp_albums))
|
|
||||||
tmp_artists = list(set(tmp_artists))
|
tmp_artists = list(set(tmp_artists))
|
||||||
if len(tmp_albums) == 1 and len(tmp_artists) > 1:
|
if len(tmp_artists) > 1:
|
||||||
compilation = "true"
|
compilation = "1"
|
||||||
|
|
||||||
tracks = []
|
tracks = []
|
||||||
file_prefix = ""
|
file_prefix = ""
|
||||||
for track in the_list["tracks"]:
|
|
||||||
|
for track in the_list["tracks"]["items"]:
|
||||||
if type == "playlist":
|
if type == "playlist":
|
||||||
file_prefix = str(len(tracks)) + " - "
|
file_prefix = str(len(tracks) + 1) + " - "
|
||||||
elif type == "album":
|
elif type == "album":
|
||||||
file_prefix = str(track["track_number"]) + " - "
|
file_prefix = str(track["track_number"]) + " - "
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"name": track["name"],
|
"name": track["name"],
|
||||||
"artist": track["artists"][0]["name"],
|
"artist": track["artists"][0]["name"],
|
||||||
"album": track["album"],
|
"album": the_list["name"],
|
||||||
"genre": self.spotify.artist(track["artists"][0]["uri"])["genres"],
|
"genre": parse_genre(self.spotify.artist(track["artists"][0]["uri"])["genres"]),
|
||||||
"track_number": track["track_number"],
|
"track_number": track["track_number"],
|
||||||
"disc_number": track["disc_number"],
|
"disc_number": track["disc_number"],
|
||||||
|
"album_art": the_list["images"][0]["url"],
|
||||||
"compilation": compilation,
|
"compilation": compilation,
|
||||||
"file_prefix": file_prefix,
|
"file_prefix": file_prefix,
|
||||||
}
|
}
|
||||||
|
@ -147,44 +150,67 @@ class Ripper:
|
||||||
tracks.append(data)
|
tracks.append(data)
|
||||||
|
|
||||||
locations = self.list(tracks)
|
locations = self.list(tracks)
|
||||||
return self.post_processing(locations)
|
return locations
|
||||||
|
#return self.post_processing(locations)
|
||||||
|
|
||||||
print ('Could not find any lists.')
|
print ('Could not find any lists.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def list(self, list_data):
|
def list(self, list_data):
|
||||||
locations = []
|
locations = []
|
||||||
with open(".irs-download-log", "w+") as file:
|
#with open(".irs-download-log", "w+") as file:
|
||||||
file.write(utils.format_download_log_data(list_data))
|
# file.write(utils.format_download_log_data(list_data))
|
||||||
|
|
||||||
for track in list_data:
|
for track in list_data:
|
||||||
loc = self.song(track["name"], track["artist"], track)
|
loc = self.song(track["name"], track["artist"], track)
|
||||||
|
|
||||||
if loc != False:
|
if loc != False:
|
||||||
utils.update_download_log_line_status(track, "downloaded")
|
#utils.update_download_log_line_status(track, "downloaded")
|
||||||
locations.append(loc)
|
locations.append(loc)
|
||||||
|
|
||||||
os.remove(".irs-download-log")
|
#os.remove(".irs-download-log")
|
||||||
return locations
|
return locations
|
||||||
|
|
||||||
def song(self, song=None, artist=None, data={}):
|
def parse_song_data(self, song, artist):
|
||||||
|
album, track = find_album_and_track(song, artist)
|
||||||
|
if album == False:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
album = self.spotify.album(album["uri"])
|
||||||
|
track = self.spotify.track(track["uri"])
|
||||||
|
genre = self.spotify.artist(album["artists"][0]["uri"])["genres"]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"name": track["name"],
|
||||||
|
"artist": track["artists"][0]["name"],
|
||||||
|
"album": album["name"],
|
||||||
|
"album_art": album["images"][0]["url"],
|
||||||
|
"genre": parse_genre(genre),
|
||||||
|
"track_number": track["track_number"],
|
||||||
|
"disc_number": track["disc_number"],
|
||||||
|
|
||||||
|
"compilation": "", # If this method is being called, it's not a compilation
|
||||||
|
"file_prefix": "" # And therefore, won't have a prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
def song(self, song, artist, data={}): # Takes data from `parse_song_data`
|
||||||
try:
|
try:
|
||||||
if not song: song = self.args["song_title"]
|
if not song: song = self.args["song_title"]
|
||||||
if not artist: artist = self.args["artist"]
|
if not artist: artist = self.args["artist"]
|
||||||
except ValueError:
|
except KeyError:
|
||||||
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.")
|
||||||
|
|
||||||
if data == {}: data = False
|
if data == {}:
|
||||||
|
data = self.parse_song_data(song, artist)
|
||||||
|
|
||||||
|
if "file_prefix" not in data:
|
||||||
|
data["file_prefix"] = ""
|
||||||
|
|
||||||
video_url, video_title = self.find_yt_url(song, artist)
|
video_url, video_title = self.find_yt_url(song, artist)
|
||||||
|
|
||||||
print ('Downloading "%s" by "%s"' % (song, artist))
|
print ('Downloading "%s" by "%s" ...' % (song, artist))
|
||||||
|
|
||||||
file_prefix = ""
|
file_name = data["file_prefix"] + utils.blank(song, False) + ".mp3"
|
||||||
if data != False:
|
|
||||||
file_prefix = data["file_prefix"]
|
|
||||||
|
|
||||||
file_name = file_prefix + utils.blank(song, False) + ".mp3"
|
|
||||||
|
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
|
@ -204,45 +230,25 @@ class Ripper:
|
||||||
for file in glob.glob("./*%s*" % video_title.split("/watch?v=")[-1]):
|
for file in glob.glob("./*%s*" % video_title.split("/watch?v=")[-1]):
|
||||||
os.rename(file, file_name)
|
os.rename(file, file_name)
|
||||||
|
|
||||||
if data == False:
|
|
||||||
if "album" not in self.args:
|
|
||||||
album, track = find_album_and_track(song, artist)
|
|
||||||
album = self.spotify.album(album["uri"])
|
|
||||||
track = self.spotify.track(track["uri"])
|
|
||||||
else:
|
|
||||||
album = self.args["album"]
|
|
||||||
if album != None:
|
|
||||||
genre = self.spotify.artist(album["artists"][0]["uri"])["genres"]
|
|
||||||
|
|
||||||
album_name = ""
|
|
||||||
if album:
|
|
||||||
if utils.check_garbage_phrases(["remastered", "explicit"], album["name"], ""):
|
|
||||||
album_name = album["name"].split(" (")[0]
|
|
||||||
else:
|
|
||||||
album_name = album["name"]
|
|
||||||
|
|
||||||
genre = parse_genre(genre)
|
|
||||||
|
|
||||||
# Ease of Variables (copyright) (patent pending) (git yer filthy hands off)
|
# Ease of Variables (copyright) (patent pending) (git yer filthy hands off)
|
||||||
#
|
# [CENSORED BY THE BAD CODE ACT]
|
||||||
# *5 Minutes Later*
|
# *5 Minutes Later*
|
||||||
# Deprecated. It won't be the next big thing. :(
|
# Deprecated. It won't be the next big thing. :(
|
||||||
|
|
||||||
|
|
||||||
m = Metadata(file_name)
|
m = Metadata(file_name)
|
||||||
|
|
||||||
m.add_tag( "title", song)
|
m.add_tag( "title", data["name"])
|
||||||
m.add_tag( "artist", artist)
|
m.add_tag( "artist", data["artist"])
|
||||||
#m.add_tag( "comment", "Downloaded from: %s\n Video Title: %s" % (video_url, video_title))
|
if data != {}:
|
||||||
if album:
|
m.add_tag("album", data["album"])
|
||||||
m.add_tag("album", album_name)
|
m.add_tag("genre", data["genre"])
|
||||||
m.add_tag("genre", genre)
|
m.add_tag("tracknumber", str(data["track_number"]))
|
||||||
m.add_tag("tracknumber", str(track["track_number"]))
|
m.add_tag("discnumber", str(data["disc_number"]))
|
||||||
m.add_tag("discnumber", str(track["disc_number"]))
|
m.add_tag("compilation", data["compilation"])
|
||||||
m.add_album_art( album["images"][0]["url"])
|
m.add_album_art( str(data["album_art"]))
|
||||||
# TODO: GET THIS WORKING:
|
|
||||||
#if "compilation" in data:
|
|
||||||
# m.add_tag("compilation", data["compilation"])
|
|
||||||
|
|
||||||
|
|
||||||
Ripper().song("Stomp", "The Stone Foxes")
|
|
||||||
|
#Ripper().song("Da Frame 2R", "Arctic Monkeys")
|
||||||
|
Ripper().spotify_list("album", "Black Treacle")
|
17
irs/utils.py
17
irs/utils.py
|
@ -15,7 +15,7 @@ class MyLogger(object):
|
||||||
|
|
||||||
def my_hook(d):
|
def my_hook(d):
|
||||||
if d['status'] == 'finished':
|
if d['status'] == 'finished':
|
||||||
print('Done downloading, now converting ...')
|
print ("Converting to mp3 ...")
|
||||||
|
|
||||||
|
|
||||||
#=================================
|
#=================================
|
||||||
|
@ -58,16 +58,16 @@ def individual_word_match(match_against, match):
|
||||||
# Download Log Reading/Updating/Formatting
|
# Download Log Reading/Updating/Formatting
|
||||||
#=========================================
|
#=========================================
|
||||||
|
|
||||||
def format_download_log_line(track, download_status="not downloaded"):
|
def format_download_log_line(t, download_status="not downloaded"):
|
||||||
return " @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
return (" @@ ".join([t["name"], t["artist"], t["album"]["id"], \
|
||||||
t["genre"], t["track_number"], t["disc_number"], str(t["compilation"]), \
|
str(t["genre"]), t["track_number"], t["disc_number"], t["compilation"], \
|
||||||
t["prefix"], download_status]) + "\n"
|
t["file_prefix"], download_status]))
|
||||||
|
|
||||||
def format_download_log_data(data):
|
def format_download_log_data(data):
|
||||||
line = []
|
lines = []
|
||||||
for track in data:
|
for track in data:
|
||||||
lines.append(format_download_log_line(track))
|
lines.append(format_download_log_line(track))
|
||||||
return lines
|
return "\n".join(lines)
|
||||||
|
|
||||||
def read_download_log(spotify):
|
def read_download_log(spotify):
|
||||||
data = []
|
data = []
|
||||||
|
@ -78,7 +78,7 @@ def read_download_log(spotify):
|
||||||
"name": line[0],
|
"name": line[0],
|
||||||
"artist": line[1],
|
"artist": line[1],
|
||||||
"album": spotify.album(line[2]),
|
"album": spotify.album(line[2]),
|
||||||
"genre": line[3],
|
"genre": eval(line[3]),
|
||||||
"track_number": line[4],
|
"track_number": line[4],
|
||||||
"disc_number": line[5],
|
"disc_number": line[5],
|
||||||
"compilation": bool(line[6]),
|
"compilation": bool(line[6]),
|
||||||
|
@ -103,3 +103,4 @@ def update_download_log_line_status(track, status="downloaded"):
|
||||||
|
|
||||||
def ease_of_variables(the_name, data):
|
def ease_of_variables(the_name, data):
|
||||||
eval(the_name + " = " + data) # Forgive me my lord, for I have sinned
|
eval(the_name + " = " + data) # Forgive me my lord, for I have sinned
|
||||||
|
|
Loading…
Reference in a new issue