Code Monkey home page Code Monkey logo

sportsipy's Introduction

Sportsipy: A free sports API written for python

Started in early 2023, this project is an active fork of roclark/sportsipy, fixing basic issues related to site layout changes, python dependencies, and failing test cases. Note that not all existing functions work due to changes on the source site, but fixes will be integrated as they are identified.

Sportsipy 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.

Sportsipy 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. Sportsipy 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.

Important: This fork is not the reference on PyPI. To include this fork in your requirements:

pip install git+https://github.com/davidjkrause/sportsipy@master

If the bleeding-edge source version of sportsipy is desired, clone this repository using git and install all of the package requirements with PIP:

git clone https://github.com/davidjkrause/sportsipy
cd sportsipy
pip install -r requirements.txt

Once complete, create a Python wheel for your default version of Python by running the following command:

python setup.py sdist bdist_wheel

This will create a .whl file in the dist directory which can be installed with the following command:

pip install dist/*.whl

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 sportsipy. Visit the documentation on Read The Docs for a complete list of all information exposed by the API.

from sportsipy.nhl.teams import Teams

teams = Teams(2018)
from sportsipy.nba.teams import Teams

teams = Teams()
for team in teams:
    print(team.name, team.abbreviation)
from sportsipy.nfl.teams import Teams

teams = Teams()
lions = teams('DET')
from sportsipy.ncaab.schedule import Schedule

purdue_schedule = Schedule('purdue')
for game in purdue_schedule:
    print(game.date)
from sportsipy.ncaaf.boxscore import Boxscore

championship_game = Boxscore('2018-01-08-georgia')
print(championship_game.away_interceptions)
from sportsipy.mlb.boxscore import Boxscore

game = Boxscore('BOS201806070')
df = game.dataframe
from sportsipy.fb.team import Team

tottenham = Team('Tottenham Hotspur')
print(tottenham.goals_scored)

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 sportsipy and is highly recommended for anyone who is new to the package.

Complete documentation is hosted on readthedocs.org. Refer to the documentation for a full list of all metrics and information exposed by sportsipy. The documentation is auto-generated using Sphinx based on the docstrings in the sportsipy package.

Sportsipy 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

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=sportsipy --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:

======================= 380 passed in 245.56 seconds =======================

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.

sportsipy's People

Contributors

roclark avatar dependabot-preview[bot] avatar pyup-bot avatar davidjkrause avatar davidliugit avatar seanammirati avatar dependabot[bot] avatar ajhodges avatar agnijdave avatar aidanglickman avatar aleckeller avatar dwalton76 avatar l3str4nge avatar stiles avatar skcwillie avatar

Stargazers

Aneury Casado avatar Orhun avatar Brandon Smith avatar Jannik avatar Jorge Soto avatar Rishi Jindal avatar  avatar  avatar  avatar Craig avatar  avatar  avatar  avatar  avatar Jack Wagner avatar  avatar Markus Tapio avatar Sean Cooper avatar Dmitrii avatar Daryl avatar Michael Costanzo avatar  avatar Rogelio Torres avatar Edd Webster avatar Saiem Gilani avatar Daniel Easterman avatar Dan Morris avatar  avatar

Watchers

James Cloos avatar  avatar

sportsipy's Issues

Error getting Boxscore().dataframe

Describe the bug
Getting an error when trying to get a dataframe of a boxscore
Boxscore("202311170NOP").dataframe

Here is the full stack trace. Not sure whats going on here. Seems like the URI link is invalid?

TypeError                                 Traceback (most recent call last)
[/var/folders/t2/tydlk74s2hjf3xvc375z5fq40000gn/T/ipykernel_76390/888226579.py](https://file+.vscode-resource.vscode-cdn.net/var/folders/t2/tydlk74s2hjf3xvc375z5fq40000gn/T/ipykernel_76390/888226579.py) in <module>
----> 1 Boxscore("202311170NOP").dataframe

[~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/nba/boxscore.py](https://file+.vscode-resource.vscode-cdn.net/Users/jack11wagner/PersonalProjects/SportsBettingProject/NBA/~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/nba/boxscore.py) in dataframe(self)
    780             'home_wins': self.home_wins,
    781             'location': self.location,
--> 782             print("UR MOM")
    783             # 'losing_abbr': self.losing_abbr,
    784             # 'losing_name': self.losing_name,

[~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/nba/boxscore.py](https://file+.vscode-resource.vscode-cdn.net/Users/jack11wagner/PersonalProjects/SportsBettingProject/NBA/~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/nba/boxscore.py) in losing_abbr(self)
    881         for the Phoenix Suns.
    882         """
--> 883         if self.winner == HOME:
    884             return utils._parse_abbreviation(self._away_name)
    885         return utils._parse_abbreviation(self._home_name)

[~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/utils.py](https://file+.vscode-resource.vscode-cdn.net/Users/jack11wagner/PersonalProjects/SportsBettingProject/NBA/~/PersonalProjects/SportsBettingProject/venv/lib/python3.8/site-packages/sportsipy/utils.py) in _parse_abbreviation(uri_link)
    149         The shortened uppercase abbreviation for a given team.
    150     """
--> 151     abbr = re.sub(r'/[0-9]+\..*htm.*', '', uri_link('a').attr('href'))
    152     abbr = re.sub(r'/.*/schools/', '', abbr)
    153     abbr = re.sub(r'/teams/', '', abbr)
...
--> 210     return _compile(pattern, flags).sub(repl, string, count)
    211 
    212 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object

To Reproduce
Boxscore("202311170NOP").dataframe

Expected behavior

A Boxscore dataframe of the Boxscore id

Fetching NHL teams data incorrectly returns empty results

Describe the bug
Fetching NHL teams for any year returns empty results, even though the data exists in hockey-reference.com.

To Reproduce
Sample code which causes an issue.

from sportsipy.nhl.teams import Teams

teams = Teams(2023)
print(teams)

Expected behavior
I would expect a list of teams from 2023 to be printed.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Windows 11
  • Sportsipy Version: 0.6.0

Additional context
This happens using Python 3.11.6 and PyQuery 2.0.0.

My guess is that the problem is in utils.py _pull_page-method, line 320:

if url:
    return pq(url)
raise ValueError('Expected either a URL or a local data file!')

pq(url) seems to generate a PyQuery object with url as the only content, e.g. 'http://www.hockey-reference.com/leagues/NHL_2023.html'

Fix seems to only require changing to pq(url=url).

NBA - Wins Losses Games Played, etc.

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
I am looking for WINS LOSES GAMES PLAYED that are missing from NBA stats but present in NFL, MLB, NCAAB.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Add these elements to constants and other files as appropriate.
'games_played': 'td[data-stat="g"]:first',
'wins': 'td[data-stat="wins"]:first',
'losses': 'td[data-stat="losses"]:first',
'win_percentage': 'td[data-stat="win_loss_pct"]:first',

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Retrieving the data from games will require too many web requests.

Additional context
Add any other context or screenshots about the feature request here.

Add Coaches Poll rankings to NCAAF

Is your feature request related to a problem? Please describe.
Users should be able to pull the Coaches Poll rankings from the ncaaf.rankings module for each college football season, along with the AP and CFP rankings which are currently supported. The Coaches Poll has a level of popularity and prominence near that of the AP and CFP rankings, and therefore is likely to be desired by many users. If implemented, the ncaaf.rankings module would then support all three college football rankings available on sports-reference.com. These rankings should function similarly to the AP and CFP rankings which are already available.

Describe the solution you'd like
The rankings should be incorporated with the ncaaf.rankings module and be requested and structured similarly to how the AP and CFP rankings are currently handled. As the AP and CFP rankings are provided through separate classes, a third class for the Coaches poll would need to be added.

Describe alternatives you've considered
An alternative to pulling these three rankings using three near-duplicate classes would be to refactor the current code to construct one Rankings class and then have each of three classes for the separate polls inherit from it. Each of these subclasses would just need to use their own respective URLs and perhaps some unique HTML tags. This change would take more work up front, but may be easier to maintain down the road.

Additional context
A link to the 2023 Coaches poll can be found here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.