Scripts/sstatus
2022-04-05 22:34:23 +02:00

48 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python3
import requests
import sys
import textwrap
api = 'https://api.steampowered.com/'
key = '63578AF8E85EDF1E49411D5D4E18E166'
uid = '76561198008294872'
# 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 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' + err + '\n' + result + '\n')
sys.exit(1)
try:
return transform(data)
except (IndexError, KeyError) as err:
print('Error when traversing JSON:\nFrom endpoint ' + url + ':\n' + err)
pprint.PrettyPrinter(indent=2).pprint(data)
sys.exit(1)
def steam_query(endpoint, transform, params={}):
new_params = params.copy()
new_params['key'] = key
json_query(api + endpoint + '/', transform, new_params)
def get_friend_list():
params = {
'steamid' : uid,
'relationship' : 'friend'
}
return steam_query('SteamUser/GetFriendList/v0001' lambda data: [friend['steamid'] for friend in data], params)
def main():
for steamid in get_friend_list():
print(steamid)
if __name__ == '__main__':
main()