Scripts/sstatus

71 lines
2.3 KiB
Plaintext
Raw Normal View History

2022-04-05 16:34:23 -04:00
#!/usr/bin/python3
import requests
import sys
import textwrap
2022-04-05 19:04:21 -04:00
import pprint
import simplejson
2022-04-05 16:34:23 -04:00
2022-04-05 17:26:17 -04:00
api = 'http://api.steampowered.com/'
2022-04-05 16:34:23 -04:00
key = '63578AF8E85EDF1E49411D5D4E18E166'
2022-04-05 17:26:17 -04:00
uid = '76561198008294872' #whiteline
#uid = '76561198111568728' #N33R
2022-04-05 16:34:23 -04:00
# Fetch JSON from url and run it through transform, pretty printing errors
# and the data worked on as exhaustively as possible.
def json_query(url, transform, params={}):
try:
result = requests.get(url, params)
2022-04-05 19:04:21 -04:00
except requests.exceptions.ConnectionError:
2022-04-05 16:34:23 -04:00
print("Network connection error.")
sys.exit(1)
try:
data = result.json()
2022-04-05 19:04:21 -04:00
except simplejson.errors.JSONDecodeError as err:
print('Error when decoding JSON:\nFrom endpoint ' + url + ':\n' + str(err) + '\n' + str(result) + '\n')
2022-04-05 16:34:23 -04:00
sys.exit(1)
try:
return transform(data)
except (IndexError, KeyError) as err:
2022-04-05 19:04:21 -04:00
print('Error when traversing JSON:\nFrom endpoint ' + url + ':\n' + str(err))
2022-04-05 16:34:23 -04:00
pprint.PrettyPrinter(indent=2).pprint(data)
sys.exit(1)
def steam_query(endpoint, transform, params={}):
new_params = params.copy()
new_params['key'] = key
2022-04-05 17:26:17 -04:00
return json_query(api + endpoint + '/', transform, new_params)
2022-04-05 16:34:23 -04:00
2022-04-05 18:14:47 -04:00
def get_friend_list(steamid):
2022-04-05 16:34:23 -04:00
params = {
2022-04-05 18:14:47 -04:00
'steamid' : steamid,
2022-04-05 16:34:23 -04:00
'relationship' : 'friend'
}
2022-04-05 18:14:47 -04:00
return steam_query('ISteamUser/GetFriendList/v0001', lambda data: data['friendslist']['friends'], params)
def get_player_summaries(steamids):
params = {
'steamids' : ','.join(steamids)
}
return steam_query('ISteamUser/GetPlayerSummaries/v0002', lambda data: data['response']['players'], params)
def personastate(n):
states = {
0 : 'Offline',
1 : 'Online',
2 : 'Busy',
3 : 'Away',
4 : 'Snooze',
5 : 'Looking to trade',
6 : 'Looking to play'
}
return states[n]
2022-04-05 16:34:23 -04:00
def main():
2022-04-05 18:14:47 -04:00
#print('\n'.join([friend['steamid'] for friend in get_friend_list(uid)]))
for player in get_player_summaries([friend['steamid'] for friend in get_friend_list(uid)]):
2022-04-05 18:22:02 -04:00
print(player['personaname'] + ': ' + personastate(player['personastate']) + (', playing ' + player['gameextrainfo'] if 'gameextrainfo' in player else ''))
2022-04-05 18:14:47 -04:00
2022-04-05 16:34:23 -04:00
if __name__ == '__main__':
main()