From: John Runyon Date: Thu, 7 Sep 2023 04:22:00 +0000 (-0600) Subject: add steam module with !STEAMNP command X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/0ee76fccb83e7fdfd6d822e164ad04a0045b1d37 add steam module with !STEAMNP command --- diff --git a/modules/steam.py b/modules/steam.py new file mode 100644 index 0000000..a72f5bf --- /dev/null +++ b/modules/steam.py @@ -0,0 +1,97 @@ +# Erebus IRC bot - Author: Erebus Team +# vim: fileencoding=utf-8 + +# module info +modinfo = { + 'author': 'Erebus Team', + 'license': 'public domain', + 'compatible': [0], # compatible module API versions + 'depends': [], # other modules required to work properly? + 'softdeps': ['help'], # modules which are preferred but not required +} + +# preamble +import modlib +lib = modlib.modlib(__name__) +modstart = lib.modstart +modstop = lib.modstop + +# module code +import json +import sys +if sys.version_info.major < 3: + from urllib import urlopen, quote, HTTPError +else: + from urllib.request import urlopen + from urllib.parse import quote + from urllib.error import HTTPError +_lookup_states = { + 0: 'Offline', + 1: 'Online', + 2: 'Busy', + 3: 'Away', + 4: 'Snooze', + 5: 'Looking to trade', + 6: 'Looking to play' +} + +def _key(): + return lib.parent.cfg.get('steam', 'apikey') + +def _resolve_urlname(urlname): + if urlname.isascii() and urlname.isdecimal(): + return urlname + url = "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s" % (_key(), urlname) + try: + data = json.load(urlopen(url)) + if lib.parent.cfg.getboolean('debug', 'steam'): + lib.parent.log('*', "?", repr(data)) + return data['response']['steamid'] + except HTTPError as e: + raise Exception("(In looking up SteamID:) No such user (or some other error: %s %s) " % (e.code, e.msg)) + except json.JSONDecodeError as e: + raise Exception("(In looking up SteamID:) Couldn't decode response: %s %d" % (e.msg, e.pos)) + except KeyError as e: + raise Exception("(In looking up SteamID:) Couldn't read response: %r" % (e)) + except Exception as e: + raise Exception("(In looking up SteamID:) Some other error occurred: %s %r" % (type(e).__name__, e)) + + + +@lib.hook(needchan=False, wantchan=True) +@lib.help('', 'looks up what a user is playing on Steam') +@lib.argsEQ(1) +def steamnp(bot, user, chan, realtarget, *args): + steamid = _resolve_urlname(args[0]) + + url = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s" % (_key(), steamid) + + try: + data = json.load(urlopen(url)) + except HTTPError as e: + raise Exception("No such user (or some other error: %s %s) " % (e.code, e.msg)) + except json.JSONDecodeError as e: + raise Exception("Couldn't decode response: %s %d" % (e.msg, e.pos)) + except KeyError as e: + raise Exception("Couldn't read response: %r" % (e)) + except Exception as e: + raise Exception("Some other error occurred: %s %r" % (type(e).__name__, e)) + + if lib.parent.cfg.getboolean('debug', 'steam'): + lib.parent.log('*', "?", repr(data)) + + player = data['response']['players'][0] + + profileurl = player['profileurl'] + stateint = player['personastate'] + state = _lookup_states[stateint] + displayname = player['personaname'] + if 'gameextrainfo' in player: + gamename = player['gameextrainfo'] + else: + gamename = None + + if gamename is None: + return "%s (%s) is not playing a game right now. %s" % (displayname, state, profileurl) + else: + return "%s (%s) is playing \037%s\037. %s" % (displayname, state, gamename, profileurl)