Laid down code for downloading of Spotify playlists. Nothing has actually changed yet.

This commit is contained in:
Kepoor Hampond 2017-01-23 21:38:27 -08:00
parent afac16a0bf
commit f57b86e717
5 changed files with 111 additions and 12 deletions

2
.gitignore vendored
View file

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

View file

@ -93,8 +93,12 @@ class Manager:
if given_up_score >= 10:
in_title = True
try:
audio_url = ("http://www.youtube.com/watch?v=" + search_results[i])
except Exception:
print (bc.FAIL + "Could not find song." + bc.ENDC)
exit(1)
audio_url = ("http://www.youtube.com/watch?v=" + search_results[i])
title = strip_special_chars((BeautifulSoup(urlopen(audio_url), 'html.parser')).title.string.lower())
song_title = song.lower().split("/")
@ -193,21 +197,13 @@ class Manager:
results = spotify.search(q=search, type='album')
items = results['albums']['items']
if len(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 = choose_from_list(items)
album_id = (album['uri'])
contents = spotify.album_tracks(album_id)["items"]
contents = contents[0:-1]
names = []
for song in contents:
song = song["name"]
song = song.split(" - ")[0]
names.append(song)
return names, album_id
else:
@ -227,6 +223,16 @@ class Manager:
album = items[0]['images'][0]['url']
return album
def rip_spotify_list(self, search, type, id=None):
spotify = spotipy.Spotify()
results = spotify.search(q=search, type=type)
items = results[type + "s"]['items']
if len(items) > 0:
spotify_list = choose_from_spotify_list(items)
else:
print (bc.FAIL + "No results were found." + bc.ENDC)
def rip_album(self):
search = self.args.artist + " " + self.args.album

View file

@ -110,3 +110,20 @@ def finish_unorganize(file_name):
os.rename(file_name, folder_name + "/" + file_name)
os.rename(folder_name, folder_name.replace("playlist - ", ""))
def choose_from_spotify_list(thelist):
thelist = list(thelist)
print ("Results:")
choice = ""
while choice not in tuple(range(0, len(thelist[:5]))):
for index, album in enumerate(thelist[:5]):
info = spotify.user(album["owner"]["id"])
try:
display_info = " (" + str(info["followers"]["total"]) + " followers)" + " - " + info["display_name"]
except Exception:
display_info = " - info couldn't be found"
print ("\t" + str(index) + ") " + album["name"] + display_info)
choice = int(input(bc.YELLOW + "\nEnter result number: " + bc.ENDC))
return thelist[choice]

View file

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name='irs',
version='2.7.19',
version='2.7.20',
description='A music downloader that just gets metadata.',
url='https://github.com/kepoorhampond/irs',
author='Kepoor Hampond',

76
test1.py Normal file
View file

@ -0,0 +1,76 @@
class bc:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[32m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
GRAY = '\033[30m'
YELLOW = '\033[33m'
def choose_from_spotify_list(thelist):
thelist = list(thelist)
print ("Results:")
choice = ""
while choice not in tuple(range(0, len(thelist[:5]))):
for index, result in enumerate(thelist[:5]):
type = result["type"]
if type == "playlist":
info = spotify.user(result["owner"]["id"])
try:
display_info = " (" + str(info["followers"]["total"]) + " followers)"
display_info += " - " + info["display_name"]
except Exception:
display_info = " - info couldn't be found"
elif type == "album":
info = spotify.album(result["id"])
display_info = " - " + info["artists"][0]["name"]
print ("\t" + str(index) + ") " + result["name"] + display_info)
choice = int(input(bc.YELLOW + "\nEnter result number: " + bc.ENDC))
return thelist[choice]
def rip_spotify_list(search, type, id=None):
spotify = spotipy.Spotify()
results = spotify.search(q=search, type=type)
items = results[type + "s"]['items']
songs = []
if len(items) > 0:
spotify_list = choose_from_spotify_list(items)
list_type = spotify_list["type"]
if list_type != "playlist":
spotify_list = eval("spotify.%s" % list_type)(spotify_list["uri"])
else:
try:
spotify_list = spotify.user_playlist(spotify_list["owner"]["id"], \
playlist_id=spotify_list["uri"], fields="tracks,next")
except spotipy.client.SpotifyException:
print (bc.FAIL + "To download Spotify playlists, you need to supply client_id's" + bc.ENDC)
exit(1)
print (bc.YELLOW + "\nFetching tracks ..." + bc.ENDC, end="\r")
for song in spotify_list["tracks"]["items"]:
artist = spotify.artist(song["artists"][0]["id"])
songs.append([song["name"], artist["name"]])
print (bc.OKGREEN + "Found tracks!" + bc.ENDC)
return songs
else:
print (bc.FAIL + "No results were found." + bc.ENDC)
exit(1)
import spotipy
spotify = spotipy.Spotify()
#results = spotify.search(q="Star Wars Headspace", type="album")
#results = results["albums"]["items"]
print (rip_spotify_list("Brain Food", "playlist"))