Skip to content

Commit

Permalink
Update artist_discography.py (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashsinha848 authored Apr 4, 2021
1 parent 8db120f commit f420247
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions examples/artist_discography.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Shows the list of all songs sung by the artist or the band
import argparse
import logging

Expand Down

13 comments on commit f420247

@greeneryAO
Copy link

@greeneryAO greeneryAO commented on f420247 Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this include songs buried in "appears in" compilations? Does it generate user playlist you can edit or take you to an artist playlist?

as well I get error

spotipy.oauth2.SpotifyOauthError: No client_id. Pass it or set a SPOTIPY_CLIENT_ID environment variable.

when I try to run it, can someone post full code where I can insert my client id and secret to my dev app please, appreciated.

@stephanebruckert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greeneryAO please read the docs at https://spotipy.readthedocs.io/en/2.18.0/#client-credentials-flow

The code doesn't need to be changed.

@greeneryAO
Copy link

@greeneryAO greeneryAO commented on f420247 Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, I understand I have to link it to my client id and secret, and don't need a redirect, but from the readme, if I try to insert

export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'

it says syntax error:invalid syntax

If I insert

SPOTIPY_CLIENT_ID='your-spotify-client-id'
SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'

it says spotipy.oauth2.SpotifyOauthError: No client_id. Pass it or set a SPOTIPY_CLIENT_ID environment variable.

how can I set a variable if it won't see it? I don't think the code works.

@stephanebruckert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of

client_credentials_manager = SpotifyClientCredentials()

you can just do

client_credentials_manager = SpotifyClientCredentials(client_id="YOUR_APP_CLIENT_ID", client_secret="YOUR_APP_CLIENT_SECRET")

or

SPOTIPY_CLIENT_ID = 'YOUR_APP_CLIENT_ID'
SPOTIPY_CLIENT_SECRET = 'YOUR_APP_CLIENT_SECRET'
client_credentials_manager = SpotifyClientCredentials(client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET)

This is why in the issue you created I suggested you to start with the example in the readme

@greeneryAO
Copy link

@greeneryAO greeneryAO commented on f420247 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried in various places, it gives errors on spotifyclientcredentials.

I don't have environmental variables set up so maybe that's why, but I don't want to do that because it might compromise my other codes that were custom made to work with a mac shortcut program I use and use Oauth and Oauth2 in code so I never had to set the variable it's all in code.

I just don't know anything about python to comprehend the readme or docs, so it's pointless unless you post a gist with everything in place.

appreciate you trying anyway, I understand it's frustrating talking to people who know nothing of coding.

@stephanebruckert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greeneryAO I've already pointed you towards the example in the README.

This simple example will allow you to authenticate:

https://github.com/plamere/spotipy#without-user-authentication

Just replace YOUR_APP_CLIENT_ID and YOUR_APP_CLIENT_SECRET by your actual credentials.

If it doesn't work, let us know because it's not normal.

Once it works, you can start coding on your task #702

@greeneryAO
Copy link

@greeneryAO greeneryAO commented on f420247 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did.

#!/usr/bin/env python3

import argparse
import logging

from spotipy.oauth2 import SpotifyClientCredentials
import spotipy

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="my id here",
												    client_secret="my secret here"))
														
logger = logging.getLogger('examples.artist_discography')
logging.basicConfig(level='INFO')
def get_args():
	parser = argparse.ArgumentParser(description='Shows albums and tracks for '
									'given artist')
	parser.add_argument('-a', '--artist', required=True,
						help='Name of Artist')
	return parser.parse_args()
def get_artist(name):
	results = sp.search(q='artist:' + name, type='artist')
	items = results['artists']['items']
	if len(items) > 0:
		return items[0]
	else:
		return None
def show_album_tracks(album):
	tracks = []
	results = sp.album_tracks(album['id'])
	tracks.extend(results['items'])
	while results['next']:
		results = sp.next(results)
		tracks.extend(results['items'])
	for i, track in enumerate(tracks):
		logger.info('%s. %s', i+1, track['name'])
def show_artist_albums(artist):
	albums = []
	results = sp.artist_albums(artist['id'], album_type='album')
	albums.extend(results['items'])
	while results['next']:
		results = sp.next(results)
		albums.extend(results['items'])
	logger.info('Total albums: %s', len(albums))
	unique = set()  # skip duplicate albums
	for album in albums:
		name = album['name'].lower()
		if name not in unique:
			logger.info('ALBUM: %s', name)
			unique.add(name)
			show_album_tracks(album)
def show_artist(artist):
	logger.info('====%s====', artist['name'])
	logger.info('Popularity: %s', artist['popularity'])
	if len(artist['genres']) > 0:
		logger.info('Genres: %s', ','.join(artist['genres']))
def main():
	args = get_args()
	artist = get_artist(args.artist)
	show_artist(artist)
	show_artist_albums(artist)
if __name__ == '__main__':
	client_credentials_manager = SpotifyClientCredentials()
	sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
	main()

spotipy.oauth2.SpotifyOauthError: No client_id. Pass it or set a SPOTIPY_CLIENT_ID environment variable.

I give up.

@stephanebruckert
Copy link
Member

@stephanebruckert stephanebruckert commented on f420247 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the simple example from the README. You should first try

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="YOUR_APP_CLIENT_ID",
                                                           client_secret="YOUR_APP_CLIENT_SECRET"))

results = sp.search(q='weezer', limit=20)
for idx, track in enumerate(results['tracks']['items']):
    print(idx, track['name'])

@greeneryAO
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, well yeah, that works, because it's simple all I have to do is add credentials, but that's not much help to me I'm trying to either get this script or that person from #702 script to work and I don't know where or what to place where, and not to sound like a dick I don't want to learn any coding just would appreciate a gist or py file where I can put my credentials without setting permanent environmental variables that could screw up my existing setup.

@stephanebruckert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well now that you have the main authentication bit working, you can try the other example and replace these 2 lines by what you know works:

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="YOUR_APP_CLIENT_ID",
                                                           client_secret="YOUR_APP_CLIENT_SECRET"))

@stephanebruckert
Copy link
Member

@stephanebruckert stephanebruckert commented on f420247 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to learn any coding just would appreciate a gist or py file where I can put my credentials

But I want you to know that even though we will always help and unblock you on spotipy-related issues here, we're not here to write code for you and efforts on your side are expected

@greeneryAO
Copy link

@greeneryAO greeneryAO commented on f420247 Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok well thanks, if it works it works, if it doesn't it doesn't, I never asked anyone to do my work or write me a code and I wouldn't because that's obnoxious. I only asked if it was possible, he volunteered nicely an already done code but probably expected some rudimentary knowledge level that I had to make it work that I didn't have.

@greeneryAO
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usage: Untitled.py [-h] -a ARTIST
Untitled.py: error: the following arguments are required: -a/--artist

a new error message so that's good, I think it wants me to insert artist ID somewhere.

Please sign in to comment.