A free sports API written for python
Project description
Development Status: This project is no longer undergoing active development. Please consider opening a pull request for any new features or bug fixes to be reviewed and merged.
sports is a free python API that pulls the stats from www.sports-reference.com and allows them to be easily be used in python-based applications, especially ones involving data analytics and machine learning.
sports exposes a plethora of sports information from major sports leagues in North America, such as the MLB, NBA, College Football and Basketball, NFL, and NHL. sports also now supports Professional Football (or Soccer) for thousands of teams from leagues around the world. Every sport has its own set of valid API queries ranging from the list of teams in a league, to the date and time of a game, to the total number of wins a team has secured during the season, and many, many more metrics that paint a more detailed picture of how a team has performed during a game or throughout a season.
NOTE (2022-11-23): The source website has implemented traffic restrictions that will result in your IP being temporarily blacklisted if you exceed 20 requests per minute. Therefore all outgoing requests are now passed through a utility function that ensures you will not surpass the rate limit threshold. This runtime slowdown is most noticeable for sports with many teams. If you are trying to bulk gather data you will need to schedule a long running job, and its recommended that you cache any historical data you intend to reuse.
Installation
TODO: This has not been released to Pypi yet, so standard pip install will not work. But I’m keepin the documentation as a template for now.
The easiest way to install pysports-stats is by downloading the latest released binary from PyPI using PIP. For instructions on installing PIP, visit PyPA.io for detailed steps on installing the package manager for your local environment.
Next, run:
pip install pysports-stats
to download and install the latest official release of pysports-stats on your machine. You now have the latest stable version of pysports-stats installed and can begin using it following the examples below!
If the bleeding-edge version of pysports-stats is desired, clone this repository using git and install all of the package requirements with PIP:
git clone https://github.com/bveber/pysports-stats cd pysports-stats poetry install
Once complete, create a Python wheel for your default version of Python by running the following command:
poetry build
This will create a .whl file in the dist directory which can be installed with the following command:
pip install dist/*.whl
Examples
The following are a few examples showcasing how easy it can be to collect an abundance of metrics and information from all of the tracked leagues. The examples below are only a miniscule subset of the total number of statistics that can be pulled using pysports-stats. TODO: Create documentation
Get instances of all NHL teams for the 2018 season
from sports.nhl.teams import Teams
teams = Teams(2018)
Print every NBA team’s name and abbreviation
from sports.nba.teams import Teams
teams = Teams()
for team in teams:
print(team.name, team.abbreviation)
Get a specific NFL team’s season information
from sports.nfl.teams import Teams
teams = Teams()
lions = teams('DET')
Print the date of every game for a NCAA Men’s Basketball team
from sports.ncaab.schedule import Schedule
purdue_schedule = Schedule('purdue')
for game in purdue_schedule:
print(game.date)
Print the number of interceptions by the away team in a NCAA Football game
from sports.ncaaf.boxscore import Boxscore
championship_game = Boxscore('2018-01-08-georgia')
print(championship_game.away_interceptions)
Get a Pandas DataFrame of all stats for a MLB game
from sports.mlb.boxscore import Boxscore
game = Boxscore('BOS201806070')
df = game.dataframe
Find the number of goals a football team has scored
from sports.fb.team import Team
tottenham = Team('Tottenham Hotspur')
print(tottenham.goals_scored)
Documentation
This repository is based on sportsipy. I am incredibly grateful for the work done by Robert Clark in creating this code base. Some internal components have been changed, but the basic interface remains the same. Two blog posts detailing the creation and basic usage of sportsipy can be found on The Medium at the following links:
The second post in particular is a great guide for getting started with pysports-stats and is highly recommended for anyone who is new to the package.
Testing
Pysports-stats contains a testing suite which aims to test all major portions of code for proper functionality. To run the test suite against your environment, ensure all of the requirements are installed by running:
pip install -r requirements.txt pip install coverage flexmock mock pycodestyle pytest pytest-cov pytest-xdist
Next, start the tests by running py.test while optionally including coverage flags which identify the amount of production code covered by the testing framework:
py.test --cov=sports --cov-report term-missing tests/
If the tests were successful, it will return a green line will show a message at the end of the output similar to the following:
======================= 752 passed in 158.03s (0:02:38) =======================
If a test failed, it will show the number of failed and what went wrong within the test output. If that’s the case, ensure you have the latest version of code and are in a supported environment. Otherwise, create an issue on GitHub to attempt to get the issue resolved.
NOTE (2022-11-23): Due to traffic restriction on the source webpage the exhaustive are unusable. They have been commented out for now, but should be replaced with a more sensible set of tests to validate connectivity.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pysports_stats-0.2.1.tar.gz
.
File metadata
- Download URL: pysports_stats-0.2.1.tar.gz
- Upload date:
- Size: 317.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.8.14 Linux/5.15.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b50268a96f4a24320c567bff025e3d013455f629d4693be2079cdf69756b05d8 |
|
MD5 | 0a164df48f5070dcecd068fba56c4070 |
|
BLAKE2b-256 | 92e11c6a7d71f40e275de8a0cf6444b6574fb2bcb5b11bdda7c5075a13ce477f |
File details
Details for the file pysports_stats-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: pysports_stats-0.2.1-py3-none-any.whl
- Upload date:
- Size: 349.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.8.14 Linux/5.15.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3fa42214491ad340d9c0fa339cfc3726adfb172cb319287f00d5a5cb87f9f8e |
|
MD5 | ffe6f4f9347e13843b52d65d7b1ffb4d |
|
BLAKE2b-256 | 2aea6972e28860455b79a80bef2b2eab22f7450f15aac7fa70df9cb42c837965 |