-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
61 lines (48 loc) · 1.72 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import re
import discord
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
def is_spot_url(url):
rgx = re.compile(r'^(spotify:|https://[a-z]+\.spotify\.com/)')
return re.match(rgx, url) is not None
with open('token_discord.txt') as f:
token_discord = f.read()
with open('id_spotify.txt') as f:
client_id = f.read()
with open('secret_spotify.txt') as f:
client_secret = f.read()
with open('redirect_spotify.txt') as f:
redirect_uri = f.read()
with open('username_spotify.txt') as f:
username = f.read()
scope_str = ','.join(['playlist-read-collaborative','playlist-modify-private'])
token = util.prompt_for_user_token(
username=username,
scope=scope_str,
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri)
id_bangerz = '2uzDA17Iy7SQnjcrugRpN4'
spotify = spotipy.Spotify(auth=token)
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'ping':
await message.channel.send('pong')
if is_spot_url(message.content):
rsp = spotify.playlist_add_items(id_bangerz, [message.content])
pl = spotify.playlist(id_bangerz)
tr = spotify.track(message.content)
await message.channel.send('added {} by {} to the {} playlist'.format(
tr['name'],
tr['artists'][0]['name'],
pl['name']
# pl['external_urls']['spotify']
))
client = MyClient()
client.run(token_discord)