Skip to content

A small script to parse the riot api documentation to generate json schema file, which get converted to pydantic models aka python dataclasses. For an eay and fast prototyping of api wrappers.

Notifications You must be signed in to change notification settings

Plutokekz/RiotApiParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

RiotApiParser

A small script to parse the riot api documentation to generate json schema file, which get converted to pydantic models aka python dataclasses. For an eay and fast prototyping of api wrappers.

Installation

clone or downloade the repository and install the requirements

cd RiotApiParser
pip install -r requirements.txt

Run

python main.py -h
usage: main.py [-h] [-p PARSER] [-jp JSON_PATH] [-pp PYTHON_PATH] [-u URL]

optional arguments:
  -h, --help            show this help message and exit
  -p PARSER, --parser PARSER
                        select a parser for bs4, default: lxml
  -jp JSON_PATH, --jsonpath JSON_PATH
                        Path to store the json schema file, default: models
  -pp PYTHON_PATH, --pythonpath PYTHON_PATH
                        Path to store the python file, default: python
  -u URL, --url URL     url to riot developers page with the api documentation, default: https://developer.riotgames.com/apis
python main.py

If the script run's with the default arguments, you got two folder models and python. In the models' directory is the json schema located and in the python directory the generated python files.

Example

Parsing the website entry to json schema

{
  "title": "AccountDto",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "puuid": {
      "type": "string"
    },
    "gameName": {
      "type": "string",
      "description": "This field may be excluded from the response if the account doesn't have a gameName."
    },
    "tagLine": {
      "type": "string",
      "description": "This field may be excluded from the response if the account doesn't have a tagLine."
    }
  }
}

Generating python code from the json schema

# generated by datamodel-codegen:
#   filename:  AccountDto.json
#   timestamp: 2022-04-11T09:10:43+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class AccountDto(BaseModel):
    puuid: Optional[str] = None
    gameName: Optional[str] = Field(
        None,
        description="This field may be excluded from the response if the account doesn't have a gameName.",
    )
    tagLine: Optional[str] = Field(
        None,
        description="This field may be excluded from the response if the account doesn't have a tagLine.",
    )

About

A small script to parse the riot api documentation to generate json schema file, which get converted to pydantic models aka python dataclasses. For an eay and fast prototyping of api wrappers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages