initial version, working with spotify

This commit is contained in:
Matthias Jacob 2022-09-18 16:26:28 +02:00
commit f25a3933ea
4 changed files with 99 additions and 0 deletions

3
config.example.py Normal file
View File

@ -0,0 +1,3 @@
SPOTIFY_PLAYLIST_ID = ''
SPOTIPY_CLIENT_ID = ''
SPOTIPY_CLIENT_SECRET = ''

2
dev_requirements.txt Normal file
View File

@ -0,0 +1,2 @@
jupyterlab
pandas

92
main.py Normal file
View File

@ -0,0 +1,92 @@
import os
import re
import pandas as pd
from urllib.parse import urlparse
import spotipy
#from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint
from config import *
SRC_SPOTIFY = 'spotify'
SRC_YOUTUBE = 'youtube'
SRC_SOUNDCLOUD = 'soundcloud'
SRC_BANDCAMP = 'bandcamp'
SRC_OTHER = 'other'
os.environ['SPOTIPY_CLIENT_ID'] = SPOTIPY_CLIENT_ID
os.environ['SPOTIPY_CLIENT_SECRET'] = SPOTIPY_CLIENT_SECRET
SERVICES = {
'open.spotify.com': SRC_SPOTIFY,
'youtu.be': SRC_YOUTUBE,
'www.youtube.com': SRC_YOUTUBE,
'soundcloud.app.goo.gl': SRC_SOUNDCLOUD,
'on.soundcloud.com': SRC_SOUNDCLOUD,
'm.soundcloud.com': SRC_SOUNDCLOUD,
'soundcloud.com': SRC_SOUNDCLOUD
}
def echo(link):
o = urlparse(link)
#print(o.hostname)
if re.match(r'([A-Za-z0-9\-]*\.)?bandcamp.com', o.hostname):
return {'source': SRC_BANDCAMP, 'link': link}
return {'source': SERVICES.get(o.hostname, SRC_OTHER), 'link': link}
def update_spotify_from_export():
df = pd.read_json("ChatExport_2022-09-18/result.json")
df1 = pd.json_normalize(df.messages)
reduced = df1[df1['type'] == 'message'][['id', 'type', 'text', 'from', 'from_id']]
get_links = pd.json_normalize(reduced.explode('text').text)
links = get_links[get_links['type'] == 'link']['text'].to_list()
links_w_source = [echo(l) for l in links]
pprint(links_w_source)
spotify_links = []
yt_links = []
soundcloud_links = []
bandcamp = []
other_links = []
for i in links_w_source:
if i['source'] == 'spotify':
spotify_links.append(i['link'])
elif i['source'] == 'youtube':
yt_links.append(i['link'])
elif i['source'] == 'soundcloud':
soundcloud_links.append(i['link'])
elif i['source'] == 'bandcamp':
bandcamp.append(i['link'])
else:
other_links.append(i['link'])
print(spotify_links)
print(yt_links)
print(soundcloud_links)
print(bandcamp)
print(other_links)
#birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
#spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
scope = "playlist-modify-private"
os.environ['SPOTIPY_REDIRECT_URI'] = 'https://example.com/callback'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
sp.playlist(SPOTIFY_PLAYLIST_ID)
print(sp.playlist_replace_items(SPOTIFY_PLAYLIST_ID, spotify_links))
if __name__ == '__main__':
update_spotify_from_export()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
#python-telegram-bot
spotipy