Skip to content
Open
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"
Empty file added c3po/playlist/__init__.py
Empty file.
Empty file.
48 changes: 48 additions & 0 deletions c3po/playlist/spotify/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import re
from dotenv import load_dotenv, find_dotenv
from music_metadata_extractor import SongData
from spotipy import Spotify
from spotipy.oauth2 import SpotifyOAuth

# env variables: SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIFY_USERNAME, SPOTIFY_PLAYLIST_ID
load_dotenv(find_dotenv())
USERNAME = os.getenv('SPOTIPY_USERNAME')
PLAYLIST_ID = os,getenv('SPOTIFY_PLAYLIST_ID')
SCOPE='playlist-modify-public'


def _get_spotify_client():
return Spotify(auth_manager=SpotifyOAuth(username=USERNAME, scope=SCOPE))

def add_song_to_playlist(spotify_track_id):
spotify_client = _get_spotify_client()
user_id = spotify_client.current_user()['id']
spotify_client.user_playlist_add_tracks(user=user_id, playlist_id=PLAYLIST_ID, tracks=[spotify_track_id])

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