Added more framework for downloading in python. It will happen. Soon.

This commit is contained in:
Kepoor Hampond 2017-03-04 16:50:22 -08:00
parent 846b2b9cb2
commit cfdec19136
2 changed files with 111 additions and 9 deletions

View file

@ -28,18 +28,18 @@ class Ripper:
self.locations = []
try:
client_credentials_manager = SpotifyClientCredentials(os.environ["SPOTIFY_CLIENT_ID"], os.environ["SPOTIFY_CLIENT_SECRET"])
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
self.authorized = True
except spotipy.oauth2.SpotifyOauthError:
spotify = spotipy.Spotify()
self.spotify = spotipy.Spotify()
self.authorized = False
def find_yt_url(self, song=None, artist=None, additional_search="lyrics"):
if not song:
song = self.args["song"]
if not artist:
artist = self.args["artist"]
try:
if not song: song = self.args["song"]
if not artist: artist = self.args["artist"]
except ValueError:
raise ValueError("Must have specify `song` and/or `artist` in either init with `args` or with method arguments.")
search_terms = song + " " + artist + " " + additional_search
query_string = urlencode({"search_query" : (search_terms)})
@ -81,4 +81,84 @@ class Ripper:
self.code = results[0]
return ("https://youtube.com" + self.code["href"], self.code["title"])
def spotify_list(self, type=None, title=None, username=None):
try:
if not type: type = self.args["type"]
if not title: title = self.args["title"]
if not username: username = self.args["username"]
except ValueError:
raise ValueError("Must specify type/title/username in `args` with init, or in method arguements.")
if type == "album":
search = title
if "artist" in self.args:
search += " " + self.args["artist"]
list_of_lists = self.spotify.search(q=search, type="album")
elif type == "playlist":
list_of_lists = self.spotify.user_playlists(username)
if len(list_of_lists) > 0:
the_list = None
for list_ in list_of_lists:
if utils.blank_include(list_.name, title):
if "artist" in self.args:
if utils.blank_include(list_["artists"][0]["name"], self.args["artist"]):
the_list = list_
break
else:
the_list = list_
break
if the_list != None:
print ('"%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"]))
compilation = False
if type == "album":
tmp_albums = []
tmp_artists = []
for track in the_list["tracks"]:
tmp_albums.append(track["album"]["name"])
tmp_artists.append(track["artists"][0]["name"])
tmp_albums = list(set(tmp_albums))
tmp_artists = list(set(tmp_artists))
if len(tmp_albums) == 1 and len(tmp_artists) > 1:
compilation = True
tracks = []
file_prefix = ""
for track in the_list["tracks"]:
if type == "playlist":
file_prefix = str(len(tracks)) + " - "
data = {
"name": track["name"],
"artist": track["artists"][0]["name"],
"album": track["album"],
"genre": track["artists"][0]["genres"],
"track_number": track["track_number"],
"disc_number": track["disc_number"],
"compilation": compilation,
"file_prefix": file_prefix,
}
tracks.append(data)
locations = self.list(tracks)
return self.post_processing(locations)
print ('Could not find any lists.')
return False
def list(list_data):
locations = []
with open(".irs-download-log", "w+") as file:
file.write(utils.format_download_log_data(list_data))
for track in list_data:
loc = self.song(track["name"], track["artist"], track)
if loc != False:
utils.update_download_log_line_status(track, "downloaded")
locations.append(loc)
os.remove(".irs-download-log")
return locations

View file

@ -27,4 +27,26 @@ def individual_word_match(match_against, match):
for word in match:
if match_ag == word:
matched.append(word)
return (float(matched.uniq.size) / float(match_against.size))
return (float(matched.uniq.size) / float(match_against.size))
def format_download_log_line(track, download_status="not downloaded"):
return " @@ ".join([t["name"], t["artist"], t["album"]["id"], \
t["genre"], t["track_number"], t["disc_number"], t["compilation"], \
t["prefix"], download_status]) + "\n"
def format_download_log_data(data):
line = []
for track in data:
lines.append(format_download_log_line(track))
return lines
def update_download_log_line_status(track, status="downloaded"):
line_to_find = format_download_log_line(track)
with open(".irs-download-log", "r") as input_file, \
open(".irs-download-log", "w") as output_file:
for line in input_file:
if line == line_to_find:
output_file.write(format_download_log_line(track, status))
else:
output_file.write(line)