From f25a3933ea1a3323541c672aa68ce211bf6c7257 Mon Sep 17 00:00:00 2001 From: Matthias Jacob Date: Sun, 18 Sep 2022 16:26:28 +0200 Subject: [PATCH] initial version, working with spotify --- config.example.py | 3 ++ dev_requirements.txt | 2 + main.py | 92 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 4 files changed, 99 insertions(+) create mode 100644 config.example.py create mode 100644 dev_requirements.txt create mode 100644 main.py create mode 100644 requirements.txt diff --git a/config.example.py b/config.example.py new file mode 100644 index 0000000..6d9b4d8 --- /dev/null +++ b/config.example.py @@ -0,0 +1,3 @@ +SPOTIFY_PLAYLIST_ID = '' +SPOTIPY_CLIENT_ID = '' +SPOTIPY_CLIENT_SECRET = '' \ No newline at end of file diff --git a/dev_requirements.txt b/dev_requirements.txt new file mode 100644 index 0000000..0459143 --- /dev/null +++ b/dev_requirements.txt @@ -0,0 +1,2 @@ +jupyterlab +pandas \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..7e4f7df --- /dev/null +++ b/main.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..658cadf --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +#python-telegram-bot +spotipy \ No newline at end of file