-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Saved Songs Download and User Authentication #1240
Conversation
I believe this would solve #1234 |
Problem is how can I feed the access and refresh tokens to spotipy?
Please keep your commit messages clear, for example the last commit message:
Is that the issue you fixed or the one you need to fix? If it's the latter, just what did you put into the commit then? Even with the title:
It can mean a lot of things. It could be a redesign of the auth process or you implemented spotipy oauth (which is done by default) which leaves the question of why are the access tokens an issue anymore... See point 2 at this subsection P.S. I haven't read the code, I just wanna say that your commit messages should cover (a) why changes were made and (b) how do those changes deal with the issue. What changes are made are indicated by the git diff so you don't have to mention that part. |
It's the latter. I'm sorry for the slip, I'll keep commit messages more focused in future. Over all these commits I've added a flag to spotdl, which is --user-authorization. This triggers a different authorization flow, which is the Authorization Code Flow, which I've implemented up to the point where the access/refresh tokens are first fetched. To keep things easy for the user, I've used pywebview to open the login screen, which is necessary for the Authorization Code Flow. I'm currently having issues integrating my implementation of the OAuth flow and spotipy. |
I've also begun adding support for the "saved" keyword If, when parsing arguments, the word "saved" is found in the place of a URL, spotipy will download the user's Saved Songs. That's if the user also has used the --user-authorization flag. Otherwise it will raise an Exception telling the user to use the flag. |
- Turned get_tokens function into SpotifyUserAuth (custom Spotipy-compatible Auth Manager) - SpotifyUserAuth now automatically refreshes access tokens before they expire - Added function to utils.py: get_saved_tracks, which gets all saved tracks by the current user. Fails without --user-auth - Added "saved" keyword, so that if it appears as a url in the command line, it downloads all saved tracks instead. - Shut off logging by http.server instance
There goes the functionality as far as I'm aware. Now I need to write the tests for literally 300 lines of new code. That's for tomorrow though. I'm guessing it will be kinda hard to write tests with webview though. |
One more note, Its a good practice to keep commits restricted to one-change-a-commit. If you have to rollback a commit and you've grouped multiple changes into a single commit you'll loose a lot of good work.
|
Thanks, I'll keep that in mind |
I'll be the first to admit: I'm new to python, and even after reading through the existing tests, I can't seem to figure out how to test downloading private playlists and the saved songs library. All current tests check out though, so after reverting to the old API keys I'm opening this for review. |
Well I'm stumped. Tests run fine on my machine. |
Sometimes python3.8 checks are failing, you just have to re-run all jobs |
I don't believe the macos 3.8 test should be taking this long... 20 minutes in when other tests finished in 1 |
GitHub actions are not perfect |
I think that pywebview is overkill for this task. We could open the default browser instead with webbrowser that comes included with stdlib |
The reason I'm choosing webview over the browser is that it makes the experience seamless. If the client closes the login window, for example, we can detect that and raise an Exception. In any case, webview could be replaced by a browser if you really think it's best. |
Please don't send the |
Will do |
ah I see #1233 adds it to gitignore |
Co-authored-by: Jakub <42355410+xnetcat@users.noreply.github.com>
Co-authored-by: Jakub <42355410+xnetcat@users.noreply.github.com>
Co-authored-by: Jakub <42355410+xnetcat@users.noreply.github.com>
def get_saved_tracks() -> List[SongObj]: | ||
''' | ||
returns a `list<songObj>` containing Url's of each track in the user's saved tracks | ||
''' | ||
|
||
spotifyClient = SpotifyClient() | ||
|
||
initialRequest = spotifyClient.current_user_saved_tracks() | ||
librarySize = int(initialRequest["total"]) | ||
with ThreadPoolExecutor() as pool: | ||
futures = [] | ||
for i in range(librarySize): | ||
if i % 20 == 0: | ||
futures.append( | ||
pool.submit( | ||
spotifyClient.current_user_saved_tracks, | ||
offset=i | ||
) | ||
) | ||
doneFutures = wait(futures)[0] | ||
doneLists = map(lambda x: x.result()["items"], doneFutures) | ||
songObjFutures = [] | ||
for currList in doneLists: | ||
for songSimplified in currList: | ||
songObjFutures.append( | ||
pool.submit( | ||
SongObj.from_url, | ||
'https://open.spotify.com/track/' + songSimplified['track']['id'] | ||
) | ||
) | ||
doneSongObjsFutures = wait(songObjFutures)[0] | ||
doneSongObjs = list(map(lambda x: x.result(), doneSongObjsFutures)) | ||
return doneSongObjs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we should run this in parallel (we might hit youtube rate limit faster if user has a lot of songs). What do you think @Silverarmor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we run it in parallel, spotDl can no longer be run on termux. Neither will it provide a significant boost over asyncio.
Between parallel and asyncio. Asyncio is better.
/cc @aklajnert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I go back to sequential fetching then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, just like the other functions in utils.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
:D |
Please pull from |
Co-authored-by: Silverarmor <23619946+Silverarmor@users.noreply.github.com>
Co-authored-by: Silverarmor <23619946+Silverarmor@users.noreply.github.com>
Please pull changes from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Silverarmor, this PR is a Go.
@aklajnert Please re-review to verify your requested changes have been made |
Nice! Happy to see this finally come through. |
Publish v3.6.0 * ignore .cache and other hidden files (#1274) * Bump minimal required python version to 3.6.1 (#1278) * Remove FFmpeg normalization causing "quiet" songs. (#1276) * Saved Songs Download and User Authentication (#1240) * regenerate cassettes (#1290) * Use ffmpeg_path to check for version (#1289) * Skip already downloaded songs before doing youtube search (#1287) * Fix security risk (#1285) * Song matching improvements (#1279) * Artist songs fixes (#1284) * More output formats (#1244) * Bump version number to 3.6.0 * Update .gitignore to remove duplicate cache * docs update (#1293) Co-authored-by: Silverarmor <23619946+Silverarmor@users.noreply.github.com> Co-authored-by: Jakub Kot <42355410+xnetcat@users.noreply.github.com> Co-authored-by: Peyton Creery <44987569+phcreery@users.noreply.github.com> Co-authored-by: AZMCode <adrianozambrana@protonmail.com> Co-authored-by: Aiden Gardner <19619206+aiden2480@users.noreply.github.com> Co-authored-by: Oliver Blanthorn <freedom4cows@gmail.com> Co-authored-by: Andrzej Klajnert <github@aklajnert.pl>
What's on the tin.
It does add a few packages, but allows the downloading of private playlists & full library download.
Still not working, but I'm nearly there.