Added ability to choose album and get the CORRESPONDING album art

This commit is contained in:
Kepoor Hampond 2017-01-16 13:09:24 -08:00
parent a02424a5ef
commit af1255df5d
5 changed files with 64 additions and 16 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ update_pypi_and_github.py
# Currently in progress # Currently in progress
/flexx-app/ /flexx-app/
/.idea/ /.idea/
*test*

View file

@ -77,7 +77,7 @@ def main():
import pkg_resources import pkg_resources
print ("\n\n" + color("Ingenious Redistribution System", ["HEADER", "BOLD"])) print ("\n\n" + color("Ingenious Redistribution System", ["HEADER", "BOLD"]))
print ("Homepage: " + color("https://github.com/kepoorhampond/irs", ["OKGREEN"])) print ("Homepage: " + color("https://github.com/kepoorhampond/irs", ["OKGREEN"]))
print ("License: " + color("The GNU", ["YELLOW"]) + " (http://www.gnu.org/licenses/gpl.html)") print ("License: " + color("GNU", ["YELLOW"]) + " (http://www.gnu.org/licenses/gpl.html)")
print ("Version: " + pkg_resources.get_distribution("irs").version) print ("Version: " + pkg_resources.get_distribution("irs").version)
print ("\n") print ("\n")
exit(0) exit(0)
@ -90,11 +90,6 @@ def main():
parser.error("error: must supply -A/--album or -s/--song if specifying -a/--artist") parser.error("error: must supply -A/--album or -s/--song if specifying -a/--artist")
exit(1) exit(1)
elif not args.artist and not args.playlist:
manager.console()
elif args.playlist: elif args.playlist:
manager.rip_playlist() manager.rip_playlist()
@ -105,6 +100,8 @@ def main():
elif args.song: elif args.song:
manager.rip_mp3() manager.rip_mp3()
else:
manager.console()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -193,19 +193,32 @@ class Manager:
results = spotify.search(q=search, type='album') results = spotify.search(q=search, type='album')
items = results['albums']['items'] items = results['albums']['items']
if len(items) > 0: if len(items) > 0:
album = items[0] print (bc.HEADER + "Album results:")
choice = ""
while choice not in tuple(range(0, 5)):
for index, album in enumerate(items[:5]):
print (bc.HEADER + "\t" + str(index) + ") " + album["name"])
choice = int(input(bc.YELLOW + "\nEnter album number: " + bc.ENDC))
album = items[choice]
album_id = (album['uri']) album_id = (album['uri'])
contents = spotify.album_tracks(album_id)["items"] contents = spotify.album_tracks(album_id)["items"]
contents = contents[0:-1] contents = contents[0:-1]
names = [] names = []
for song in contents: for song in contents:
names.append(song["name"]) song = song["name"]
return names song = song.split(" - ")[0]
names.append(song)
return names, album_id
def get_album_art(self, artist, album): def get_album_art(self, artist, album, id=None):
spotify = spotipy.Spotify() spotify = spotipy.Spotify()
results = spotify.search(q="album:" + album, type='album') if id:
album = spotify.album(id)
return album["images"][0]["url"]
results = spotify.search(q=artist + " " + album, type='album')
items = results['albums']['items'] items = results['albums']['items']
if len(items) > 0: if len(items) > 0:
album = items[0]['images'][0]['url'] album = items[0]['images'][0]['url']
@ -214,7 +227,7 @@ class Manager:
def rip_album(self): def rip_album(self):
search = self.args.artist + " " + self.args.album search = self.args.artist + " " + self.args.album
songs = self.get_album_contents(search) songs, album_uri = self.get_album_contents(search)
if not songs: if not songs:
print (bc.FAIL + "Could not find album." + bc.ENDC) print (bc.FAIL + "Could not find album." + bc.ENDC)
@ -226,7 +239,7 @@ class Manager:
print (bc.OKBLUE + " - " + song + bc.ENDC) print (bc.OKBLUE + " - " + song + bc.ENDC)
print (bc.YELLOW + "\nFinding album cover ... " + bc.ENDC, end="\r") print (bc.YELLOW + "\nFinding album cover ... " + bc.ENDC, end="\r")
album_art_url = self.get_album_art(self.args.artist, self.args.album) album_art_url = self.get_album_art(self.args.artist, self.args.album, id=album_uri)
print (bc.OKGREEN + "Album cover found: " + bc.ENDC + album_art_url) print (bc.OKGREEN + "Album cover found: " + bc.ENDC + album_art_url)
for track_number, song in enumerate(songs): for track_number, song in enumerate(songs):

View file

@ -84,6 +84,20 @@ class Metadata:
return True return True
def get_album_art(self, artist, album, id=None):
spotify = spotipy.Spotify()
if id:
album = spotify.album(id)
return album["images"][0]["url"]
results = spotify.search(q=artist + " " + album, type='album')
items = results['albums']['items']
if len(items) > 0:
album = items[0]['images'][0]['url']
return album
def add_album_art(self, image_url): def add_album_art(self, image_url):
mp3 = EasyMP3(self.location, ID3=ID3) mp3 = EasyMP3(self.location, ID3=ID3)
@ -93,7 +107,7 @@ class Metadata:
pass pass
if not image_url: if not image_url:
image_url = self.get_albumart_url(album) image_url = self.get_album_art(self.args.artist, self.mp3["album"][0])
mp3.tags.add( mp3.tags.add(
APIC( APIC(

View file

@ -1,4 +1,4 @@
import sys import sys, os
def strip_special_chars(string): def strip_special_chars(string):
special_chars = "\ / : * ? \" < > | - ( )".split(" ") special_chars = "\ / : * ? \" < > | - ( )".split(" ")
@ -87,3 +87,26 @@ def search_google(self, search_terms=""):
headers=hdr)).read(), 'html.parser').findAll(text=True) headers=hdr)).read(), 'html.parser').findAll(text=True)
return list(filter(visible, texts)) return list(filter(visible, texts))
def unorganize(file_name, location, song_number, artist):
locations = location.split("/")
folder_name = ("playlist - " + file_name)[:40]
if not os.path.isdir(folder_name):
os.makedirs(folder_name)
os.rename(location, "%s/%s - %s" % (folder_name, song_number, locations[-1]))
if remove:
import shutil # Only import this if I have to.
shutil.rmtree(locations[0])
def finish_unorganize(file_name):
folder_name = ("playlist - " + file_name)[:40]
os.rename(file_name, folder_name + "/" + file_name)
os.rename(folder_name, folder_name.replace("playlist - ", ""))