Skip to content

Instantly share code, notes, and snippets.

@Membear
Last active October 21, 2020 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Membear/9f5a07805bd7843bf40e21f0a412a18c to your computer and use it in GitHub Desktop.
Save Membear/9f5a07805bd7843bf40e21f0a412a18c to your computer and use it in GitHub Desktop.
CoD API demonstration
; for testing enter /set %username USERNAMEHERE
; for testing enter /set %password PASSHERE
;
; /cod.stats.show Ancientswede#2802
; /cod.latest Ancientswede#2802
; /cod.latest.team Ancientswede#2802
;
alias CoD.Debug { return $true }
alias CoD.Defaults {
if ($1 == mode) { return wz }
if ($1 == title) { return mw }
if ($1 == platform) { return battle }
}
;;--------------------------------------;;
;; User Commands ;;
;;--------------------------------------;;
;; /CoD.Stats.Show @Username [@Mode] [@Title] [@Platform]
alias CoD.Stats.Show {
var %stats = $$CoD.Stats($1,$2,$3,$4)
echo -ag Username: $json(%stats,data,username).value
echo -ag Level: $json(%stats,data,level).value
echo -ag Kills: $json(%stats,data,lifetime,all,properties,kills).value
}
;; /CoD.Latest @Username
alias CoD.Latest {
var %matches = $$CoD.Matches($1,$2,$3,$4)
var %match = $json(%matches,data,matches,0)
var %username = $json(%match,player,username).value
var %mode = $CoD.Enum($json(%match,mode).value)
var %place = $ord($json(%match,playerStats,teamPlacement).value)
var %kills = $json(%match,playerStats,kills).value
var %deaths = $json(%match,playerStats,deaths).value
var %damage = $json(%match,playerStats,damageDone).value
echo -ag %mode $iif(%place,- %place)
echo -ag Kills: %kills Deaths: %deaths Damage: %damage
return %match
}
;; /CoD.Latest.Team @Username
alias CoD.Latest.Team {
var %username = $$1, %mode = $iif($2,$2,$CoD.Defaults(mode)), %title = $iif($3,$3,$CoD.Defaults(title)), %platform = $iif($4,$4,$CoD.Defaults(platform))
var %matches = $CoD.Matches($1,$2,$3,$4)
var %match = $json(%matches,data,matches,0)
var %matchID = $json(%match,matchID).value
var %team = $json(%match,player,team).value
var %mode = $CoD.Enum($json(%match,mode).value)
var %place = $ord($json(%match,playerStats,teamPlacement).value)
var %details = $CoD.Match.Details(%matchID)
echo -ag %mode $iif(%place,- %place)
echo -ag                 Kills  |  Deaths  |  Damage
var %players = $json(%details,data,allPlayers)
noop $JSONForEach(%players, match.details.worker %team)
}
alias match.details.worker {
if ($json($2,player,team).value != $1) { return }
var %username = $json($2,player,username).value
var %p1 = 13 - $len(%username)
var %kills = $json($2,playerStats,kills).value
var %p2 = 2 - $len(%kills)
var %deaths = $json($2,playerStats,deaths).value
var %p3 = 2 - $len(%deaths)
var %damage = $json($2,playerStats,damageDone).value
var %p4 = 5 - $len(%damage)
echo -ag $+(%username,$str( ,%p1),|    ,$str( ,%p2),%kills,   |    ,$str( ,%p3),%deaths,    |  ,$str( ,%p4),%damage)
}
;;--------------------------------------;;
;; Public Routes ;;
;;--------------------------------------;;
;; $Cod.Match.Details(@MatchID, @Mode, @Title, @Platform)
;; Fetch match details
;;
;; Returns - json reference
;;
;; @Mode - wz|mp
;; @Title - mw|wwii|bo4
;; @Platform - battle|psn|steam|uno|xbl
;;
alias CoD.Match.Details {
var %matchID = $$1, %mode = $iif($2,$2,$CoD.Defaults(mode)), %title = $iif($3,$3,$CoD.Defaults(title)), %platform = $iif($4,$4,$CoD.Defaults(platform))
var %url = https://my.callofduty.com/api/papi-client/crm/cod/v2/title/ $+ %title $+ /platform/ $+ %platform $+ /fullMatch/ $+ %mode $+ / $+ %matchID $+ /en
return $CoD.Call(%url)
}
;;--------------------------------------;;
;; Protected Routes ;;
;;--------------------------------------;;
;; $Cod.Stats(@Username, @Mode, @Title, @Platform)
;; Fetch user stats
;;
;; Returns - json reference
;;
;; @Mode - wzw|mp
;; @Title - mw|wwii|bo4
;; @Platform - battle|psn|steam|uno|xbl
;;
alias CoD.Stats {
var %username = $$1, %mode = $iif($2,$2,$CoD.Defaults(mode)), %title = $iif($3,$3,$CoD.Defaults(title)), %platform = $iif($4,$4,$CoD.Defaults(platform))
var %url = https://my.callofduty.com/api/papi-client/stats/cod/v1/title/ $+ %title $+ /platform/ $+ %platform $+ /gamer/ $+ %username $+ /profile/type/ $+ %mode
return $CoD.Call(%url)
}
;; $CoD.Matches(@Username, @Mode, @Title, @Platform)
;; Fetch match history
;;
;; Returns - json reference
;;
;; @Mode - wz|mp
;; @Title - mw|wwii|bo4
;; @Platform - battle|psn|steam|uno|xbl
;;
alias CoD.Matches {
var %username = $$1, %mode = $iif($2,$2,$CoD.Defaults(mode)), %title = $iif($3,$3,$CoD.Defaults(title)), %platform = $iif($4,$4,$CoD.Defaults(platform))
var %url = https://my.callofduty.com/api/papi-client/crm/cod/v2/title/ $+ %title $+ /platform/ $+ %platform $+ /gamer/ $+ %username $+ /matches/ $+ %mode $+ /start/0/end/0/details
return $CoD.Call(%url)
}
;;--------------------------------------;;
;; Private Routes ;;
;;--------------------------------------;;
alias CoD.Identity {
var %url = https://www.callofduty.com/api/papi-client/crm/cod/v2/identities
var %xhr = $CoD.Call(%url)
if ($CoD.Debug) echo -ag $json(%xhr).HttpBody
return %xhr
}
;; $CoD.Platforms(@Username,@Platform)
;;
;; @Platform - battle|psn|steam|uno|xbl
;;
alias CoD.Platforms {
var %username = $$1, %platform = $iif($2,$2,$CoD.Defaults(platform))
var %url = https://my.callofduty.com/api/papi-client/crm/cod/v2/accounts/platform/ $+ %platform $+ /gamer/ $+ %username
var %xhr = $CoD.Call(%url)
if ($CoD.Debug) echo -ag $json(%xhr).HttpBody
return %xhr
}
alias CoD.Friends {
var %url = https://my.callofduty.com/api/papi-client/codfriends/v1/compendium
var %xhr = $CoD.Call(%url)
if ($CoD.Debug) echo -ag $json(%xhr).HttpBody
return %xhr
}
;;--------------------------------------;;
;; HTTP Interface ;;
;;--------------------------------------;;
alias CoD.Call {
:retry
var %xhr = CoD. $+ $rand(10000,99999) $+ $ticks, %cookies
var %url = $1
%cookies = $$CoD.Cookies
JSONOpen -duw %xhr %url
JSONHttpHeader %xhr Cookie %cookies
JSONHttpFetch %xhr
if ($json(%xhr,status).value == error) && ($json(%xhr,data,message).value == Not permitted: not authenticated) {
hfree -w cod
if (!%retry) {
var %retry = 1
goto retry
}
}
if ($Cod.Debug) url $replace(%url,$chr(35),% $+ 23)
return %xhr
}
alias CoD.ProcessResponse {
var %headers = $json($$1).HttpHeaders
if ($CoD.Debug) echo -ag -
if ($CoD.Debug) echo 45 -ag headers: %headers
if ($CoD.Debug) echo -ag -
if ($regex($JSON($1).HttpHeaders,/Set-Cookie: XSRF-TOKEN=(.+?);/)) {
hadd -m cod token $regml(1)
}
if ($regex($JSON($1).HttpHeaders,/Set-Cookie: atkn=([^;]+?);\s*Expires=([^;]+?);/g)) {
var %atkn $regmlex($v1,1)
var %expires = $calc($ctime($regmlex($v1,2)) - $ctime - 600)
if (%expires > 0) {
hadd -mu $+ %expires cod atkn %atkn
}
elseif ($hget(cod)) { hdel cod atkn }
}
var %match = $regex(%headers,/Set-Cookie: (.+?;) (.+)/g)
if ($CoD.Debug) echo 7 -ag %match
var %i = 0, %n = %match
while (%i < %n) {
inc %i
if (HttpOnly isin $regmlex(%i,2)) { continue }
if ($regmlex(%i,1) isin %cookies) { continue }
if ($CoD.Debug) echo 3 -ag %i :: $regmlex(%i,1)
if ($CoD.Debug) echo 4 -ag %i :: $regmlex(%i,2)
var %cookies = %cookies $regmlex(%i,1)
}
hadd -m cod cookies %cookies
if ($CoD.Debug) echo -ag -
if ($CoD.Debug) echo -ag 3 $+ %cookies
}
;;--------------------------------------;;
;; Authentication ;;
;;--------------------------------------;;
alias CoD.Cookies {
if ($hget(cod,cookies)) { return $v1 }
if ($1 != auth.login) { Cod.Login }
return $hget(cod,cookies)
}
alias CoD.Token {
if ($hget(cod,token)) return $v1
var %xhr = CoD. $+ $rand(10000,99999) $+ $ticks
var %url = https://profile.callofduty.com/cod/login
JSONOpen -dUw %xhr %url
JSONHttpMethod %xhr HEAD
JSONHttpFetch %xhr
CoD.ProcessResponse %xhr
return $hget(cod,token)
}
alias CoD.Login {
var %xhr = CoD. $+ $rand(10000,99999) $+ $ticks
var %url = https://profile.callofduty.com/do_login?new_SiteId=cod
var %token = $$CoD.Token
var %cookies = $$Cod.Cookies(auth.login)
var %data = username= $+ %username $+ &password= $+ $urlencode(%password) $+ &remember_me=true&_csrf= $+ %token
if ($CoD.Debug) echo -ag token: %token
if ($CoD.Debug) echo -ag cookies: %cookies
if ($CoD.Debug) echo -ag data: %data
if ($CoD.Debug) echo -ag len: $len(%data)
if ($CoD.Debug) echo 4 -ag @@@@@@
JSONOpen -dURw %xhr %url
JSONHttpMethod %xhr POST
JSONHttpHeader %xhr Cookie %cookies
JSONHttpFetch %xhr %data
CoD.ProcessResponse %xhr
if ($CoD.Debug) echo 12 -ag Status: $json(%xhr).HttpStatus
if ($CoD.Debug) echo 12 -ag Head: $json(%xhr).HttpHead
var %bvar = $json(%xhr).HttpBodyToBvar
if ($bvar(%bvar,0)) { echo 10 -ag $bvar(%bvar,1-3000).text }
}
;;-------------------------------------;;
;; Utility ;;
;;-------------------------------------;;
alias -l urlencode return $regsubex($1-,/([^A-Z0-9._\-])/gi,$+(%,$base($asc(\1),10,16)))
alias CoD.Enum {
if ($1 == br_brquads) { return BR Quads }
if ($1 == br_brtrios) { return BR Trios }
if ($1 == br_brduos) { return BR Duos }
if ($1 == br_brsolo) { return BR Solo }
if ($1 == br_dmz_plnbld) { return Plunder: Blood Money }
if ($1 == br_brhwntrios) { return BR Trick-Or-Trios }
if ($1 == br_zxp_zmbroy) { return Zombies Royale }
return $1-
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment