mirror of
https://github.com/MrDetonia/Maki.git
synced 2024-11-28 21:45:30 -05:00
v0.17.4 use JSON instead of XML for last.fm API
This commit is contained in:
parent
55e7b9e51b
commit
0d966522fd
43
bot.py
43
bot.py
@ -11,8 +11,7 @@ import discord
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
import urllib3
|
import requests
|
||||||
from html.parser import HTMLParser
|
|
||||||
import sys
|
import sys
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -32,7 +31,7 @@ from secret import token, lfmkey
|
|||||||
# CONFIGURATION
|
# CONFIGURATION
|
||||||
|
|
||||||
# bot version
|
# bot version
|
||||||
version = "v0.17.3"
|
version = "v0.17.4"
|
||||||
|
|
||||||
# text shown by .help command
|
# text shown by .help command
|
||||||
helptext = """I am a Discord bot written in Python
|
helptext = """I am a Discord bot written in Python
|
||||||
@ -75,9 +74,6 @@ handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w'
|
|||||||
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
# init urllib3 pool manager
|
|
||||||
http = urllib3.PoolManager()
|
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
|
|
||||||
# converts a datetime to a string
|
# converts a datetime to a string
|
||||||
@ -93,23 +89,24 @@ def lastfm_np(username):
|
|||||||
# sanitise username
|
# sanitise username
|
||||||
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
|
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
|
||||||
|
|
||||||
# fetch xml from last.fm
|
# fetch JSON from last.fm
|
||||||
r = http.request("GET", "https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=" + cleanusername + "&limit=1&api_key=" + lfmkey)
|
payload = {'format': 'json', 'method': 'user.getRecentTracks', 'user': cleanusername, 'limit': '1', 'api_key': lfmkey}
|
||||||
|
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload)
|
||||||
|
|
||||||
if r.status != 200:
|
# read json data
|
||||||
|
np = r.json()
|
||||||
|
|
||||||
|
# check we got a valid response
|
||||||
|
if 'error' in np:
|
||||||
return "Couldn't get last.fm data for " + username
|
return "Couldn't get last.fm data for " + username
|
||||||
|
|
||||||
xml = r.data.decode('utf-8')
|
# get fields
|
||||||
|
username = np['recenttracks']['@attr']['user']
|
||||||
# un-fuck text
|
track = np['recenttracks']['track'][0]
|
||||||
h = HTMLParser()
|
album = track['album']['#text']
|
||||||
xml = h.unescape(xml)
|
artist = track['artist']['#text']
|
||||||
|
song = track['name']
|
||||||
# isolate fields
|
nowplaying = '@attr' in track
|
||||||
username = xml.split('" page="')[0].split('<recenttracks user="')[1]
|
|
||||||
artist = xml.split('</artist>')[0].split('>')[-1]
|
|
||||||
song = xml.split('</name>')[0].split('<name>')[1]
|
|
||||||
album = xml.split('</album>')[0].split('>')[-1]
|
|
||||||
|
|
||||||
# grammar
|
# grammar
|
||||||
if album != "":
|
if album != "":
|
||||||
@ -117,10 +114,10 @@ def lastfm_np(username):
|
|||||||
else:
|
else:
|
||||||
albumtext = "`"
|
albumtext = "`"
|
||||||
|
|
||||||
if xml.find("track nowplaying=\"true\">") == -1:
|
if nowplaying == True:
|
||||||
nowplaying = " last listened"
|
|
||||||
else:
|
|
||||||
nowplaying = " is listening"
|
nowplaying = " is listening"
|
||||||
|
else:
|
||||||
|
nowplaying = " last listened"
|
||||||
|
|
||||||
# construct string
|
# construct string
|
||||||
return username + nowplaying + " to `" + song + "` by `" + artist + albumtext
|
return username + nowplaying + " to `" + song + "` by `" + artist + albumtext
|
||||||
|
Loading…
Reference in New Issue
Block a user