Skip to content

Instantly share code, notes, and snippets.

@jeffsdata
Last active June 9, 2021 15:39
Show Gist options
  • Save jeffsdata/f093af64eea79092003af70ef8f25c68 to your computer and use it in GitHub Desktop.
Save jeffsdata/f093af64eea79092003af70ef8f25c68 to your computer and use it in GitHub Desktop.
Call of Duty - Python API
import requests
import json
from datetime import date
####################################
##
## Call of Duty API
## -- Go to "SETUP" to fill in missing data (username, password, deviceID)
## -- Note: Only really tested using Modern Warfare (mw) and Warzone (wz) games on Python 3.7.4 on Windows/Anaconda.
##
## Calls
## -- Save Matches
## -- Save Profile
##
######################################
class CODAPI:
def __init__(self):
# SETUP: You have to enter values for these three items.
# User name and password is in plain text, that you'd use for my.callofduty.com.
self.userName = ""
self.password = ""
# DeviceID is a random string (20-30 characters)
self.deviceId = ""
self.baseAPIURL = "https://my.callofduty.com/api/papi-client/"
self.baseCookie = "new_SiteId=cod; ACT_SSO_LOCALE=en_US;country=US;XSRF-TOKEN=68e8b62e-1d9d-4ce1-b93f-cbe5ff31a041"
self.authObject = self.CreateAuth()
self.theWholeCookie = self.baseCookie + ";rtkn=" + self.authObject[0] + ";ACT_SSO_COOKIE=" + self.authObject[1] + ";atkn=" + self.authObject[2]
def CreateAuth(self):
device_url = "https://profile.callofduty.com/cod/mapp/registerDevice"
device_header = {'Content-Type': 'application/json',
"Cookie": self.baseCookie}
device_payload = "{\n\t\"deviceId\": \"" + self.deviceId + "\"\n}"
device_response = requests.request("POST", device_url, headers=device_header, data = device_payload)
device_data = json.loads(device_response.text)
authheader = ""
if(device_data["status"]=="success"):
authheader = device_data["data"]["authHeader"]
auth_url = "https://profile.callofduty.com/cod/mapp/login"
auth_header = {"x_cod_device_id": self.deviceId,
'Authorization': 'Bearer ' + authheader,
'Content-Type': 'application/json'}
auth_payload = "{\n\t\"email\": \"" + self.userName + "\", \"password\": \"" + self.password + "\"\n}"
auth_response = requests.request("POST", auth_url, headers=auth_header, data=auth_payload)
auth_data = json.loads(auth_response.text)
if(auth_data["success"]==True):
return(auth_data["rtkn"],auth_data["s_ACT_SSO_COOKIE"], auth_data["atkn"])
else:
print("An error occurred when trying to get the authorization cookie.")
else:
print("An error occurred when trying to create device.")
###
### MATCH HISTORY
### This function saves a file for each user for the g
###
def SavePlayerMatches(self, gamecode, gamemode, users):
today = date.today()
d1 = today.strftime("%Y%m%d")
for user in users:
print("Running..." + user[1])
url = self.baseAPIURL + "crm/cod/v2/title/" + gamecode + "/platform/" + user[2] + "/gamer/" + user[3] + "/matches/" + gamemode + "/start/0/end/0/details"
payload = {}
headers = {
"content-type": "application/json",
'Cookie': self.theWholeCookie,
'userAgent': 'Node/1.0.27'
}
response = requests.request("GET", url, headers=headers, data = payload)
response.encoding = 'utf-8'
responsetext = response.text
filename = d1 + "-warzone-matches-" + user[1].lower() + ".json"
with open(filename, "w", encoding="utf-8") as f:
f.write(responsetext)
def SavePlayerProfile(self, gamecode,gamemode,users):
###
### USER PROFILES
### Saves a json file for each user's profile.
###
today = date.today()
d1 = today.strftime("%Y%m%d")
for user in users:
print("Running..." + user[1])
url = self.baseAPIURL + "/stats/cod/v1/title/" + gamecode + "/platform/" + user[2] + "/gamer/" + user[3] + "/profile/type/" + gamemode
payload = {}
headers = {
"content-type": "application/json",
'Cookie': self.theWholeCookie,
'userAgent': 'Node/1.0.27'
}
response = requests.request("GET", url, headers=headers, data = payload)
response.encoding = 'utf-8'
responsetext = response.text
filename = d1 + "-warzone-profile-" + user[1].lower() + ".json"
with open(filename, "w", encoding="utf-8") as f:
f.write(responsetext)
def main():
### NOTES
### platform codes -- blizz battle.net: "battle", steam: "steam", playstation: "psn", xbox: "xbl", acti: "uno"
### game codes -- infiniteWarfare: "iw", worldWar2: "wwii", blackops3: "bo3", blackops4: "bo4", modernwarfare: "mw"
### game types -- zombies: "zm", multiplayer: "mp", warzone: "wz"
gamecode = "mw"
gamemode = "wz"
## List of users - each user has a first name, friendly user name (without numbers), platform code, full user name tuple
users = [
("Jeff","DocAsteria","battle","DocAsteria%231638")]
b = CODAPI()
b.SavePlayerMatches(gamecode,gamemode,users)
b.SavePlayerProfile(gamecode,gamemode,users)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment