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