Skip to content

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.template
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"
18 changes: 18 additions & 0 deletions spotify_playlist/fetch_songs.py
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():
Copy link
Member

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.

if len(songs)<=50:
if get_spotify_id(l.url) is not None:
Copy link
Member

Choose a reason for hiding this comment

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

This can be changed to simply if get_spotify_id(l.url):

songs.append(get_spotify_id(l.url))
else:
continue
Comment on lines +14 to +15
Copy link
Member

Choose a reason for hiding this comment

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

This is code is redundant

else:
break
return songs
19 changes: 19 additions & 0 deletions spotify_playlist/spotify_playlist.py
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)
26 changes: 26 additions & 0 deletions spotify_playlist/spotify_uri.py
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