Merge pull request #18 from kepoorhampond/artist-and-album

Added feature to use the -a flag with -A
This commit is contained in:
Kepoor Hampond 2017-04-11 19:26:28 -07:00 committed by GitHub
commit 04909589cf
4 changed files with 47 additions and 21 deletions

View file

@ -14,7 +14,7 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
# Single Song # Single Song
parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song") parser.add_argument("-a", "--artist", dest="artist", help="Specify artist name. Must be used with -s/--song or -A/--album")
parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist") parser.add_argument("-s", "--song", dest="song", help="Specify song name. Must be used with -a/--artist")
# Album # Album
@ -39,7 +39,6 @@ def main():
print (os.path.dirname(irs.__file__) + "/config.py") print (os.path.dirname(irs.__file__) + "/config.py")
sys.exit() sys.exit()
ripper_args = { ripper_args = {
"post_processors": { "post_processors": {
"custom_directory": args.location, "custom_directory": args.location,
@ -47,12 +46,17 @@ def main():
} }
} }
# Combiner args from argparse and the ripper_args as above and then
# remove all keys with the value of "None"
ripper_args.update(vars(args))
ripper_args = dict((k, v) for k, v in ripper_args.iteritems() if v)
ripper = Ripper(ripper_args) ripper = Ripper(ripper_args)
if args.artist and args.song: if args.artist and args.song:
ripper.song(args.song, args.artist) ripper.song(args.song, args.artist)
elif args.album: elif args.album:
ripper.spotify_list("album", args.album) ripper.spotify_list("album", args.album, artist=args.artist)
elif args.username and args.playlist: elif args.username and args.playlist:
ripper.spotify_list("playlist", args.playlist, args.username) ripper.spotify_list("playlist", args.playlist, args.username)
else: else:

View file

@ -11,6 +11,7 @@ CONFIG = dict(
# You can either specify Spotify keys here, or in environment variables. # You can either specify Spotify keys here, or in environment variables.
additional_search_terms = 'lyrics', additional_search_terms = 'lyrics',
# Search terms for youtube
organize = True, organize = True,
# True always forces organization. # True always forces organization.

View file

@ -79,7 +79,6 @@ class Ripper:
new_loc = new_loc.replace("//", "/") new_loc = new_loc.replace("//", "/")
os.rename(loc, new_loc) os.rename(loc, new_loc)
locations[index] = new_loc locations[index] = new_loc
print (new_loc)
elif self.type == "playlist": elif self.type == "playlist":
for index, loc in enumerate(locations): for index, loc in enumerate(locations):
new_loc = "" new_loc = ""
@ -113,7 +112,7 @@ class Ripper:
link = "http://www.youtube.com/results?" + query_string link = "http://www.youtube.com/results?" + query_string
html_content = urlopen(link).read() html_content = urlopen(link).read()
soup = BeautifulSoup(html_content, 'html.parser')#.prettify soup = BeautifulSoup(html_content, 'html.parser')#.prettify()
def find_link(link): def find_link(link):
try: try:
@ -125,22 +124,30 @@ class Ripper:
results = list(filter(None, (map(find_link, soup.find_all("a"))))) results = list(filter(None, (map(find_link, soup.find_all("a")))))
garbage_phrases = "cover album live clean rare version".split(" ") garbage_phrases = "cover album live clean rare version full full album".split(" ")
self.code = None self.code = None
for link in results: counter = 0
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:
while self.code == None and counter <= 10:
counter += 1
for link in results: for link in results:
if check_garbage_phrases: continue if blank_include(link["title"], song) and blank_include(link["title"], artist):
if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist): if check_garbage_phrases(garbage_phrases, link["title"], song): continue
self.code = link self.code = link
break break
if self.code == None:
for link in results:
if check_garbage_phrases(garbage_phrases, link["title"], song): continue
if individual_word_match(song, link["title"]) >= 0.8 and blank_include(link["title"], artist):
self.code = link
break
if self.code == None:
song = limit_song_name(song)
if self.code == None: if self.code == None:
if additional_search == "lyrics": if additional_search == "lyrics":
return self.find_yt_url(song, artist, "") return self.find_yt_url(song, artist, "")
@ -155,7 +162,7 @@ class Ripper:
def playlist(self, title, username): # Alias for `spotify_list("playlist", ...)` def playlist(self, title, username): # Alias for `spotify_list("playlist", ...)`
return self.spotify_list("playlist", title, username) return self.spotify_list("playlist", title, username)
def spotify_list(self, type=None, title=None, username=None): def spotify_list(self, type=None, title=None, username=None, artist=None):
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"]
@ -179,9 +186,9 @@ class Ripper:
the_list = None the_list = None
for list_ in list_of_lists: for list_ in list_of_lists:
if blank_include(list_["name"], title): if blank_include(list_["name"], title):
if "artist" in self.args: if parse_artist(self):
if blank_include(list_["artists"][0]["name"], self.args["artist"]): if blank_include(list_["artists"][0]["name"], parse_artist(self)):
the_list = list_ the_list = self.spotify.album(list_["uri"])
break break
else: else:
if type == "album": if type == "album":
@ -196,6 +203,7 @@ class Ripper:
compilation = "" compilation = ""
if type == "album": if type == "album":
tmp_artists = [] tmp_artists = []
for track in the_list["tracks"]["items"]: for track in the_list["tracks"]["items"]:
tmp_artists.append(track["artists"][0]["name"]) tmp_artists.append(track["artists"][0]["name"])
tmp_artists = list(set(tmp_artists)) tmp_artists = list(set(tmp_artists))

View file

@ -28,6 +28,15 @@ def my_hook(d):
# Object Manipulation and Checking # Object Manipulation and Checking
#================================= #=================================
def limit_song_name(song):
bad_phrases = "remaster remastered master".split(" ")
# I have "master" here because Spotify actually sometimes mispells stuff
# and it is hella annoying, so this was my solution
for phrase in bad_phrases:
if blank_include(song.split(" - ")[-1], phrase):
return song.split(" - ")[0]
return song
def check_garbage_phrases(phrases, string, title): def check_garbage_phrases(phrases, string, title):
for phrase in phrases: for phrase in phrases:
if phrase in blank(string): if phrase in blank(string):
@ -57,7 +66,7 @@ def individual_word_match(match_against, match):
for word in match: for word in match:
if match_ag == word: if match_ag == word:
matched.append(word) matched.append(word)
return (float(matched.uniq.size) / float(match_against.size)) return (float(len(set(matched))) / float(len(match_against)))
def flatten(l): def flatten(l):
flattened_list = [] flattened_list = []
@ -300,7 +309,7 @@ from .config import CONFIG
def check_sources(ripper, key, default=None, environment=False, where=None): def check_sources(ripper, key, default=None, environment=False, where=None):
tmp_args = ripper.args tmp_args = ripper.args
if where != None and ripper.args.get(where): if where != None and ripper.args.get(where):
tmp_args = ripper.args.get("where") tmp_args = ripper.args.get("where")
if ripper.args.get(key): if ripper.args.get(key):
return ripper.args.get(key) return ripper.args.get(key)
@ -320,6 +329,10 @@ def parse_search_terms(ripper):
search_terms = check_sources(ripper, "additional_search_terms", "lyrics") search_terms = check_sources(ripper, "additional_search_terms", "lyrics")
return search_terms return search_terms
def parse_artist(ripper):
artist = check_sources(ripper, "artist")
return artist
def parse_directory(ripper): def parse_directory(ripper):
directory = check_sources(ripper, "custom_directory", where="post_processors") directory = check_sources(ripper, "custom_directory", where="post_processors")
if directory == None: if directory == None: