├── .gitignore ├── LICENSE ├── README.md ├── docker-compose.yml ├── requirements.txt ├── screenshot.jpg ├── src ├── api.py └── scraper │ ├── fide_scraper.py │ └── functions │ ├── __init__.py │ ├── player_history.py │ ├── player_info.py │ ├── top_players.py │ └── utils.py ├── start.sh └── vercel.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | .vercel 162 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 cassiofb-dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | FIDE API 3 |

4 | 5 |

Python FIDE scraper and HTTP API

6 | 7 |

8 | About • 9 | Features • 10 | Usage • 11 | Credits • 12 | License 13 |

14 | 15 | ![screenshot](screenshot.jpg) 16 | 17 | ## About 18 | 19 | Working with FIDE oficial data is not simple, mainly because they don't have an API. That's the reason I made a simple API with FastAPI to scrape the data from their own website and provide it as JSON over HTTP requests. 20 | 21 | ## Features 22 | 23 | Check it on: 24 | [https://fide-api.vercel.app/docs](https://fide-api.vercel.app/docs) 25 | 26 | - Get top players list 27 | - Get player info 28 | - Get player history 29 | 30 | ## Usage 31 | 32 | ### Docker (recommended) 33 | 34 | You will need docker and docker-compose installed, from your terminal: 35 | 36 | ```sh 37 | git clone https://github.com/cassiofb-dev/fide-api 38 | 39 | cd fide-api 40 | 41 | docker compose up -d 42 | ``` 43 | 44 | ### Native 45 | 46 | You will need git and python installed, from your terminal: 47 | 48 | ```sh 49 | git clone https://github.com/cassiofb-dev/fide-api 50 | 51 | cd fide-api 52 | 53 | python -m venv venv 54 | 55 | source venv/bin/activate 56 | 57 | pip install -r requirements.txt 58 | 59 | uvicorn src.api:app --reload 60 | ``` 61 | 62 | To see the docs go to ``localhost:8000/docs`` 63 | 64 | ## Credits 65 | 66 | This project uses git, python. 67 | 68 | The following python dependecies were used: 69 | ```txt 70 | annotated-types==0.7.0 71 | anyio==4.6.0 72 | beautifulsoup4==4.12.3 73 | certifi==2024.8.30 74 | charset-normalizer==3.3.2 75 | click==8.1.7 76 | fastapi==0.115.0 77 | h11==0.14.0 78 | idna==3.10 79 | orjson==3.10.7 80 | pydantic==2.9.2 81 | pydantic_core==2.23.4 82 | requests==2.32.3 83 | sniffio==1.3.1 84 | soupsieve==2.6 85 | starlette==0.38.6 86 | typing_extensions==4.12.2 87 | urllib3==2.2.3 88 | uvicorn==0.31.0 89 | ``` 90 | 91 | ## License 92 | 93 | MIT 94 | 95 | --- 96 | 97 | > [Website](https://cassiofernando.com)  ·  98 | > GitHub [@cassiofb-dev](https://github.com/cassiofb-dev)  ·  99 | > Twitter [@cassiofb_dev](https://twitter.com/cassiofb_dev) 100 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | fide-api: 3 | network_mode: "host" 4 | container_name: fide-api 5 | image: python:3.12-alpine3.20 6 | working_dir: "/fide-api" 7 | stop_grace_period: 1s 8 | command: sh -c "pip install -r requirements.txt && uvicorn src.api:app --reload" 9 | volumes: 10 | - ./:/fide-api 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | annotated-types==0.7.0 2 | anyio==4.6.0 3 | beautifulsoup4==4.12.3 4 | certifi==2024.8.30 5 | charset-normalizer==3.3.2 6 | click==8.1.7 7 | fastapi==0.115.0 8 | h11==0.14.0 9 | idna==3.10 10 | orjson==3.10.7 11 | pydantic==2.9.2 12 | pydantic_core==2.23.4 13 | requests==2.32.3 14 | sniffio==1.3.1 15 | soupsieve==2.6 16 | starlette==0.38.6 17 | typing_extensions==4.12.2 18 | urllib3==2.2.3 19 | uvicorn==0.31.0 20 | -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cassiofb-dev/fide-api/399c5b692b86cbb4cdd0ec6a71af3c33e47d458c/screenshot.jpg -------------------------------------------------------------------------------- /src/api.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | from fastapi import FastAPI 4 | from fastapi.responses import ORJSONResponse, RedirectResponse 5 | from fastapi.middleware.cors import CORSMiddleware 6 | 7 | from src.scraper import fide_scraper 8 | 9 | app = FastAPI(default_response_class=ORJSONResponse) 10 | 11 | app.add_middleware( 12 | CORSMiddleware, 13 | allow_origins=["*"], 14 | allow_credentials=True, 15 | allow_methods=["*"], 16 | allow_headers=["*"], 17 | ) 18 | 19 | @app.get("/") 20 | def home(): 21 | return RedirectResponse('/docs') 22 | 23 | @app.get("/top_players/") 24 | async def top_players(limit: int = 100, history: bool = False): 25 | response = fide_scraper.get_top_players(limit=limit, history=history) 26 | return response 27 | 28 | @app.get("/player_history/") 29 | async def player_history(fide_id: str): 30 | response = fide_scraper.get_player_history(fide_id=fide_id) 31 | return response 32 | 33 | @app.get("/player_info/") 34 | async def player_info(fide_id: str, history: bool = False): 35 | response = fide_scraper.get_player_info(fide_id=fide_id, history=history) 36 | return response 37 | -------------------------------------------------------------------------------- /src/scraper/fide_scraper.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import src.scraper.functions as scraper 3 | 4 | def get_top_players(limit: int = 100, history: bool = False) -> list[dict]: 5 | fide_top_players_page = requests.get("https://ratings.fide.com/a_top.php?list=open") 6 | 7 | html_doc = fide_top_players_page.text 8 | 9 | top_players = scraper.get_top_players(html_doc) 10 | 11 | top_players = top_players[0:limit] 12 | 13 | if history == False: return top_players 14 | 15 | for player_dict in top_players: 16 | fide_profile_page = f"https://ratings.fide.com/profile/{player_dict['fide_id']}" 17 | 18 | response = requests.get(fide_profile_page) 19 | 20 | html_doc = response.text 21 | 22 | player_history = scraper.get_player_history(html_doc) 23 | 24 | player_dict["history"] = player_history 25 | 26 | return top_players 27 | 28 | def get_player_history(fide_id: str) -> list[dict]: 29 | fide_profile_page = f"https://ratings.fide.com/profile/{fide_id}" 30 | 31 | response = requests.get(fide_profile_page) 32 | 33 | html_doc = response.text 34 | 35 | player_history = scraper.get_player_history(html_doc) 36 | 37 | return player_history 38 | 39 | def get_player_info(fide_id: str, history: bool = False): 40 | fide_profile_page = f"https://ratings.fide.com/profile/{fide_id}" 41 | 42 | response = requests.get(fide_profile_page) 43 | 44 | html_doc = response.text 45 | 46 | player_info = scraper.get_player_info(html_doc) 47 | 48 | if history == False: return player_info 49 | 50 | player_history = scraper.get_player_history(html_doc) 51 | 52 | player_info["history"] = player_history 53 | 54 | return player_info 55 | -------------------------------------------------------------------------------- /src/scraper/functions/__init__.py: -------------------------------------------------------------------------------- 1 | from src.scraper.functions.player_history import get_player_history 2 | from src.scraper.functions.player_info import get_player_info 3 | from src.scraper.functions.top_players import get_top_players 4 | -------------------------------------------------------------------------------- /src/scraper/functions/player_history.py: -------------------------------------------------------------------------------- 1 | from bs4 import BeautifulSoup 2 | 3 | from src.scraper.functions.utils import fide_date_to_numeric_string 4 | 5 | def get_player_history(html_doc: str): 6 | soup = BeautifulSoup(html_doc, "html.parser") 7 | 8 | table_selector = ".profile-table_calc > tbody:nth-child(2)" 9 | 10 | table = soup.select_one(table_selector) 11 | 12 | rows: list = table.find_all("tr") 13 | 14 | player_history = [] 15 | 16 | for row in rows: 17 | raw_row = [] 18 | 19 | for column in row.find_all("td"): 20 | raw_data = column.get_text().replace(u'\xa0', '').strip() 21 | 22 | raw_row.append(raw_data) 23 | 24 | player_history.append({ 25 | "period": raw_row[0], 26 | "classical_rating": int(raw_row[1] or 0), 27 | "classical_games": int(raw_row[2] or 0), 28 | "rapid_rating": int(raw_row[3] or 0), 29 | "rapid_games": int(raw_row[4] or 0), 30 | "blitz_rating": int(raw_row[5] or 0), 31 | "blitz_games": int(raw_row[6] or 0), 32 | "date": fide_date_to_numeric_string(raw_row[0]), 33 | }) 34 | 35 | return player_history 36 | -------------------------------------------------------------------------------- /src/scraper/functions/player_info.py: -------------------------------------------------------------------------------- 1 | from bs4 import BeautifulSoup, Tag 2 | 3 | def get_player_info(html_doc: str): 4 | soup = BeautifulSoup(html_doc, "html.parser") 5 | 6 | player_info_raw = { 7 | "fide_id": soup.select_one(".profile-info-id"), 8 | "fide_title": soup.select_one(".profile-info-title "), 9 | "federation": soup.select_one(".profile-info-country"), 10 | "birth_year": soup.select_one(".profile-info-byear"), 11 | "sex": soup.select_one(".profile-info-sex "), 12 | "name": soup.select_one(".player-title"), 13 | "world_rank_all": soup.select_one(".profile-rank-block:nth-of-type(1) .profile-rank-row:nth-of-type(2) p"), 14 | "world_rank_active": soup.select_one(".profile-rank-block:nth-of-type(1) .profile-rank-row:nth-of-type(1) p"), 15 | "continental_rank_all": soup.select_one(".profile-rank-block:nth-of-type(3) .profile-rank-row:nth-of-type(2) p"), 16 | "continental_rank_active": soup.select_one(".profile-rank-block:nth-of-type(3) .profile-rank-row:nth-of-type(1) p"), 17 | "national_rank_all": soup.select_one(".profile-rank-block:nth-of-type(2) .profile-rank-row:nth-of-type(2) p"), 18 | "national_rank_active": soup.select_one(".profile-rank-block:nth-of-type(2) .profile-rank-row:nth-of-type(1) p"), 19 | "classical_rating": soup.select_one(".profile-standart > p:nth-child(2)"), 20 | "rapid_rating": soup.select_one(".profile-rapid > p:nth-child(2)"), 21 | "blitz_rating": soup.select_one(".profile-blitz > p:nth-child(2)"), 22 | } 23 | 24 | player_info = { 25 | "fide_id": safely_get_string(player_info_raw["fide_id"]), 26 | "fide_title": safely_get_string(player_info_raw["fide_title"]), 27 | "federation": safely_get_string(player_info_raw["federation"]), 28 | "birth_year": safely_get_int(player_info_raw["birth_year"]), 29 | "sex": safely_get_string(player_info_raw["sex"]), 30 | "name": safely_get_string(player_info_raw["name"]), 31 | "world_rank_all": safely_get_int(player_info_raw["world_rank_all"]), 32 | "world_rank_active": safely_get_int(player_info_raw["world_rank_active"]), 33 | "continental_rank_all": safely_get_int(player_info_raw["continental_rank_all"]), 34 | "continental_rank_active": safely_get_int(player_info_raw["continental_rank_active"]), 35 | "national_rank_all": safely_get_int(player_info_raw["national_rank_all"]), 36 | "national_rank_active": safely_get_int(player_info_raw["national_rank_active"]), 37 | "classical_rating": safely_get_int(player_info_raw["classical_rating"]), 38 | "rapid_rating": safely_get_int(player_info_raw["rapid_rating"]), 39 | "blitz_rating": safely_get_int(player_info_raw["blitz_rating"]), 40 | } 41 | 42 | return player_info 43 | 44 | def safely_get_string(tag: Tag): 45 | if tag is None: 46 | return None 47 | 48 | return tag.get_text().strip() 49 | 50 | def safely_get_int(tag: Tag): 51 | if tag is None: 52 | return None 53 | 54 | if not tag.get_text().strip().isdigit(): 55 | return None 56 | 57 | return int(tag.get_text().strip()) 58 | -------------------------------------------------------------------------------- /src/scraper/functions/top_players.py: -------------------------------------------------------------------------------- 1 | from bs4 import BeautifulSoup 2 | 3 | def get_top_players(html_doc): 4 | soup = BeautifulSoup(html_doc, "html.parser") 5 | 6 | table_selector = ".top_recors_table" 7 | 8 | table = soup.select_one(table_selector) 9 | 10 | rows: list = table.find_all("tr") 11 | 12 | rows.pop(0) 13 | 14 | top_players = [] 15 | 16 | for row in rows: 17 | raw_row = [] 18 | 19 | for column in row.find_all("td"): 20 | raw_data = column.get_text().replace(u'\xa0', '') 21 | 22 | raw_row.append(raw_data) 23 | 24 | player_url = column.find("a") 25 | 26 | if player_url: raw_row.append(player_url["href"].split("/")[-1]) 27 | 28 | top_players.append({ 29 | "rank": raw_row[0], 30 | "name": raw_row[1], 31 | "fide_id": raw_row[2], 32 | "country": raw_row[3].strip(), 33 | "rating": raw_row[4], 34 | }) 35 | 36 | return top_players 37 | -------------------------------------------------------------------------------- /src/scraper/functions/utils.py: -------------------------------------------------------------------------------- 1 | import calendar 2 | 3 | month_abbr_to_number = {month: index for index, month in enumerate(calendar.month_abbr) if month} 4 | 5 | def fide_date_to_numeric_string(fide_date: str) -> str: 6 | year, month = fide_date.split("-") 7 | 8 | date_string = f"{year}-{month_abbr_to_number[month]:02}" 9 | 10 | return date_string 11 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | uvicorn src.api:app --reload 2 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "public": false, 4 | "builds": [{ "src": "src/api.py", "use": "@vercel/python" }], 5 | "routes": [ 6 | { "src": "/(.*)", "dest": "src/api.py" } 7 | ] 8 | } --------------------------------------------------------------------------------