From ce43eac38b806d47a03e56a6c8e60dfe37b97ab0 Mon Sep 17 00:00:00 2001 From: booboy Date: Sat, 20 Jan 2018 00:23:16 -0600 Subject: [PATCH] d13 - made a script that gets current players in all games on steam --- ...018-01-01-100-days-of-code-day013.markdown | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 _100-days-of-code/2018-01-01-100-days-of-code-day013.markdown diff --git a/_100-days-of-code/2018-01-01-100-days-of-code-day013.markdown b/_100-days-of-code/2018-01-01-100-days-of-code-day013.markdown new file mode 100644 index 0000000..1ba4025 --- /dev/null +++ b/_100-days-of-code/2018-01-01-100-days-of-code-day013.markdown @@ -0,0 +1,67 @@ +--- +layout: post +title: "day 12" +date: 2018-01-19 +categories: programming +--- + +# 100 Days of Code + +### Day 13: +Today I made a script that uses the Steam HTTP API to iterate through all +possible games/apps on steam and outputs the game/app name, and current player +count. It is a fun way for me to practice interacting with json dictionaries. + +```python +import json +import requests + +BASE_URL = 'https://api.steampowered.com' + + +def main(): + """ Loop through list of appids and feed appid list into retrieval of + current players in game corresponding to appid + """ + + appnames, appids = zip(*get_appid()) + + for appname, appid in zip(appnames, appids): + resp_data = json.loads(get_current_players(appid)) + player_count = resp_data['response']['player_count'] + print('Game Name: {}'.format(appname)) + print('Current Players: {}\n'.format(player_count)) + + +def get_current_players(appid): + """ Queries Steam API for number of current players in a game + + :param appid: The appid of the game title + :param type: `str` + + :returns: Returns string json response of number of current players + :rtype: `str` + """ + + url = '{}/ISteamUserStats/GetNumberofCurrentPlayers/v1/?key=KEY&format=json&appid={}'.format(BASE_URL, appid) + r = requests.get(url) + return r.text + + +def get_appid(): + """ Queries Steam API for appids + :returns: (appname, appid) + :rtype: `tuple` + """ + url = '{}/ISteamApps/GetAppList/v2'.format(BASE_URL) + r = requests.get(url) + json_data = json.loads(r.text) + apps = json_data['applist']['apps'] + appname_and_id = [(dic['name'], dic['appid']) for dic in apps] + + return appname_and_id + + +if __name__ == '__main__': + main() +```