picapi_

Steam Game Roulette

Oct 2nd, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. import time
  2. import os
  3. import webbrowser
  4. import urllib.request
  5. import json
  6. import random
  7.  
  8. print("Steam Roulette")
  9. time.sleep(1)
  10. if os.path.exists('apikey.cfg') == True:
  11.     print("API file found")
  12. else:
  13.     file222 = open("apikey.cfg","w")
  14.     file222.close()
  15. apikeyfile = open("apikey.cfg","r+")
  16. if apikeyfile.readline() == "SteamAPIKEY:\n":
  17.     print("Found a preexisting API key, using that!")
  18.     apikey = apikeyfile.readline()
  19.     print(apikey)
  20.     apikeyfile.close()
  21. else:
  22.     apikeyfile.close()
  23.     print("In order to use this tool you require a Steam Web API key.")
  24.     print("You can get this from the steam website. Login on the webpage that was just opened and follow the instructions.")
  25.     print("When you are given a API key, paste it into this prompt, then press enter! The key will then be saved, so you don't have to type it in again!")
  26.     webbrowser.open("https://steamcommunity.com/dev/apikey", new=2, autoraise=True)
  27.     apikey = input()
  28.     print("Saving the API key to disk...")
  29.     apikeyfileow = open("apikey.cfg","w")
  30.     apikeyfileow.write("SteamAPIKEY:\n")
  31.     apikeyfileow.write(apikey)
  32.     apikeyfileow.close()
  33.     print("Saved!")
  34. print("This program requires a SteamID to run. Input a full vanity URL OR a steam64ID for the user.")
  35. steamURLunparsed = input()
  36. if steamURLunparsed.startswith("http") == True:
  37.     steamURLsplit = steamURLunparsed.split("/",)
  38.     steamURL = steamURLsplit[4]
  39.     print("Resolving URL...")
  40.     try:
  41.         urllib.request.urlretrieve("https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?key="+apikey+"&vanityurl="+steamURL,"User.PICACACHE")
  42.     except urllib.error.HTTPError:
  43.         print("Something went wrong while trying to contact the Steam Web API, terminating program...")
  44.         time.sleep(3.5)
  45.         exit()
  46.     userjson = open("./User.PICACACHE")
  47.     userjsoninuse = True
  48.     usercontent = userjson.read()
  49.     decodeduser = json.loads(usercontent)
  50.     steamusernumber = decodeduser['response']['steamid']
  51.     print("User "+steamURL+" has the ID "+steamusernumber)
  52. elif steamURLunparsed.startswith("7656119") == True:
  53.     steamusernumber = steamURLunparsed
  54.     userjsoninuse = False
  55. else:
  56.     print("That's not a valid URL or ID, terminating program.")
  57.     time.sleep(1.5)
  58.     exit()
  59. print("Getting a list of games that this player owns...")
  60. try:
  61.     urllib.request.urlretrieve("https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key="+apikey+"&steamid="+steamusernumber,"Games.PICACACHE")
  62. except urllib.error.HTTPError:
  63.     print("Something went wrong while trying to contact the Steam Web API, terminating program...")
  64.     time.sleep(3.5)
  65.     exit()
  66. badgeslist1 = open("Games.PICACACHE","r")
  67. badgeslist2 = badgeslist1.read()
  68. decodedbadgeslist1 = json.loads(badgeslist2)
  69. badgeapps = list()
  70. count = int("0")
  71. for x in decodedbadgeslist1['response']['games']:
  72.     keys = (list(x.keys()))
  73.     values = (list(x.values()))
  74.     for i in range(1,len(keys)):
  75.         if keys[i] == "appid":
  76.                         count = count+1
  77.                         badgeapps.append(str(values[i]))
  78. print("Found "+str(count)+" games!")
  79. print("Selecting random game...")
  80. gamenumber = random.randint(0,count-1)
  81. print("Selected appid "+badgeapps[gamenumber]+", launching now!")
  82. webbrowser.open('steam://rungameid/'+badgeapps[gamenumber])
  83. time.sleep(3)
  84. print("Clearing PICACACHE files...")
  85. badgeslist1.close()
  86. if userjsoninuse == True:
  87.     userjson.close()
Add Comment
Please, Sign In to add comment