-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_setlist.py
78 lines (60 loc) · 3.41 KB
/
send_setlist.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
72
73
74
75
76
77
78
import datetime
import json
import asyncio
import logging
from sys import stdout
from os import path
from time import sleep
from maj.songlist import Song,SongList
from maj.discordbot import MajBotClient
from maj.twitchrecorder import TwitchRecorder
from maj.utils.spotifyclient import SpotifyClient
from maj.utils.botreplys import get_stream_name_by_day
from bot import logger, config
if __name__ == "__main__":
loop = asyncio.get_event_loop()
spotify_playlist = None
tracks_added = 0
# ensure user has stopped streaming before generating/posting
twitch_recorder = TwitchRecorder(config['botClientID'], config['botSecret'], config['channel'], config['recordedSavePath'])
twitch_recorder.authorize(config['botToken']['oauthToken'], config['botToken']['expirationDate'])
try:
while twitch_recorder.is_user_online():
logger.info('waiting for channel to be offline ...')
sleep(60)
except KeyboardInterrupt:
pass
playlist = SongList(config['recordedSavePath'], config['channel'], datetime.datetime.today())
day_of_week = playlist.setlist_start.weekday()
# save setlist to a spotify playlist
if config.get('spotify') is not None and len(playlist.songs) > 0:
spotify_client = SpotifyClient(config['spotify']['clientID'], config['spotify']['clientSecret'], scopes="playlist-read-collaborative playlist-modify-public playlist-modify-private playlist-read-private")
prefix = f"{get_stream_name_by_day(day_of_week)} Setlist"
descr = f"{playlist.stream_title} ||... Automatically generated by {config['botUsername']}"
logger.info('generating spotify playlist ...')
spotify_playlist, tracks_added = spotify_client.create_setlist_playlist(playlist, name_prefix=prefix, description=descr, is_public=False, is_collab=False, verbose=True)
logger.info(spotify_playlist)
megamixes = spotify_client.get_playlist_ids(f'{get_stream_name_by_day(day_of_week)} Megamix')
if config['spotify'].get('enableMegamix', False) and len(megamixes) > 0:
logger.info('adding to megamix ...')
megamix_id = megamixes[0]
spotify_client.merge_playlists(megamix_id, spotify_playlist['id'])
# post spotify playlist and image of playlist to discord
if config.get('discord') is not None and len(playlist.songs) > 0:
logger.info('generating png ...')
png_filename = 'setlist.png'
playlist.save_setlist_png(output_path=png_filename)
logger.info('generating csv ...')
path_to_csv = playlist.save_setlist_csv()
msg_content = f'{get_stream_name_by_day(day_of_week)} {playlist.setlist_start.strftime("%Y-%m-%d")}'
if spotify_playlist is not None:
msg_content += f": {spotify_playlist['external_urls']['spotify']} \n...Found {tracks_added} of {len(playlist.songs)} songs on Spotify."
discord_bot = MajBotClient(token=config['discord']['botToken'], guildName=config['discord']['guildName'], channelName=config['discord']['channelName'])
logger.info('sending discord message ...')
logger.info(msg_content)
loop.run_until_complete(discord_bot.send_message(msg_content, png_filename))
loop.run_until_complete(discord_bot.send_message("", path_to_csv, path.basename(path_to_csv)))
try:
loop.run_until_complete(discord_bot.close())
except Exception as e:
pass