From d50e746d2f5aa333d45f590545cbd58d6b48fd7a Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sun, 18 Aug 2019 17:35:22 +0200 Subject: [PATCH] Spotify playlist hdcon --- Spotify.py | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ Stroopwafel.py | 2 + 2 files changed, 125 insertions(+) create mode 100644 Spotify.py diff --git a/Spotify.py b/Spotify.py new file mode 100644 index 0000000..7398afc --- /dev/null +++ b/Spotify.py @@ -0,0 +1,123 @@ +import discord +from discord.ext import commands +from discord.ext.commands import Bot +import json +import spotipy +import spotipy.util as util +from spotipy.oauth2 import SpotifyClientCredentials +import asyncio +import random +import time +from json.decoder import JSONDecodeError + +class Spotify(commands.Cog): + """HD Con 2019 Playlist features! Alleen te gebruiken in het juiste kanaal""" + def __init__(self, bot): + self.bot = bot + + #voting settings + self.desired_count = 2 # aantal upvotes dat nodig is om een liedje in de lijst te krijgen + self.veto_count = 2 # aantal downvotes dat nodig is om een liedje weg te stemmen + self.thumbs_up = '👌' #"\N{Ok Hand Sign}" + self.thumbs_down = '🙅' #"\N{Face With No Good Gesture}" + self.stemkanaal_naam = 'top2000' # naam van het stemkanaal + + #All the spotipy settings + self.redirect = 'http://localhost' #must match app settings. + self.scope = 'user-read-private user-read-playback-state user-modify-playback-state playlist-modify-public playlist-modify-private' + self.username = self.bot.tokens["spotifyUsername"] + self.secret = self.bot.tokens["spotifySecret"] + self.client = self.bot.tokens["spotifyId"] + self.playlist_id = self.bot.tokens["spotifyPlaylist"] + + #Create spotipy objects for authentication + client_credentials_manager = SpotifyClientCredentials( + client_id=self.client, + client_secret=self.secret) + self.token = util.prompt_for_user_token( + username=self.username, + scope=self.scope, + client_id=self.client, + client_secret=self.secret, + redirect_uri=self.redirect) + self.sp_app = spotipy.Spotify(client_credentials_manager=client_credentials_manager) + self.sp_user = spotipy.Spotify(auth=self.token) + @self.bot.event + @asyncio.coroutine + def on_reaction_add(reaction, user): + # Als kanaal niet top2000 is niet tellen + if not reaction.message.channel.name.startswith(self.stemkanaal_naam): + return + # Bij genoeg stemmen voor + if reaction.emoji == self.thumbs_up and reaction.count >= self.desired_count: + print(str(reaction.message.content)) # debugging + with open("lijst.txt", "a") as f: + f.write(str(reaction.message.content)+'\n') + self.nummer_toevoegen(str(reaction.message.content)) + yield from reaction.message.delete() # verwijder poll + yield from reaction.message.channel.send("Dit nummer heeft genoeg stemmen ontvangen en is op de lijst gezet: " + str(reaction.message.content)) + # Bij genoeg stemmen tegen + elif reaction.emoji == self.thumbs_down and reaction.count >= self.veto_count: + stemmen = reaction.message.reactions[0].count - 1 # tel het aantal voor-stemmen + if stemmen == 1: men = '' # om de grammar nazi's een plezier te doen + else: men = 'men' + yield from reaction.message.channel.send("Helaas, dit nummer heeft het ondanks " + str(stemmen) + " stem" + men + " niet gehaald: " + str(reaction.message.content)) + try: + yield from ctx.message.delete() + except: + pass + + #Properties and helper functions + @property + def playlist(self): + playlist_results = self.sp_user.user_playlist(self.username, self.playlist_id) + playlist_results = playlist_results['tracks']['items'] + return playlist_results + + def nummer_toevoegen(self, nummer): + tracks = ["spotify:track:"+nummer[31:]] # Het moet in een list + self.sp_user.user_playlist_add_tracks(user=self.username, playlist_id=self.playlist_id, tracks=tracks) + + #User-facing commands + @commands.command(pass_context=True, hidden=False) + @asyncio.coroutine + def geefplaylist(self, ctx): + yield from ctx.channel.send(ctx.author.mention + " https://open.spotify.com/playlist/" + self.playlist_id) + try: + yield from ctx.message.delete() + except: + pass + + @commands.command(pass_context=True, hidden=False) + @asyncio.coroutine + def nummers(self, ctx): + yield from ctx.channel.send(str(len(self.playlist))) + + @commands.command(pass_context=True, hidden=False) + @asyncio.coroutine + def stem(self, ctx, *, text=None): + '''Stel een liedje voor dat in de HDcon afspeellijst moet komen! De rest mag dan stemmen. + Werkt alleen in het #top2000 kanaal. + Bijvoorbeeld: + !stem https://open.spotify.com/track/LOr3miP5uMd0l0R51t4m3T''' + # Maak lijst met links om dubbele nummers te vermijden + voorlopigelijst = [line.rstrip('\n') for line in open('lijst.txt')] + # if not ctx.channel.id == stemkanaal: # ??? kapoet ??? + if not ctx.channel.name.startswith(self.stemkanaal_naam): # indien verkeerde kanaal + yield from ctx.channel.send(ctx.author.mention + ", stemmen doen we in het " + self.stemkanaal + " kanaal") + return + elif text in voorlopigelijst: # filter duplicaten + yield from ctx.channel.send(ctx.author.mention + ", je hebt geluk: dit liedje is reeds in de lijst opgenomen!") + return + if text != None and text.startswith("https://open.spotify.com/track/"): # indien geen leeg commando en valide link + message = yield from ctx.channel.send(text) + for emoji in (self.thumbs_up, self.thumbs_down): # geeft stemkeuze + yield from message.add_reaction(emoji) + else: + yield from ctx.channel.send(ctx.author.mention + ", je moet wel een valide link geven waar we op kunnen stemmen!") + try: + yield from ctx.message.delete() + except: + pass # pas op icm !stemtest + + diff --git a/Stroopwafel.py b/Stroopwafel.py index 1086b15..5c07f7a 100644 --- a/Stroopwafel.py +++ b/Stroopwafel.py @@ -6,6 +6,7 @@ import General import Cryptocoin import Chatbot import Points +import Spotify import logging import importlib import configparser @@ -41,6 +42,7 @@ bot.add_cog(Cryptocoin.Cryptocoin(bot)) bot.add_cog(Chatbot.Chatbot(bot)) pointsBot = Points.Points(bot) bot.add_cog(pointsBot) +bot.add_cog(Spotify.Spotify(bot)) @bot.event @asyncio.coroutine