From cfdec19136367b4d315f85d698ad9dbcb65b5691 Mon Sep 17 00:00:00 2001 From: Kepoor Hampond Date: Sat, 4 Mar 2017 16:50:22 -0800 Subject: [PATCH] Added more framework for downloading in python. It will happen. Soon. --- irs/ripper.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++----- irs/utils.py | 24 ++++++++++++- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/irs/ripper.py b/irs/ripper.py index 332d3bf..371c1d8 100644 --- a/irs/ripper.py +++ b/irs/ripper.py @@ -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"]) - \ No newline at end of file + + 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 \ No newline at end of file diff --git a/irs/utils.py b/irs/utils.py index 8799721..6063b43 100644 --- a/irs/utils.py +++ b/irs/utils.py @@ -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)) \ No newline at end of file + 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) + \ No newline at end of file