-
Notifications
You must be signed in to change notification settings - Fork 21
spotify playlist created #98
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
SPOTIPY_CLIENT_ID="xxxx" | ||
SPOTIPY_CLIENT_SECRET="xxxx" | ||
SPOTIFY_REDIRECT_URI="xxxx" | ||
SPOTIFY_USERNAME="xxxx" | ||
SPOTIFY_PLAYLIST_ID="xxxx" | ||
GOOGLE_APPLICATION_CREDENTIALS="xxxx" | ||
WHOAMI="xxxx" | ||
POSTGRES_URI="xxxx" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from c3po.db.base import session_scope | ||
from c3po.db.models.user import UserPosts | ||
from c3po.db.models.link import Link | ||
from spotify_uri import get_spotify_id | ||
|
||
# get first 50 songs from the db | ||
def get_song_url(): | ||
with session_scope() as session: | ||
songs = [] | ||
for u,l in session.query(UserPosts,Link).filter(UserPosts.link_id==Link.id).limit(50).all(): | ||
if len(songs)<=50: | ||
if get_spotify_id(l.url) is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be changed to simply |
||
songs.append(get_spotify_id(l.url)) | ||
else: | ||
continue | ||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is code is redundant |
||
else: | ||
break | ||
return songs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
import spotipy | ||
from spotipy.oauth2 import SpotifyOAuth | ||
from fetch_songs import get_song_url | ||
from music_metadata_extractor import SongData | ||
|
||
scope="playlist-modify-public" | ||
username=os.getenv("SPOTIFY_USERNAME") | ||
token = SpotifyOAuth(scope=scope,username=username) | ||
spotifyObject = spotipy.Spotify(auth_manager=token) | ||
|
||
# fetch spotify uri | ||
list_of_songs = get_song_url() | ||
|
||
# fetch latest created playlist id | ||
playlist_id = os.getenv("SPOTIFY_PLAYLIST_ID") | ||
|
||
# add songs to playlist | ||
spotifyObject.user_playlist_replace_tracks(user=username,playlist_id=playlist_id,tracks=list_of_songs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import re | ||
from music_metadata_extractor import SongData | ||
|
||
def get_spotify_id(url): | ||
spotify_pattern = re.compile( | ||
r""" | ||
https?:\/\/open\.spotify\.com\/track\/(?P<id>[a-zA-Z0-9]+) | ||
""", | ||
re.VERBOSE | ||
) | ||
spotify_match = spotify_pattern.match(url) | ||
if spotify_match: | ||
return spotify_match.group("id") | ||
else: | ||
yt_pattern = re.compile( | ||
r""" | ||
(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\. | ||
(com|be)/(watch\?v=|embed/|v/|.+\?v=)? | ||
(?P<id>[A-Za-z0-9\-=_]{11})""", | ||
re.VERBOSE, | ||
) | ||
yt_match = yt_pattern.match(url) | ||
if(yt_match): | ||
data = SongData(url) | ||
if data.track: | ||
return data.track.provider_id |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You are not using the
u
variable anywhere. It would be better if you queried the Songs table (which has entries that definitely have corresponding Spotify IDs). Do a join with the Links table and then get the URLs from there. This would ensure you get 50 songs in a single go.