]> jfr.im git - erebus.git/blame - modules/steam.py
update comments
[erebus.git] / modules / steam.py
CommitLineData
0ee76fcc
JR
1# Erebus IRC bot - Author: Erebus Team
2# vim: fileencoding=utf-8
bac69af4 3# Steam API commands (!steamnp)
0ee76fcc
JR
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [0], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11 'softdeps': ['help'], # modules which are preferred but not required
12}
13
14# preamble
15import modlib
16lib = modlib.modlib(__name__)
17modstart = lib.modstart
18modstop = lib.modstop
19
20# module code
21import json
22import sys
23if sys.version_info.major < 3:
24 from urllib import urlopen, quote, HTTPError
25else:
26 from urllib.request import urlopen
27 from urllib.parse import quote
28 from urllib.error import HTTPError
29_lookup_states = {
30 0: 'Offline',
31 1: 'Online',
32 2: 'Busy',
33 3: 'Away',
34 4: 'Snooze',
35 5: 'Looking to trade',
36 6: 'Looking to play'
37}
38
39def _key():
40 return lib.parent.cfg.get('steam', 'apikey')
41
42def _resolve_urlname(urlname):
43 if urlname.isascii() and urlname.isdecimal():
44 return urlname
45 url = "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s" % (_key(), urlname)
46 try:
47 data = json.load(urlopen(url))
48 if lib.parent.cfg.getboolean('debug', 'steam'):
49 lib.parent.log('*', "?", repr(data))
50 return data['response']['steamid']
51 except HTTPError as e:
52 raise Exception("(In looking up SteamID:) No such user (or some other error: %s %s) " % (e.code, e.msg))
53 except json.JSONDecodeError as e:
54 raise Exception("(In looking up SteamID:) Couldn't decode response: %s %d" % (e.msg, e.pos))
55 except KeyError as e:
56 raise Exception("(In looking up SteamID:) Couldn't read response: %r" % (e))
57 except Exception as e:
58 raise Exception("(In looking up SteamID:) Some other error occurred: %s %r" % (type(e).__name__, e))
59
60
61
62@lib.hook(needchan=False, wantchan=True)
63@lib.help('<custom URL|SteamID>', 'looks up what a user is playing on Steam')
64@lib.argsEQ(1)
65def steamnp(bot, user, chan, realtarget, *args):
66 steamid = _resolve_urlname(args[0])
67
68 url = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s" % (_key(), steamid)
69
70 try:
71 data = json.load(urlopen(url))
72 except HTTPError as e:
73 raise Exception("No such user (or some other error: %s %s) " % (e.code, e.msg))
74 except json.JSONDecodeError as e:
75 raise Exception("Couldn't decode response: %s %d" % (e.msg, e.pos))
76 except KeyError as e:
77 raise Exception("Couldn't read response: %r" % (e))
78 except Exception as e:
79 raise Exception("Some other error occurred: %s %r" % (type(e).__name__, e))
80
81 if lib.parent.cfg.getboolean('debug', 'steam'):
82 lib.parent.log('*', "?", repr(data))
83
84 player = data['response']['players'][0]
85
86 profileurl = player['profileurl']
87 stateint = player['personastate']
88 state = _lookup_states[stateint]
89 displayname = player['personaname']
90 if 'gameextrainfo' in player:
91 gamename = player['gameextrainfo']
92 else:
93 gamename = None
94
95 if gamename is None:
96 return "%s (%s) is not playing a game right now. %s" % (displayname, state, profileurl)
97 else:
98 return "%s (%s) is playing \037%s\037. %s" % (displayname, state, gamename, profileurl)