Created
August 30, 2019 21:52
-
-
Save brad-alexander/72402b9b7010ace7cf2bce38742a3b2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
writes out a csv with public playtime stats and wishlist for your steam friends. | |
fill in vars: | |
apikey: https://steamcommunity.com/dev/apikey | |
mysteamid: steamid64 per https://steamid.io/lookup/ | |
''' | |
import codecs | |
from collections import defaultdict | |
from itertools import islice | |
import requests | |
apikey = '' | |
mysteamid = '' | |
def get_playerinfo(*steamids): | |
playerinfo = {} | |
remaining_steamids = iter(steamids) | |
while remaining_steamids: | |
batch = ','.join([str(x) for x in islice(remaining_steamids, 100)]) | |
if not batch: break | |
resp = requests.get(f'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={apikey}&steamids={batch}') | |
for player in resp.json()['response']['players']: | |
playerinfo[player['steamid']] = player | |
return playerinfo | |
def get_wishlist(steamid): | |
url = f'https://store.steampowered.com/wishlist/profiles/{steamid}/wishlistdata/?p=0' | |
resp = requests.get(url) | |
try: | |
gamelist = [game[1]['name'] for game in resp.json().items()] | |
except TypeError: | |
gamelist = [] | |
return gamelist | |
def get_user_gamestats(steamid): | |
try: | |
resp = requests.get(f'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={apikey}&steamid={steamid}&format=json&include_appinfo=true&include_played_free_games=true').json()['response']['games'] | |
except: | |
resp = [] | |
return resp | |
def summary(friendinfo): | |
for friendid, friend in friendinfo.items(): | |
try: | |
for game in friend['wishlist']: | |
yield friend['personaname'] + ',wishlist,' + game + '\n' | |
for game in friend['gamestats']: | |
yield friend['personaname'] + ',playtime,' + str.replace(game['name'], ",", " ") + ',' + str(game['playtime_forever']) + '\n' | |
except GeneratorExit: | |
raise | |
except: | |
continue | |
def main(): | |
print('getting friends') | |
r = requests.get(f'http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key={apikey}&steamid={mysteamid}&relationship=friend') | |
friends = [friend['steamid'] for friend in r.json()['friendslist']['friends']] | |
friends.append(mysteamid) | |
print('getting gamestats for friends') | |
friendinfo = get_playerinfo(*friends) | |
wishlist = defaultdict(list) | |
for friend in friendinfo: | |
friendinfo[friend]['wishlist'] = get_wishlist(friend) | |
friendinfo[friend]['gamestats'] = get_user_gamestats(friend) | |
for game in friendinfo[friend]['wishlist']: | |
wishlist[game].append(friendinfo[friend]) | |
print('writing gamestats.csv') | |
lines = summary(friendinfo) | |
with codecs.open('gamestats.csv', 'w', 'utf-8') as f: | |
f.writelines(lines) | |
print('done') | |
return friendinfo, wishlist | |
if __name__ == '__main__': | |
friendinfo, wishlist = main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment