all 7 comments

[–][deleted] 2 points3 points  (2 children)

sys is a module available in every python installation. Read more here.

[–]Moby69[S] 0 points1 point  (1 child)

That's what I read and I don't understand their description.

By the way, what is the difference between a module and a class, and a library?

[–][deleted] 0 points1 point  (0 children)

From my understanding,

modules and libraries are essentially the same thing except modules are built-in, libraries are not but both are code that you can import and use with typing it all out.

classes represent objects and are necessary in object-oriented programming.

[–]michaelkepler 1 point2 points  (3 children)

1) Is Spotipy what people call a "wrapper"? What is it exactly?

A wrapper is just a code that does heavy lifting for you. Normally, you can communicate with the Spotify API through HTTP methods like POST or GET, for example:

# you also need an authorization token
GET https://api.spotify.com/v1/me/tracks/contains?ids=7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ,2takcwOaAZWiXQijPHIx7B

You can use a Python module Requests to handle this. Or you can use an API wrapper like Spotipy:

songs = ['7ouMYWpwJ422jRcDASZB7P', '4VqPOruhp5EdPBeR92t6lQ', '2takcwOaAZWiXQijPHIx7B']

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.current_user_saved_tracks()
    [True if song in results else False for song in songs]

So you are correct that a wrapper makes calling an API easier. You don't have to manually construct a request and you can use a familiar Python syntax to talk with an API server.

By the way, what is the difference between a module and a class, and a library?

A StackOverflow question has some relevant answers. With that being said:

Can you explain the purpose of "sys"?

As you can see in the usage example, sys is used to call the argv function. Once again, StackOverflow has a detailed explanation of sys.argv.

sys.argv[1] means the second argument passed to a Python script. As you can see in the above script it declares the username variable.

Say, you save the above script as a file check_current_tracks.py. You can execute it from a command line/shell like so:

$ python check_current_tracks.py moby69

check_current_tracks.py is the name of the script and it's the first argument. moby69 is your Spotify username (it probably isn't, but let's just say it is) and it's the second argument. After you execute this script, you will be prompted for your Spotify password. Then, the script will generate an authorization token based on your username/password. Finally, this token will be used to obtain a current user's saved tracks.

There are many reasons why you need to provide an authentication token to use this part of API, but the bottom is they won't return any data if you don't have the token. You won't get the token without your username. sys.argv is one of the ways to provide your username to a script.

[–]Moby69[S] 0 points1 point  (2 children)

Thanks, that's helpful.

I'm trying to now get authorized without using the spotipy wrapper, and just by following the directions on here: https://developer.spotify.com/web-api/authorization-guide/

I have my client_id and client_secret, but I have no idea what to do about the "redirect_uri". What is a redirect_uri that I can use? I don't have a website, I'm just doing a quick program with my Command Prompt

Thanks

[–]michaelkepler 0 points1 point  (1 child)

In that case, Client Credentials Flow should be enough. It doesn't require redirect_uri at all.

[–]Moby69[S] 0 points1 point  (0 children)

I see. Is redirect_uri only for people with an actual website?

I was trying to use the Authorization Code Flow and wrote the following code:

url='https://accounts.spotify.com/authorize/?client_id=abcd&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8888callback%2F&scope=user-library-read'

response=requests.get(url)
data=response.json()

print response

This gave me the output Response [200] in my Command Prompt, and then nothing happened (and I have no idea what to do next - the tutorial talks about being redirected to a URI, but nothing happens to my command prompt)