From 1bd841a59012161683a3d73baaaa178315ab1920 Mon Sep 17 00:00:00 2001 From: MarkPash Date: Sat, 8 Apr 2017 13:32:48 +0000 Subject: [PATCH 1/2] added steam status --- .gitignore | 1 + bot.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ffdb91c..fee03e2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ secret.py __pycache__ *.json *.log +venv/ diff --git a/bot.py b/bot.py index bc105a5..309b129 100644 --- a/bot.py +++ b/bot.py @@ -25,7 +25,7 @@ import logging import markov # file in this directory called "secret.py" should contain these variables -from secret import token, lfmkey +from secret import token, lfmkey, steamkey # CONFIGURATION @@ -50,6 +50,7 @@ My commands are: .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 +.steam [] - fetch steam status for you or a specific vanityname ```""" # IDs of admin users @@ -128,6 +129,48 @@ def lastfm_np(username): # construct string return username + nowplaying + " to `" + song + "` by `" + artist + albumtext +# gets general steam user info from a vanityurl name +def steamdata(vanityname): + # sanitise username + cleanvanityname = re.sub(r'[^a-zA-Z0-9_-]', '', vanityname, 0) + + resolveurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=' + dataurl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + + # fetch json from steam + try: + idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' + vanityname).json()['response'] + except: + return 'I can\'t connect to Steam' + + # check if user was found and extract steamid + if idresponse['success'] is not 1: + return ' I couldn\'t find ' + vanityname + else: + steamid = idresponse['steamid'] + + # fetch steam user info + try: + dataresponse = requests.get(dataurl + steamkey + '&steamids=' + steamid).json()['response']['players'][0] + except: + return 'Can\'t find info on ' + vanityname + + personastates = ['Offline', 'Online', 'Busy', 'Away', 'Snoozed', 'Looking to trade', 'Looking to play'] + + if 'personaname' in dataresponse: + namestr = dataresponse['personaname'] + else: namestr = '' + if 'personastate' in dataresponse: + statestr = personastates[dataresponse['personastate']] + else: statestr = '' + if 'gameextrainfo' in dataresponse: + gamestr = ' playing ' + dataresponse['gameextrainfo'] + else: gamestr = '' + + responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')] + + return '\n'.join(responsetext) + # EVENT HANDLERS @@ -304,6 +347,15 @@ def on_message(message): else: response = lastfm_np(tmp) + elif message.content.startswith('.steam'): + # show steam status + tmp = message.content[7:] + + if tmp == '': + response = steamdata(message.author.name) + else: + response = steamdata(tmp) + elif message.content.startswith('.qr '): # generate QR code - DANGEROUS, CHECK CAREFULLY HERE tmp = message.content[4:] From 5b5588f537a8d48570902f8059769f9a3e21908c Mon Sep 17 00:00:00 2001 From: MarkPash Date: Sat, 8 Apr 2017 13:48:12 +0000 Subject: [PATCH 2/2] formatting responsetext --- bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 309b129..18cf4a9 100644 --- a/bot.py +++ b/bot.py @@ -161,10 +161,10 @@ def steamdata(vanityname): namestr = dataresponse['personaname'] else: namestr = '' if 'personastate' in dataresponse: - statestr = personastates[dataresponse['personastate']] + statestr = '`' + personastates[dataresponse['personastate']] + '`' else: statestr = '' if 'gameextrainfo' in dataresponse: - gamestr = ' playing ' + dataresponse['gameextrainfo'] + gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`' else: gamestr = '' responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')]