forked from gunyu1019/chzzkpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_playlist_bot.py
71 lines (52 loc) · 1.79 KB
/
spotify_playlist_bot.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
62
63
64
65
66
67
68
69
70
71
# required spotipy
# pip install spotipy
import spotipy
from typing import Any
from pydantic import BaseModel
from chzzkpy.chat import ChatClient, ChatMessage
# Configuration
channel_id = "channel_id"
prefix = "$"
# Naver Authorization
NID_AUT = "NID_AUTHORIZATION_KEY"
NID_SES = "NID_SESSION_KEY"
# Spotify Authorization
client_id = "Spotify App Client ID"
secret = "Spotify App Client Secrect ID"
redirect_uri = "http://localhost:8888/redirect"
scope = "user-modify-playback-state"
oauth_manager = spotipy.SpotifyOAuth(
client_id=client_id, client_secret=secret, redirect_uri=redirect_uri, scope=scope
)
oauth_manager.get_auth_response()
chzzk_client = ChatClient(channel_id)
spotify_client = spotipy.Spotify(oauth_manager=oauth_manager)
class SpotifyMusicInfo(BaseModel):
name: str
artists: str
uri: str
@classmethod
def from_spotify(cls, data: dict[str, Any]):
artists = ", ".join([x["name"] for x in data["artists"]])
return cls(name=data["name"], artists=artists, uri=data["uri"])
@chzzk_client.event
async def on_connect():
print("Ready bot.")
@chzzk_client.event
async def on_chat(message: ChatMessage):
if not message.content.startswith("%s선곡" % prefix):
return
music_name = message.content.split()[1:]
search_result = spotify_client.search(q=music_name, type="track", limit=5)
tracks_result = search_result["tracks"]
items = [SpotifyMusicInfo.from_spotify(x) for x in tracks_result["items"]]
if len(items) <= 0:
await chzzk_client.send_chat("검색 결과가 없습니다 :(")
return
item = items[0]
spotify_client.add_to_queue(item.uri)
await chzzk_client.send_chat(
"%s - %s 노래가 추가되었습니다." % (item.name, item.artists)
)
return
chzzk_client.run(NID_AUT, NID_SES)