Advertisement
Aveneid

SteamWebApi

Dec 6th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.04 KB | None | 0 0
  1. """
  2. Steam Informator
  3. Created by Bartlomiej Ferenc (C) 2018
  4. """
  5.  
  6. import requests,json,sys,os
  7. from datetime import datetime
  8.  
  9.  
  10. api_key = "&key=steam_api_key"
  11.  
  12.  
  13.  
  14. # Main menu
  15. def main_menu():
  16.     os.system('clear')
  17.     print("\n\n")
  18.     print("~~~~ Welcome to SteamInformator ~~~~")
  19.     print("Please choose the menu you want to start:")
  20.     print("1. User info")
  21.     print("2. Game info")
  22.     print("\n0. Quit")
  23.     choice = input(" >>  ")
  24.     exec_menu(choice)
  25.  
  26.     return
  27.  
  28. # Execute menu
  29. def exec_menu(choice):
  30.     os.system('clear')
  31.     ch = choice.lower()
  32.     if ch == '':
  33.         menu_actions['main_menu']()
  34.     else:
  35.         menu_actions[ch]('0')
  36.     return
  37.  
  38. def exit():
  39.     sys.exit()
  40.  
  41. def game_info(l):
  42.     choose = input("Game id: ")
  43.     url = "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid="+choose + "&format=json"
  44.     r = requests.get(url)
  45.     data = r.json()['achievementpercentages']['achievements']
  46.     data_count = json.dumps(data).count('percent')
  47.     data_percentages = []
  48.    
  49.     i=0
  50.     for i in range(data_count):
  51.         data_percentages.append([data[i]['name'],data[i]['percent']])
  52.        
  53.     i=0
  54.     for i in range(data_count):
  55.         print("achievement name: ",data_percentages[i][0])
  56.         print("all  % completed: ",float("{0:.2f}".format(data_percentages[i][1])))
  57.     main_menu()
  58. def user_info(choice):
  59.     url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?&steamids="
  60.     if choice == '0':
  61.         choice = input("\nEnter profile number: ")
  62.     url = url + choice + api_key
  63.     r = requests.get(url)
  64.     data = r.json()['response']['players'][0]
  65.     steam_id = json.dumps(data['steamid']).strip('"')
  66.     visiblity = json.dumps(data['profilestate']).strip('"')
  67.     state = json.dumps(data['personastate']).strip('"')
  68.     last_online = unixToNormal(json.dumps(data['lastlogoff'])).strip('"')
  69.     time_created = unixToNormal(json.dumps(data['timecreated'])).strip('"')
  70.     nick_name = json.dumps(data['personaname']).strip('"')
  71.     url = "http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?&steamids=" + steam_id + api_key
  72.     r = requests.get(url)
  73.     data = r.json()['players'][0]
  74.    
  75.     bans = []
  76.     bans.append(json.dumps(data['CommunityBanned']))
  77.     bans.append(json.dumps(data['VACBanned']))
  78.     bans.append(json.dumps(data['NumberOfVACBans']))
  79.     bans.append(json.dumps(data['DaysSinceLastBan']))
  80.     bans.append(json.dumps(data['NumberOfGameBans']))
  81.     bans.append(json.dumps(data['EconomyBan']))
  82.    
  83.     print('\n\n~~~~~~ Profile info ~~~~~~')
  84.     print("Nick: ", nick_name)
  85.     print('SteamID: ',steam_id)
  86.     print('Last Online:',last_online)
  87.     print('Profile created: ',time_created)
  88.     print('Visibility: ',visiblity_text[visiblity])
  89.     print('State: ',states[state])
  90.     print('\nBans: ')
  91.    
  92.     print('         CommunityBanned:', bans[0])
  93.     print('         VACBanned:', bans[1])
  94.     print('         NumberOfVACBans:', bans[2])
  95.     print('         DaysSinceLastBan:', bans[3])
  96.     print('         NumberOfGameBans:', bans[4])
  97.     print('         EconomyBan:', bans[4])
  98.        
  99.        
  100.    
  101.    
  102.     print("\n\nMenu:\n1 Show friend list")
  103.     print("2 Show owned games")
  104.     print("3 Show achievements")
  105.     print("0 Main menu")
  106.    
  107.     choose = input(" >> ")
  108.     if choose == '1':
  109.         friend_list_show(steam_id)
  110.     if choose == '2':
  111.         game_list_show(steam_id)
  112.     if choose == '3':
  113.         achievements_show(steam_id)
  114.     if choose == '0':
  115.         main_menu()
  116.    
  117.        
  118. def friend_list_show(steam_id):
  119.     print("\nFriend list:")
  120.     url = ' http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?relationship=friend&steamid='+steam_id + api_key
  121.     r = requests.get(url)
  122.     data = r.json()['friendslist']['friends']
  123.    
  124.     friends_data = []
  125.     friends = json.dumps(data).count("steamid")
  126.     i=0
  127.    
  128.     for i in range(friends):
  129.         friends_data.append([json.dumps(data[i]['steamid']),json.dumps(data[i]['friend_since'])])    
  130.     i=0
  131.     for i in range(friends):
  132.         url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?&steamids=" + friends_data[i][0] + api_key
  133.         r = requests.get(url)
  134.         data = r.json()['response']['players'][0]['personaname'].strip('"')
  135.         print("\n- Nick: ",data)
  136.         print("  Friends since: ",unixToNormal(json.dumps(int(friends_data[i][1]))))
  137.     user_info(steam_id)
  138. def achievements_show(steam_id):
  139.     url =  "http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid="
  140.     appID = input("App id: ")
  141.     url = url + appID + "&steamid=" + steam_id + api_key
  142.     r = requests.get(url)
  143.     data = r.json()['playerstats']['achievements']
  144.    
  145.     achievement_data = []
  146.     achieviement_count = json.dumps(data).count("achieved")
  147.     i=0
  148.    
  149.     for i in range(achieviement_count):
  150.         achievement_data.append([json.dumps(data[i]['apiname']).strip('"'),json.dumps(data[i]['achieved']),json.dumps(data[i]['unlocktime'])])
  151.        
  152.     print("1 Only achieved")
  153.     print("2 Only not achieved")
  154.     print("3 Both")
  155.     choose = input(" >> ")
  156.     i=0
  157.     if choose == '1':
  158.        for i in range(achieviement_count):
  159.            if achievement_data[i][1] == '1':
  160.                print("Achievement name: ", achievement_data[i][0])
  161.                print("        Unlocked: ", unixToNormal(achievement_data[i][2]))
  162.     if choose == '2':
  163.         for i in range(achieviement_count):
  164.            if achievement_data[i][1] == '0':
  165.                print("Achievement name: ", achievement_data[i][0])
  166.                print("        Unlocked: No")
  167.     if choose == '3':
  168.      for i in range(achieviement_count):
  169.                print("Achievement name: ", achievement_data[i][0])
  170.                if achievement_data[i][1] == '0':
  171.                    print("        Unlocked:  No")
  172.                else:
  173.                    print("        Unlocked: ",unixToNormal( achievement_data[i][2]))
  174.        
  175.     user_info(steam_id)
  176.  
  177. def game_list_show(steam_id):
  178.     url = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?steamid=" + steam_id + api_key + "&format=json&include_appinfo=1"
  179.     r = requests.get(url)
  180.     data = r.json()['response']['games']
  181.     game_count = r.json()['response']['game_count']
  182.    
  183.     for i in range(game_count):
  184.         print("Game name:{:>8}  %s >> playtime (in hours): ".format(data[i]['appid']) % data[i]['name']  , float("{0:.2f}".format(int(data[i]['playtime_forever'])/60)))
  185.     user_info(steam_id)
  186.    
  187.  
  188. def unixToNormal(time):
  189.     return datetime.utcfromtimestamp(int(time)).strftime('%Y-%m-%d %H:%M:%S')
  190.  
  191.  
  192. menu_actions  = {
  193.     'main': main_menu,
  194.     '1': user_info,
  195.     '2': game_info,
  196.     '0': exit}
  197. visiblity_text = {
  198.         '1':'Private',
  199.         '3':'Public'}
  200. states = {
  201.         '0':'Offline',
  202.         '1':'Online',
  203.         '2':'Busy',
  204.         '3':'Away',
  205.         '4':'Snooze',
  206.         '5':'looking to trade',
  207.         '6':'looking to play'}
  208.  
  209.  
  210. if __name__ == "__main__":
  211.     # Launch main menu
  212.     main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement