]>
jfr.im git - erebus.git/blob - modules/steam.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
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
15 lib
= modlib
.modlib(__name__
)
16 modstart
= lib
.modstart
22 if sys
.version_info
.major
< 3:
23 from urllib
import urlopen
, quote
, HTTPError
25 from urllib
.request
import urlopen
26 from urllib
.parse
import quote
27 from urllib
.error
import HTTPError
34 5: 'Looking to trade',
39 return lib
.parent
.cfg
.get('steam', 'apikey')
41 def _resolve_urlname(urlname
):
42 if urlname
.isascii() and urlname
.isdecimal():
44 url
= "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s" % (_key(), urlname
)
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
))
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
))
61 @lib.hook(needchan
=False, wantchan
=True)
62 @lib.help('<custom URL|SteamID>', 'looks up what a user is playing on Steam')
64 def steamnp(bot
, user
, chan
, realtarget
, *args
):
65 steamid
= _resolve_urlname(args
[0])
67 url
= "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s" % (_key(), steamid
)
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
))
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
))
80 if lib
.parent
.cfg
.getboolean('debug', 'steam'):
81 lib
.parent
.log('*', "?", repr(data
))
83 player
= data
['response']['players'][0]
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']
95 return "%s (%s) is not playing a game right now. %s" % (displayname
, state
, profileurl
)
97 return "%s (%s) is playing \037%s\037. %s" % (displayname
, state
, gamename
, profileurl
)