From d5630eadda7ec8c845da9353df574b972d0bd05b Mon Sep 17 00:00:00 2001 From: Kepoor Hampond Date: Wed, 15 Mar 2017 18:37:13 -0700 Subject: [PATCH] Implemented config file --- README.md | 2 +- irs/cli.py | 28 +++---- irs/config.py | 34 ++++----- irs/metadata.py | 4 +- irs/ripper.py | 157 +++++++++++++++++++++------------------ irs/utils.py | 97 +++++++++++++++++------- tests/post_processors.py | 4 +- 7 files changed, 187 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index 1d37d49..6e6e573 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Ironic Redistribution System [![License: GNU](https://img.shields.io/badge/license-gnu-yellow.svg?style=flat-square)](http://www.gnu.org/licenses/gpl.html) [![Stars](https://img.shields.io/github/stars/kepoorhampond/irs.svg?style=flat-square)](https://github.com/kepoorhampond/irs/stargazers) -[![Build Status](https://img.shields.io/travis/kepoorhampond/irs/master.svg?style=flat-square)](https://travis-ci.org/karma-runner/karma-coverage) +[![Build Status](https://img.shields.io/travis/kepoorhampond/irs/master.svg?style=flat-square)](https://travis-ci.org/kepoorhampond/irs) [![Say Thanks](https://img.shields.io/badge/say-thanks-ff69b4.svg?style=flat-square)](https://saythanks.io/to/kepoorhampond) [![Coverage Status](http://img.shields.io/coveralls/kepoorhampond/irs.svg?style=flat-square)](https://coveralls.io/github/kepoorhampond/irs?branch=master&style=flat-square) [![PyPI](https://img.shields.io/badge/pypi-irs-blue.svg?style=flat-square)](https://pypi.python.org/pypi/irs) diff --git a/irs/cli.py b/irs/cli.py index efc443c..86b58b7 100644 --- a/irs/cli.py +++ b/irs/cli.py @@ -7,38 +7,38 @@ import os # Powered by: from .ripper import Ripper -from .utils import console, remove_none_values +from .utils import console, parse_default_flags +from .config import CONFIG def main(): parser = argparse.ArgumentParser() - + # Single Song parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song") parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist") - + # Album parser.add_argument("-A", "--album", dest="album", help="Specify album name") - + # Playlist parser.add_argument("-u", "--username", dest="username", help="Specify username. Must be used with -p/--playlist") parser.add_argument("-p", "--playlist", dest="playlist", help="Specify playlist name. Must be used with -u/--username") - + # Post-Processors parser.add_argument("-l", "--location", dest="location", help="Specify a directory to place files in.") parser.add_argument("-o", "--organize", dest="organize", action="store_true", help="Organize downloaded files.") - - - args = parser.parse_args() - + + args = parser.parse_args(parse_default_flags()) + ripper_args = { "post_processors": { - "location": args.location, + "custom_directory": args.location, "organize": args.organize, } - } - + } + ripper = Ripper(ripper_args) - + if args.artist and args.song: ripper.song(args.song, args.artist) elif args.album: @@ -46,7 +46,7 @@ def main(): elif args.username and args.playlist: ripper.spotify_list("playlist", args.playlist, args.username) else: - console(Ripper()) + console(ripper) if __name__ == "__main__": main() \ No newline at end of file diff --git a/irs/config.py b/irs/config.py index 29a4fd3..372170f 100644 --- a/irs/config.py +++ b/irs/config.py @@ -1,29 +1,23 @@ -from os.path import expanduser - CONFIG = dict( - # To autostart rhythmbox with a new song: - # default_flags = '-c rhythmbox %(loc)s', - # To make choosing of links default: - # default_flags = '-l', - # To place all playlist songs into one folder: - # default_flags = '-of', - default_flags = '', + default_flags = ['-o'], + # For default flags. Right now, it organizes your files into an + # artist/album/song structure. + # To add a flag or argument, add an element to the index: + # default_flags = ['-o', '-l', '~/Music'] - # These are necessary to download Spotify playlists - client_id = '', - client_secret = '', + SPOTIFY_CLIENT_ID = '', + SPOTIFY_CLIENT_SECRET = '', + # You can either specify Spotify keys here, or in environment variables. additional_search_terms = 'lyrics', - # For a custom directory. Note that `~` will not work as a shortcut in a - # plain text manner. - directory = str(expanduser("~")) + "/Music", + organize = True, + # True always forces organization. + # False always forces non-organization. + # None allows options and flags to determine if the files will be organized. - # If you want numbered file names - numbered_file_names = True, - - # Downloaded file names - download_file_names = False, + custom_directory = "", + # Defaults to '~/Music' ) diff --git a/irs/metadata.py b/irs/metadata.py index b7336a3..eb49472 100644 --- a/irs/metadata.py +++ b/irs/metadata.py @@ -73,6 +73,4 @@ def parse_genre(genres): genres.sort(key=lambda x: len(x.split())) return genres[0].title() else: - return "" - -#Metadata("Da Frame 2R.mp3").add_album_art("https://i.scdn.co/image/c8931dc7eb717531de93ae0d4626362875c51ba9") \ No newline at end of file + return "" \ No newline at end of file diff --git a/irs/ripper.py b/irs/ripper.py index 61307fb..1df7879 100644 --- a/irs/ripper.py +++ b/irs/ripper.py @@ -19,7 +19,7 @@ elif sys.version_info[0] < 3: else: print ("Must be using Python 2 or 3") sys.exit(1) - + # Local utilities from .utils import * from .metadata import * @@ -30,28 +30,39 @@ class Ripper: self.locations = [] self.type = None try: - client_credentials_manager = SpotifyClientCredentials(os.environ["SPOTIFY_CLIENT_ID"], os.environ["SPOTIFY_CLIENT_SECRET"]) + CLIENT_ID, CLIENT_SECRET = parse_spotify_creds(self) + client_credentials_manager = SpotifyClientCredentials(CLIENT_ID, CLIENT_SECRET) self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) self.authorized = True - except spotipy.oauth2.SpotifyOauthError: + except Exception as e: self.spotify = spotipy.Spotify() self.authorized = False - + def post_processing(self, locations): post_processors = self.args.get("post_processors") if post_processors: - if type(post_processors.get("location")) is str: + if parse_directory(self) != None: for index, loc in enumerate(locations): - new_file_name = post_processors["location"] + "/" + loc + new_file_name = parse_directory(self) + "/" + loc os.rename(loc, new_file_name) locations[index] = new_file_name - if post_processors.get("organize") == True: - if self.type == "album": + # I'd just go on believing that code this terrible doesn't exist. + # You can just close your eyes and scroll by. I'd encourage it. + # It's okay if you need to cry though. + # The rest of the code is here for you. + # It's like loving someone, + # Everyone has some flaws, but you still appreciate and embrace + # those flaws for being exclusive to them. + # And if those flaws are really enough to turn you off of them, then + # you *probably* don't really want to be with them anyways. + # Either way, it's up to you. + if parse_organize(self): + if self.type in ("album", "song"): for index, loc in enumerate(locations): mp3 = Metadata(loc) new_loc = "" - if len(loc.split("/")) > 1: - new_loc = "/".join(loc.split("/")[0:-1]) + if len(loc.split("/")) >= 2: + new_loc = "/".join(loc.split("/")[0:-1]) + "/" file_name = loc.split("/")[-1] else: file_name = loc @@ -66,6 +77,7 @@ class Ripper: new_loc = new_loc.replace("//", "/") os.rename(loc, new_loc) locations[index] = new_loc + print (new_loc) elif self.type == "playlist": for index, loc in enumerate(locations): new_loc = "" @@ -80,79 +92,79 @@ class Ripper: loc = loc.replace("//", "/") new_loc = (new_loc + "/" + file_name).replace("//", "/") os.rename(loc, new_loc) - + return locations - - def find_yt_url(self, song=None, artist=None, additional_search="lyrics"): + + def find_yt_url(self, song=None, artist=None, additional_search=None): + if additional_search == None: + additional_search = parse_search_terms(self) try: if not song: song = self.args["song_title"] if not artist: artist = self.args["artist"] except KeyError: raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.") - - clear_line() - sys.stdout.write("Finding Youtube Link ...\r") - sys.stdout.flush() - + + print ("Finding Youtube Link ...") + search_terms = song + " " + artist + " " + additional_search query_string = urlencode({"search_query" : (search_terms)}) link = "http://www.youtube.com/results?" + query_string - + html_content = urlopen(link).read() soup = BeautifulSoup(html_content, 'html.parser')#.prettify - + def find_link(link): try: if "yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link" in str(" ".join(link["class"])): - if not "&list=" in link["href"]: + if not "&list=" in link["href"]: return link except KeyError: pass - + results = list(filter(None, (map(find_link, soup.find_all("a"))))) - + garbage_phrases = "cover album live clean rare version".split(" ") - + self.code = None for link in results: 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 check_garbage_phrases: continue if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist): self.code = link break - + if self.code == None: if additional_search == "lyrics": return self.find_yt_url(song, artist, "") else: self.code = results[0] - + return ("https://youtube.com" + self.code["href"], self.code["title"]) - + def album(self, title): # Alias for `spotify_list("album", ...)` return self.spotify_list("album", title) - + def playlist(self, title, username): # Alias for `spotify_list("playlist", ...)` return self.spotify_list("playlist", title, username) - + def spotify_list(self, type=None, title=None, username=None): try: if not type: type = self.args["type"] if not title: title = self.args["list_title"] if not username and type == "playlist": - username = self.args["username"] + username = self.args["username"] except KeyError: raise ValueError("Must specify type/title/username in `args` with init, or in method arguments.") - + if not self.type: self.type = type - + if type == "album": search = title if "artist" in self.args: @@ -160,7 +172,7 @@ class Ripper: list_of_lists = self.spotify.search(q=search, type="album")["albums"]["items"] elif type == "playlist": list_of_lists = self.spotify.user_playlists(username)["items"] - + if len(list_of_lists) > 0: the_list = None for list_ in list_of_lists: @@ -178,8 +190,7 @@ class Ripper: break if the_list != None: clear_line() - sys.stdout.write(type.title() + ': "%s" by "%s"\r' % (the_list["name"], the_list["artists"][0]["name"])) - sys.stdout.flush() + print (type.title() + ': "%s" by "%s"' % (the_list["name"], the_list["artists"][0]["name"])) compilation = "" if type == "album": tmp_artists = [] @@ -188,14 +199,14 @@ class Ripper: tmp_artists = list(set(tmp_artists)) if len(tmp_artists) > 1: compilation = "1" - + tracks = [] file_prefix = "" - + for track in the_list["tracks"]["items"]: - if type == "playlist": + if type == "playlist": self.playlist_title = the_list["name"] # For post-processors - + file_prefix = str(len(tracks) + 1) + " - " track = track["track"] album = self.spotify.album(track["album"]["uri"]) @@ -215,13 +226,13 @@ class Ripper: "compilation": compilation, "file_prefix": file_prefix, } - + tracks.append(data) - + locations = self.list(tracks) return locations #return self.post_processing(locations) - + print ('Could not find any lists.') return False @@ -229,27 +240,27 @@ class Ripper: locations = [] #with open(".irs-download-log", "w+") as file: # file.write(format_download_log_data(list_data)) - + for track in list_data: loc = self.song(track["name"], track["artist"], track) if loc != False: #update_download_log_line_status(track, "downloaded") locations.append(loc) - + if self.type in ("album", "playlist"): return self.post_processing(locations) - + #os.remove(".irs-download-log") return locations - + def parse_song_data(self, song, artist): album, track = find_album_and_track(song, artist) - if album == False: + if album == False: return {} - + album = self.spotify.album(album["uri"]) - track = self.spotify.track(track["uri"]) + track = self.spotify.track(track["uri"]) genre = self.spotify.artist(album["artists"][0]["uri"])["genres"] return { @@ -260,37 +271,35 @@ class Ripper: "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` if not self.type: self.type = "song" - + try: if not song: song = self.args["song_title"] if not artist: artist = self.args["artist"] except KeyError: raise ValueError("Must specify song_title/artist in `args` with init, or in method arguments.") - - if data == {}: + + if data == {}: data = self.parse_song_data(song, artist) song = data["name"] artist = data["artist"] - + if "file_prefix" not in data: data["file_prefix"] = "" - + video_url, video_title = self.find_yt_url(song, artist) - - clear_line() - sys.stdout.write('Downloading "%s" by "%s" ...\r' % (song, artist)) - sys.stdout.flush() - + + print ('Downloading "%s" by "%s" ...' % (song, artist)) + file_name = str(data["file_prefix"] + blank(song, False) + ".mp3") - + ydl_opts = { 'format': 'bestaudio/best', #'quiet': True, @@ -304,22 +313,22 @@ class Ripper: 'output': "tmp_file", 'prefer-ffmpeg': True, } - + with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([video_url]) - - + + for file in glob.glob("./*%s*" % video_url.split("/watch?v=")[-1]): os.rename(file, file_name) - + # Ease of Variables (copyright) (patent pending) (git yer filthy hands off) # [CENSORED BY THE BAD CODE ACT] # *5 Minutes Later* # Deprecated. It won't be the next big thing. :( - - - m = Metadata(file_name) - + + + m = Metadata(file_name) + m.add_tag( "title", data["name"]) m.add_tag( "artist", data["artist"]) if data != {}: @@ -329,8 +338,8 @@ class Ripper: m.add_tag("discnumber", str(data["disc_number"])) m.add_tag("compilation", data["compilation"]) m.add_album_art( str(data["album_art"])) - + if self.type == "song": return self.post_processing([file_name]) - + return file_name \ No newline at end of file diff --git a/irs/utils.py b/irs/utils.py index f0a74b1..9035913 100644 --- a/irs/utils.py +++ b/irs/utils.py @@ -21,9 +21,7 @@ class MyLogger(object): def my_hook(d): if d['status'] == 'finished': - clear_line() # TODO: Make this into a method. - sys.stdout.write("Converting to mp3 ...\r") - sys.stdout.flush() + print ("Converting to mp3 ...") #================================= @@ -36,14 +34,14 @@ def check_garbage_phrases(phrases, string, title): if not phrase in blank(title): return True return False - + def blank(string, downcase=True): import re regex = re.compile('[^a-zA-Z0-9\ ]') string = regex.sub('', string) if downcase: string = string.lower() return string - + def blank_include(this, includes_this): this = blank(this) includes_this = blank(includes_this) @@ -77,13 +75,13 @@ def remove_none_values(d): if type(new_d[x]) is list: new_d[x] = remove_none_values(d[x]) elif new_d[x] == None: - del new_d[x] + del new_d[x] return new_d #========================================= # Download Log Reading/Updating/Formatting #========================================= - + def format_download_log_line(t, download_status="not downloaded"): return (" @@ ".join([t["name"], t["artist"], t["album"]["id"], \ str(t["genre"]), t["track_number"], t["disc_number"], t["compilation"], \ @@ -94,7 +92,7 @@ def format_download_log_data(data): for track in data: lines.append(format_download_log_line(track)) return "\n".join(lines) - + def read_download_log(spotify): data = [] with open(".irs-download-log", "r") as file: @@ -111,7 +109,7 @@ def read_download_log(spotify): "file_prefix": line[7], }) return data - + 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, \ @@ -121,8 +119,8 @@ def update_download_log_line_status(track, status="downloaded"): output_file.write(format_download_log_line(track, status)) else: output_file.write(line) - - + + #============================================ # And Now, For Something Completely Different #============================================ @@ -137,39 +135,39 @@ COLS = int(os.popen('tput cols').read().strip("\n")) if sys.version_info[0] == 2: def input(string): return raw_input(string) - + def code(code1): return "\x1b[%sm" % str(code1) - + def no_colors(string): return re.sub("\x1b\[\d+m", "", string) - + def center_colors(string, cols): - return no_colors(string).center(cols).replace(no_colors(string), string) - + return no_colors(string).center(cols).replace(no_colors(string), string) + def decode_utf8(string): if sys.version_info[0] == 3: return string.encode("utf8", "strict").decode() elif sys.version_info[0] == 2: return string.decode("utf8") - + def center_unicode(string, cols): tmp_chars = "X" * len(decode_utf8(string)) chars = center_colors(tmp_chars, cols) return chars.replace(tmp_chars, string) - + def center_lines(string, cols, end="\n"): lines = [] for line in string.split("\n"): lines.append(center_unicode(line, cols)) return end.join(lines) - + def flush_puts(msg, time=0.01): # For slow *burrrp* scroll text, Morty. They-They just love it, Morty. # When they see this text. Just slowwwly extending across the page. Mmm, mmm. # You just give the time for how *buurp* slow you wa-want it, Morty. - # It works with colors and escape characters too, Morty. + # It works with colors and escape characters too, Morty. # Your grandpa's a genius *burrrp* Morty pattern = re.compile("(\x1b\[\d+m)") def check_color(s): @@ -187,7 +185,7 @@ def flush_puts(msg, time=0.01): sys.stdout.write(char) sys.stdout.flush() print ("") - + BOLD = code(1) END = code(0) @@ -225,10 +223,10 @@ def banner(): sleep(0.3) flush_puts(center_colors("{0}Ironic Redistribution System ({1}IRS{2})"\ .format(BYELLOW, BRED, BYELLOW), COLS)) - + flush_puts(center_colors("{0}Made with 😈 by: {1}Kepoor Hampond ({2}kepoorhampond{3})"\ .format(BBLUE, BYELLOW, BRED, BYELLOW) + END, COLS)) - + flush_puts(center_colors("{0}Version: {1}".format(BBLUE, BYELLOW) + pkg_resources.get_distribution("irs").version, COLS)) def menu(unicode, time=0.01): @@ -277,6 +275,55 @@ def console(ripper): except KeyboardInterrupt: print ("") pass - + except KeyboardInterrupt: - sys.exit(0) \ No newline at end of file + sys.exit(0) + +#====================== +# Config File and Flags +#====================== +from .config import CONFIG + +def check_sources(ripper, key, default=None, environment=False, where=None): + if where != None: + tmp_args = ripper.args.get(where) + else: + tmp_args = ripper.args + + if tmp_args.get(key): + return tmp_args.get(key) + elif CONFIG.get(key): + return CONFIG.get(key) + elif os.environ.get(key) and environment == True: + return os.environ.get(key) + else: + return default + +def parse_spotify_creds(ripper): + CLIENT_ID = check_sources(ripper, "SPOTIFY_CLIENT_ID", environment=True) + CLIENT_SECRET = check_sources(ripper, "SPOTIFY_CLIENT_SECRET", environment=True) + return CLIENT_ID, CLIENT_SECRET + +def parse_search_terms(ripper): + search_terms = check_sources(ripper, "additional_search_terms", "lyrics") + return search_terms + +def parse_directory(ripper): + directory = check_sources(ripper, "custom_directory", where="post_processors") + if directory == None: + directory = check_sources(ripper, "custom_directory", "~/Music") + return directory.replace("~", os.path.expanduser("~")) + +def parse_default_flags(default=""): + if CONFIG.get("default_flags"): + args = sys.argv[1:] + CONFIG.get("default_flags") + else: + args = default + return args + +def parse_organize(ripper): + organize = check_sources(ripper, "organize") + if organize == None: + return check_sources(ripper, "organize", False, where="post_processors") + else: + return True \ No newline at end of file diff --git a/tests/post_processors.py b/tests/post_processors.py index 4c1eab6..5aacc7d 100644 --- a/tests/post_processors.py +++ b/tests/post_processors.py @@ -7,14 +7,14 @@ if not os.path.exists("test_dir"): os.makedirs("test_dir") Ripper({ "post_processors": { - "location": "test_dir/", + "custom_directory": "test_dir/", "organize": True, } }).album("Da Frame 2R / Matador") Ripper({ "post_processors": { - "location": "test_dir/", + "custom_directory": "test_dir/", "organize": True, } }).playlist("IRS Testing", "prakkillian")