Created
May 12, 2022 09:38
-
-
Save ryan-mooore/9e8c3a9746e4ad7b5813022693b7eadb to your computer and use it in GitHub Desktop.
Program that converts a notion database to spotify playlist
This file contains 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
import spotipy | |
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth | |
from os import environ | |
from pprint import pprint | |
from notion_client import Client | |
notion = Client(auth=environ["NOTION_TOKEN"]) | |
spotify = spotipy.Spotify( | |
client_credentials_manager=SpotifyClientCredentials(), | |
auth_manager=SpotifyOAuth( | |
scope="playlist-modify-public", redirect_uri="http://localhost:8080" | |
), | |
) | |
track_ids = [] | |
for result in notion.databases.query( | |
environ["NOTION_DB"], sorts=[{"property": "Date", "direction": "ascending"}] | |
)["results"]: | |
try: | |
name = result["properties"]["Name"]["title"][0]["plain_text"] | |
artist = result["properties"]["Artist"]["rich_text"][0]["plain_text"] | |
except IndexError: | |
continue | |
try: | |
result = spotify.search(f"track:{name}+artist:{artist}", limit=1) | |
print( | |
result["tracks"]["items"][0]["name"], | |
"-", | |
result["tracks"]["items"][0]["artists"][0]["name"], | |
) | |
track_ids.append(result["tracks"]["items"][0]["id"]) | |
except IndexError: | |
result = spotify.search(f"{name} - {artist}", limit=1) | |
print( | |
result["tracks"]["items"][0]["name"], | |
"-", | |
result["tracks"]["items"][0]["artists"][0]["name"], | |
) | |
track_ids.append(result["tracks"]["items"][0]["id"]) | |
spotify.playlist_add_items(environ["SPOTIFY_PLAYLIST"], track_ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment