From 67a1b2cc0fc9ec705cb3aaf016f5e2f999603b92 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Wed, 24 May 2017 20:13:36 -0400 Subject: [PATCH] Added now playing command for ToukuFM --- example_config.cfg | 6 +++++- helpcommand.txt | 3 ++- pocketbot.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/example_config.cfg b/example_config.cfg index af379a3..76f80e1 100644 --- a/example_config.cfg +++ b/example_config.cfg @@ -15,4 +15,8 @@ owner_id: #Reddit Stuff reddit_client_id: -reddit_client_secret: \ No newline at end of file +reddit_client_secret: + +#Icecast Stuff +metadataurl: +iceurl: \ No newline at end of file diff --git a/helpcommand.txt b/helpcommand.txt index 6e0c2aa..c393d7e 100644 --- a/helpcommand.txt +++ b/helpcommand.txt @@ -18,4 +18,5 @@ **!joke**: Posts a random joke from /r/Jokes **!8ball**: You ask a question, it answeres. Simple! **!conch** or **!magicconch**: Same as the 8ball command but waaaaay cooler -**!giveaway prize**: Do a giveaway based on users reacting with the emoji you react with secretly. Use in private and replace prize with an actual prize! \ No newline at end of file +**!giveaway prize**: Do a giveaway based on users reacting with the emoji you react with secretly. Use in private and replace prize with an actual prize! +**!np**: Posts currently playing info for ToukuFM \ No newline at end of file diff --git a/pocketbot.py b/pocketbot.py index a974ee3..10051b3 100644 --- a/pocketbot.py +++ b/pocketbot.py @@ -7,6 +7,8 @@ import random import xkcd import praw import os +import requests +import json #Get script location cwd = os.path.dirname(os.path.realpath(__file__)) @@ -26,6 +28,10 @@ reddit_client_secret = config.get("configuration", "reddit_client_secret") reddit_user_agent = 'linux:com.pocketjawa.discord-pocketbot:v0.1 (by /u/pocketjava)' reddit = praw.Reddit(client_id=reddit_client_id,client_secret=reddit_client_secret,user_agent=reddit_user_agent) +#Icecast stream info setup +metadataurl = config.get("configuration", "metadataurl") +iceurl = config.get("configuration", "iceurl") + client = discord.Client() #Subroutine to get UPS Status @@ -207,6 +213,38 @@ def on_message(message): #Congratulate and ,essage the winner yield from client.send_message(public_channel_id, 'Congrats to {0.user}!'.format(res)) yield from client.send_message(res.user, args) + #Get currently playing stats from toukufm.com + elif message.content.lower().startswith('!np'): + + #Load JSON from stream APIs + nprequest = requests.get(metadataurl) + npobject = json.loads(nprequest.text) + icerequest = requests.get(iceurl) + iceobject = json.loads(icerequest.text) + + #Add up listeners + listenersum = int(0) + for stream in iceobject['icestats']['source']: + listenersum += stream["listeners"] + + #Put together the message + output = "" + if 'comment' in npobject: + output += "[{}]".format(npobject['comment']) + if 'artist' in npobject: + output += "\n{} - ".format(npobject['artist']) + if 'title' in npobject: + if 'artist' in npobject: + output += "{} ".format(npobject['title']) + else: + output += "\n{} ".format(npobject['title']) + if 'WOAR' in npobject: + output += "\nDownload/Buy: {}".format(npobject['WOAR']) + output += "\nListeners: {} ".format(listenersum) + + #Set embed and send the message + npem = discord.Embed(title="Now Playing on ToukuFM", description=output, url="https://toukufm.com/") + yield from client.send_message(message.channel, embed=npem) client.loop.create_task(ups_check())