From f1436637f8de50de7541d975ae8e66dcf8ee1c70 Mon Sep 17 00:00:00 2001 From: Kepoor Hampond Date: Sun, 5 Mar 2017 16:38:52 -0800 Subject: [PATCH] Fixed bug with playlists where they would all be of the same album. --- irs/__init__.py | 1 - irs/cli.py | 31 +++++++++++++++++++++++++++++++ irs/metadata.py | 6 +++--- irs/ripper.py | 31 +++++++++++++++++-------------- setup.py | 2 +- 5 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 irs/cli.py diff --git a/irs/__init__.py b/irs/__init__.py index 53f18a7..e69de29 100644 --- a/irs/__init__.py +++ b/irs/__init__.py @@ -1 +0,0 @@ -import argparse \ No newline at end of file diff --git a/irs/cli.py b/irs/cli.py new file mode 100644 index 0000000..47e15cf --- /dev/null +++ b/irs/cli.py @@ -0,0 +1,31 @@ +# Arguments +import argparse + +# System +import sys +import os + +# Powered by: +from .ripper import Ripper + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name") + parser.add_argument("-s", "--song", dest="song", help="Specify song name") + + parser.add_argument("-A", "--album", dest="album", help="Specify album name") + + parser.add_argument("-u", "--username", dest="username", help="Specify username") + parser.add_argument("-p", "--playlist", dest="playlist", help="Specify playlist name") + + args = parser.parse_args() + + if args.artist and args.song: + Ripper().song(args.song, args.artist) + elif args.album: + Ripper().spotify_list("album", args.album) + elif args.username and args.playlist: + Ripper().spotify_list("playlist", args.playlist, args.username) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/irs/metadata.py b/irs/metadata.py index 420c4d8..53de713 100644 --- a/irs/metadata.py +++ b/irs/metadata.py @@ -22,7 +22,7 @@ from re import match from bs4 import BeautifulSoup # Local utils -import .utils +from .utils import * # Powered by... import spotipy @@ -55,8 +55,8 @@ class Metadata: def find_album_and_track(song, artist): tracks = spotipy.Spotify().search(q=song, type="track")["tracks"]["items"] for track in tracks: - if utils.blank_include(track["name"], song): - if utils.blank_include(track["artists"][0]["name"], artist): + if blank_include(track["name"], song): + if blank_include(track["artists"][0]["name"], artist): return track["album"], track return False, False diff --git a/irs/ripper.py b/irs/ripper.py index 2179106..3bc9bb0 100644 --- a/irs/ripper.py +++ b/irs/ripper.py @@ -21,7 +21,7 @@ else: sys.exit(1) # Local utilities -import .utils +from .utils import * from .metadata import * class Ripper: @@ -64,15 +64,15 @@ class Ripper: self.code = None for link in results: - if utils.blank_include(link["title"], song) and utils.blank_include(link["title"], artist): - if utils.check_garbage_phrases: continue + if blank_include(link["title"], song) and blank_include(link["title"], artist): + if check_garbage_phrases: continue self.code = link break if self.code == None: for link in results: - if utils.check_garbage_phrases: continue - if utils.individual_word_match(song, link["title"]) >= 0.8 and utils.blank_include(link["title"], artist): + if check_garbage_phrases: continue + if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist): self.code = link break @@ -104,9 +104,9 @@ class Ripper: if len(list_of_lists) > 0: the_list = None for list_ in list_of_lists: - if utils.blank_include(list_["name"], title): + if blank_include(list_["name"], title): if "artist" in self.args: - if utils.blank_include(list_["artists"][0]["name"], self.args["artist"]): + if blank_include(list_["artists"][0]["name"], self.args["artist"]): the_list = list_ break else: @@ -137,14 +137,17 @@ class Ripper: elif type == "album": file_prefix = str(track["track_number"]) + " - " + #print (track) + album = self.spotify.album(track["album"]["uri"]) + data = { "name": track["name"], "artist": track["artists"][0]["name"], - "album": the_list["name"], + "album": album["name"], "genre": parse_genre(self.spotify.artist(track["artists"][0]["uri"])["genres"]), "track_number": track["track_number"], "disc_number": track["disc_number"], - "album_art": the_list["images"][0]["url"], + "album_art": album["images"][0]["url"], "compilation": compilation, "file_prefix": file_prefix, } @@ -161,13 +164,13 @@ class Ripper: def list(self, list_data): locations = [] #with open(".irs-download-log", "w+") as file: - # file.write(utils.format_download_log_data(list_data)) + # file.write(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") + #update_download_log_line_status(track, "downloaded") locations.append(loc) #os.remove(".irs-download-log") @@ -212,7 +215,7 @@ class Ripper: print ('Downloading "%s" by "%s" ...' % (song, artist)) - file_name = data["file_prefix"] + utils.blank(song, False) + ".mp3" + file_name = data["file_prefix"] + blank(song, False) + ".mp3" ydl_opts = { 'format': 'bestaudio/best', @@ -222,8 +225,8 @@ class Ripper: 'preferredcodec': 'mp3', 'preferredquality': '192', }], - 'logger': utils.MyLogger(), - 'progress_hooks': [utils.my_hook], + 'logger': MyLogger(), + 'progress_hooks': [my_hook], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: diff --git a/setup.py b/setup.py index 5a295b0..cebe64d 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,6 @@ setup( 'spotipy', ], entry_points = { - 'console_scripts': ['irs = irs.__main__:main'], + 'console_scripts': ['irs = irs.cli:main'], }, )