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