Skip to main content

wzlight

MIT License made-with-python

wzlight is an asynchronous Python wrapper for the Call of Duty API that focuses on Warzone endpoints.

Features

  • Asynchronous with help of HTTPX, the HTTP client library
  • Light : only centered around the few GET methods to collect Warzone stats
  • Handle SSO auth. the (now) only available way to connect to the API

Installation

  # with pip
  pip install wzlight
  # with Poetry
  poetry add wzlight

Client usage

import os
import asyncio
from pprint import pprint

import httpx
from dotenv import load_dotenv

from wzlight import Api


async def main():

    load_dotenv()
    sso = os.environ["SSO"]
    username = "amadevs#1689"
    platform = "battle"

    # Initialize Api with a SSO token
    # SSO token can be found in browser while login-in to callofduty.com

    api = Api(sso)

    # Wrapper internal methods are using the httpx async HTTP library

    # Initially httpx.AsyncClient) was built in its own cls, but letting users define it at app-level --as a context manager,
    # ensures more robustness and flexibility (custom timeout, limits...) and prevent async loop event errors.
    # Thus, your main() should follow the preferred approach while working with an async http client:

    async with httpx.AsyncClient() as httpxClient:

        profile = await api.GetProfile(httpxClient, platform, username)
        pprint(profile, depth=2)

        recent_matches = await api.GetRecentMatches(httpxClient, platform, username)
        recent_matches_short = [match for match in recent_matches[:2]]
        pprint(recent_matches_short, depth=3)

        matchStats = await api.GetMatch(
            httpxClient, platform, matchId=11378702801403672847
        )
        pprint(matchStats, depth=1)


if __name__ == "__main__":
    asyncio.run(main())

Focus : example of a concurrent run

import os
import asyncio
from pprint import pprint

import httpx
from dotenv import load_dotenv
from typing import AsyncContextManager

from wzlight import Api

async def main():

    load_dotenv()
    sso = os.environ["SSO"]
    username = "amadevs#1689"
    platform = "battle"

    # Initialize Api with a SSO token

    api = Api(sso)

    # We're running concurrently on match details endpoint, given a list of matchIds
    # COD API is unofficial and undocumented, rate limits / restrictions are not known
    # So you should probably --at least, handle some concurrency limits e.g for fetching a batch of matchs
    # See. httpx.Limits, async.Semaphore or other librairies such as aiometer


    matchIds = [
        5850171651963062771,
        6910618934945378397,
        16975576559940046894,
        639235311963231866,
        11887968911271282782,
        7897970481732864368,
    ]

    # enclose api.getMatch into safe_GetMatch that accept a Semaphore threshold arg.

    limit = asyncio.Semaphore(2)

    async def safe_GetMatch(httpxClient, platform, matchId, sema: AsyncContextManager):
        # No more than two concurrent tasks. If reaches two, wait 1 sec
        async with sema:
            r = await api.GetMatch(httpxClient, platform, matchId)
            if sema.locked():
                print("Concurrency limit reached, waiting ...")
                await asyncio.sleep(1)
            return r

    # Do not forget to open Client as context manager :
    async with httpx.AsyncClient() as httpxClient:
	
        sema = asyncio.Semaphore(2)
        
        tasks = []
        for matchId in matchIds:
            tasks.append(safe_GetMatch(httpxClient, platform, matchId, sema))
        results = await asyncio.gather(*tasks, return_exceptions=True)
        return resultss
            
    print(len(results))


if __name__ == "__main__":
    asyncio.run(main())

Acknowledgements

Love
Inspiration (heavily) came from :
Also check those links if your need documentation on how the API works

  • EthanC/CallofDuty.py : the most complete but now slightly deprecated (mainly the Auth.), async COD client (lot of exploited endpoints and methods + more than WZ)
  • Lierrmm/Node-CallOfDuty : very clean async. wrapper written in NodeJS. Also check their Discord to get a grip on API subtleties and unofficial changes (privacy changes, rate limits etc)
  • valtov/WarzoneStats : very clean synch. Python wrapper by the creator of wzstats.gg

License

MIT

Metadata

Release files for wzlight 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wzlight 0.1.5
File Size Uploaded
wzlight-0.1.5.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wzlight 0.1.5
File Interpreter ABI Platform
wzlight-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / wzlight-0.1.5.tar.gz

Download URL wzlight-0.1.5.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
23b9043b1bfa9fe5147e2d2282534fc170581740ca96990a0f97c6980d848816
BLAKE2b-256 checksum
How to use checksums
55ffc8d68619a2f6a0e69c70de4f563e67b0ea27e4eff07aeafd2e884815facf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.4 Linux/5.19.0-76051900-generic

Release files / wzlight-0.1.5-py3-none-any.whl

Download URL wzlight-0.1.5-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdcb2a6fc1a1009d239c4772372bba6e240e2ee1035da4c6a19a6f8d7964438c
BLAKE2b-256 checksum
How to use checksums
68a5ee883e32403d47e709c85d9e11310317de026d1a2c28dc9dd18d0c66667b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.4 Linux/5.19.0-76051900-generic

Release history Release notifications | RSS feed

0.1.33

2 release files

0.1.32

2 release files

0.1.31

2 release files

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page