From 73e31db41853ec7da22c5616870983528d8592f2 Mon Sep 17 00:00:00 2001 From: Zac Herd Date: Thu, 1 Dec 2016 19:29:58 +0000 Subject: [PATCH] v0.17.0 added last.fm now playing command --- bot.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index bb53aa9..4ca7967 100644 --- a/bot.py +++ b/bot.py @@ -11,6 +11,8 @@ import discord import asyncio import os import io +import urllib3 +from html.parser import HTMLParser import sys import shlex import subprocess @@ -24,13 +26,13 @@ import logging import markov # file in this directory called "secret.py" should contain these variables -from secret import token +from secret import token, lfmkey # CONFIGURATION # bot version -version = "v0.16.8" +version = "v0.17.0" # text shown by .help command helptext = """I am a Discord bot written in Python @@ -45,9 +47,10 @@ My commands are: .seen - prints when user was last seen .say - say something .sayy - say something a e s t h e t i c a l l y -.markov [] - generate markov chain over chat history; a blank user will use you +.markov [] - generate markov chain over chat history for you or another user .roll d - roll x number of y sided dice .qr - generate a QR code +.np [] - fetch now playing from last.fm for you or a specific username ```""" # IDs of admin users @@ -72,6 +75,8 @@ handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w' handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) logger.addHandler(handler) +# init urllib3 pool manager +http = urllib3.PoolManager() # FUNCTIONS @@ -83,6 +88,40 @@ def strfromdt(dt): def dtfromts(ts): return datetime.datetime.fromtimestamp(ts) +# gets now playing information from last.fm +def lastfm_np(username): + # fetch xml from last.fm + r = http.request("GET", "https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=" + username + "&limit=1&api_key=" + lfmkey) + + if r.status != 200: + return "Couldn't get last.fm data for " + username + + xml = r.data.decode('utf-8') + + # un-fuck text + h = HTMLParser() + xml = h.unescape(xml) + + # isolate fields + username = xml.split('" page="')[0].split('") == -1: + nowplaying = " last listened" + else: + nowplaying = " is listening" + + # construct string + return username + nowplaying + " to `" + song + "` by `" + artist + albumtext + # EVENT HANDLERS @@ -242,6 +281,14 @@ def on_message(message): else: response = 'you did it wrong!' + elif message.content.startswith('.np'): + tmp = message.content[4:] + + if tmp == '': + response = lastfm_np(message.author.name) + else: + response = lastfm_np(tmp) + elif message.content.startswith('.qr '): # generate QR code - DANGEROUS, CHECK CAREFULLY HERE tmp = message.content[4:]