Scripts/sstatus
2022-04-08 13:09:15 +02:00

83 lines
2.7 KiB
Python
Executable File

#!/usr/bin/python3
import requests
import sys
import textwrap
import pprint
try:
from simplejson.errors import JSONDecodeError
except ImportError:
from json.decoder import JSONDecodeError
api = 'https://api.steampowered.com/'
key = '63578AF8E85EDF1E49411D5D4E18E166'
uid = '76561198008294872' #whiteline
#uid = '76561198111568728' #N33R
# 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)
except requests.exceptions.ConnectionError:
print("Network connection error.")
sys.exit(1)
try:
data = result.json()
except JSONDecodeError as err:
print('Error when decoding JSON:\nFrom endpoint ' + url + ':\n' + str(err) + '\n' + str(result) + '\n')
sys.exit(1)
try:
return transform(data)
except (IndexError, KeyError) as err:
print('Error when traversing JSON:\nFrom endpoint ' + url + ':\n' + str(err))
pprint.PrettyPrinter(indent=2).pprint(data)
sys.exit(1)
def steam_query(endpoint, transform, params={}):
new_params = params.copy()
new_params['key'] = key
return json_query(api + endpoint + '/', transform, new_params)
def get_friend_list(steamid):
params = {
'steamid' : steamid,
'relationship' : 'friend'
}
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 boldgreen(string):
return '\033[92m' + '\033[1m' + string + '\033[0m'
def boldred(string):
return '\033[91m' + '\033[1m' + string + '\033[0m'
def boldgray(string):
return '\033[90m' + '\033[1m' + string + '\033[0m'
def personastate(n):
states = {
0 : boldred('Offline'),
1 : boldgreen('Online'),
2 : boldred('Busy'),
3 : boldgray('Away'),
4 : boldgray('Snooze'),
5 : boldgreen('Looking to trade'),
6 : boldgreen('Looking to play')
}
return states[n]
def main():
friends = [friend['steamid'] for friend in get_friend_list(uid)]
players = sorted(get_player_summaries(friends), key=lambda player: player['personaname'])
print('\n'.join([player['personaname'] + ': ' + personastate(player['personastate']) + (', playing ' + player['gameextrainfo'] if 'gameextrainfo' in player else '') for player in players]))
if __name__ == '__main__':
main()