battlelog.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import urllib2
  2. import json
  3. import bf3api
  4. usernameDict = {}
  5. #fact options: accuracy, skillchange, kills, kd
  6. def getLatestGameFact(api, name, fact):
  7. gameData = getLatestGameData(api, name)
  8. if fact in gameData:
  9. return gameData[fact]
  10. else:
  11. return "not available"
  12. def getLatestGameData(api, name):
  13. username = getusername(api, name)
  14. latestGameToken = getRecentGameTokens(username)[0]
  15. gameData = getGameData(latestGameToken)
  16. return gameData
  17. def getGameData(gameToken):
  18. baseurl = "http://battlelog.battlefield.com/bf3/battlereport/loadplayerreport"
  19. url = baseurl + "/" + gameToken[0] + "/2/" + gameToken[1]
  20. con = urllib2.urlopen(url)
  21. result = con.read()
  22. con.close()
  23. raw = json.loads(result)
  24. idat = raw["playerReport"]["intData"]
  25. fdat = raw["playerReport"]["floatData"]
  26. data = {}
  27. #if you got no kills, the data will be completely missing, so be careful here.
  28. if "c___shw_g" in idat and "c___sfw_g" in idat:
  29. data["accuracy"] = float(idat["c___shw_g"]) / float(idat["c___sfw_g"])
  30. if "elo" in fdat:
  31. data["skillchange"] = float(fdat["elo"])
  32. if "c___k_g" in idat:
  33. data["kills"] = int(idat["c___k_g"])
  34. if "c___d_g" in idat:
  35. data["kd"] = float(idat["c___k_g"]) / float(idat["c___d_g"])
  36. else:
  37. data["kd"] = float(idat["c___k_g"])
  38. return data
  39. def getRecentGameTokens(username):
  40. url = "http://battlelog.battlefield.com/bf3/user/" + username
  41. headers = {'X-AjaxNavigation': 1}
  42. req = urllib2.Request(url, None, headers)
  43. con = urllib2.urlopen(req)
  44. result = con.read()
  45. con.close()
  46. raw_data = json.loads(result)
  47. group = raw_data["context"]["gameReportPreviewGroups"]
  48. #The data coming from battlelog is weird. There's a list of lists, and the
  49. #placement and length of inner lists is seemingly unpredictable.
  50. flatlist = []
  51. for outerindex in range(0, len(group)):
  52. for innerindex in range(0, len(group[outerindex])):
  53. flatlist.append(group[outerindex][innerindex])
  54. data = []
  55. for i in range(0, len(flatlist)):
  56. token = (flatlist[i]["reportId"], flatlist[i]["personaId"])
  57. data.append(token)
  58. return data
  59. def getusername(api, name):
  60. global usernameDict
  61. if name in usernameDict:
  62. return usernameDict[name]
  63. data = api.player(name, None, "clear,urls")
  64. statsurl = data.battlelog
  65. headers = {'X-AjaxNavigation': 1}
  66. req = urllib2.Request(statsurl, None, headers)
  67. con = urllib2.urlopen(req)
  68. result = con.read()
  69. con.close()
  70. raw_data = json.loads(result)
  71. username = raw_data["context"]["profileCommon"]["user"]["username"]
  72. usernameDict[name] = username
  73. return username