steam.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. '''This script contains methods belonging to the Steam web API
  2. that can collect information based on an user's gaming library.'''
  3. import requests
  4. import json
  5. def gamespulling(steamid, apikey):
  6. '''Returns the JSON data from the Steam API based of one's
  7. Steam ID number and returns a dictionary of
  8. gameids and minutes played.'''
  9. steaminfo = {
  10. 'key': apikey,
  11. 'steamid': steamid,
  12. 'format':'JSON',
  13. 'include_appinfo':'1'
  14. }
  15. apiurl = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/'
  16. req = requests.get(apiurl, params=steaminfo)
  17. data = json.loads(req.content)
  18. return data['response']['games']
  19. def steamidpulling(steamun, apikey):
  20. '''Pulls out and returns the steam id number for use in steam queries.'''
  21. steaminfo = {'key': apikey, 'vanityurl': steamun}
  22. apiurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/'
  23. req = requests.get(apiurl, params=steaminfo)
  24. data = json.loads(req.content)
  25. steamid = data['response']['steamid']
  26. return steamid
  27. def steamlibrarypull(steamid, apikey):
  28. '''Pulls out a CSV of Steam appids.'''
  29. steaminfo = {
  30. 'key': apikey,
  31. 'steamid': steamid,
  32. 'format':'JSON',
  33. 'include_appinfo':'1'
  34. }
  35. url = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/'
  36. req = requests.get(url, params=steaminfo)
  37. data = json.loads(req.content)
  38. response = data['response']['games']
  39. games = {}
  40. for game in response:
  41. url = 'http://store.steampowered.com/api/appdetails/?appids=%d&filters=price_overview&cc=us'
  42. getprice = requests.get(url % game['appid'])
  43. if getprice.status_code == 200:
  44. rjson = json.loads(getprice.text)
  45. # use the appid to fetch the value and convert to decimal
  46. # appid is numeric, cast to string to lookup the price
  47. try:
  48. price = rjson[str(game['appid'])]['data']['price_overview']['initial'] * .01
  49. except KeyError:
  50. pass
  51. games[game['name']] = {'price': price, 'appid': game['appid']}
  52. return games