xgclient


Namexgclient JSON
Version 0.1.10 PyPI version JSON
download
home_pagehttps://github.com/oRastor/xgclient
SummaryPython client for football (soccer) expected goals (xG) statistics API
upload_time2022-12-17 14:04:39
maintainer
docs_urlNone
authorOrest Bduzhak
requires_python
licenseMIT
keywords football soccer xg expected-goals
VCS
bugtrack_url
requirements requests marshmallow pandas
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python xG Client

Python client for [football (soccer) expected goals (xG) statistics API](https://rapidapi.com/Wolf1984/api/football-xg-statistics/).
It provides a list of events with xG metric for every game of more than 80 leagues.

## Usage

To install the latest version of `xgclient` use [pip](https://pypi.org/project/pip/).

```bash
pip install xgclient
```

## Example usage

Basic usage
```python
from xgclient.client import ExpectedGoalsClient

client = ExpectedGoalsClient('Your API Key')

countries = client.countries() # list of countries
tournaments = client.tournaments(country_id) # list of leagues for specified country
seasons = client.seasons(league_id) # list of seasons for specified league
fixtures = client.fixtures(season_id) # list of fixtures for specified season
fixture = client.fixture(fixture_id) # get one fixture
```

Calculating xg90 (expected goals for 90 minutes) metric for every team of available seasons 
```python
import operator
from xgclient.client import ExpectedGoalsClient

client = ExpectedGoalsClient('Your API key')

for country in client.countries():
    for tournament in client.tournaments(country['id']):
        for season in client.seasons(tournament['id']):
            print(country['name'], tournament['name'], season['name'])
            print('=====')

            season_fixtures = client.fixtures(season['id'])

            expected_goals = {}
            minutes = {}
            team_names = {}
            for fixture in season_fixtures:
                if not team_names.get(fixture['homeTeam']['id']):
                    team_names[fixture['homeTeam']['id']] = fixture['homeTeam']['name']
                    minutes[fixture['homeTeam']['id']] = 0

                if not team_names.get(fixture['awayTeam']['id']):
                    team_names[fixture['awayTeam']['id']] = fixture['awayTeam']['name']
                    minutes[fixture['awayTeam']['id']] = 0

                fixture_duration = fixture['duration']['firstHalf'] + fixture['duration']['secondHalf']

                minutes[fixture['homeTeam']['id']] += fixture_duration
                minutes[fixture['awayTeam']['id']] += fixture_duration

                for event in fixture['events']:
                    if not event['xg']:
                        continue

                    if not expected_goals.get(event['teamId']):
                        expected_goals[event['teamId']] = 0

                    expected_goals[event['teamId']] += event['xg']

            result = {}
            for team_id in expected_goals:
                result[team_id] = (expected_goals[team_id] / minutes[team_id]) * 90

            result = sorted(result.items(), key=operator.itemgetter(1), reverse=True)

            for team_id, value in result:
                print(team_names[team_id], value)

            print('')
```

Example Output:
```
England Premier League 2016/2017
=====
Manchester City 2.2112692731277535
Tottenham 2.052839403973515
Chelsea 1.826269731376351
Arsenal 1.799702725020647
Liverpool 1.69972352778546
Manchester Utd 1.6932413793103451
Southampton 1.439378453038676
Everton 1.3932328539823016
Bournemouth 1.2910729023383791
Stoke 1.2596034150371813
Leicester 1.212548156301597
West Ham 1.2049150684931513
Crystal Palace 1.1981870860927168
Swansea 1.0498671831765367
Burnley 0.9535088202866603
Watford 0.9309592061742009
West Brom 0.9158252695604089
Sunderland 0.9000000000000007
Hull 0.8362012717721877
Middlesbrough 0.6971943443304693

England Premier League 2017/2018
=====
Manchester City 2.398823204419891
Liverpool 1.871100993377485
Tottenham 1.8331631244824735
Arsenal 1.6883651452282165
Manchester Utd 1.5726460005535572
Chelsea 1.4510011061946915
Crystal Palace 1.403015741507872
Leicester 1.2518565517241396
Watford 1.1562657534246574
Everton 1.1204689655172415
Newcastle 1.0640897755610998
West Ham 1.0446826051112954
Bournemouth 0.9957362637362651
Brighton 0.9839266870313802
Southampton 0.9228472987872113
Stoke 0.8937382661512978
Burnley 0.8835910224438907
West Brom 0.8344257316399778
Swansea 0.7753942254303168
Huddersfield 0.7536753318584073
```

Pandas dataframe usage example

```python
from xgclient.client import ExpectedGoalsClient, create_fixtures_dataframe, create_events_dataframe, create_fixture_odds

client = ExpectedGoalsClient('Your API Key')

season_fixtures = client.fixtures(8202)
fixtures_df = create_fixtures_dataframe(season_fixtures)
events_df = create_events_dataframe(season_fixtures)
upcoming_odds_df = create_fixture_odds(client.upcoming_odds())
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/oRastor/xgclient",
    "name": "xgclient",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "football soccer xg expected-goals",
    "author": "Orest Bduzhak",
    "author_email": "doom4eg@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9a/7e/2c10900d9cab26c0c9adad155fcd5ae0361f36ab688c88388103176114ba/xgclient-0.1.10.tar.gz",
    "platform": null,
    "description": "# Python xG Client\n\nPython client for [football (soccer) expected goals (xG) statistics API](https://rapidapi.com/Wolf1984/api/football-xg-statistics/).\nIt provides a list of events with xG metric for every game of more than 80 leagues.\n\n## Usage\n\nTo install the latest version of `xgclient` use [pip](https://pypi.org/project/pip/).\n\n```bash\npip install xgclient\n```\n\n## Example usage\n\nBasic usage\n```python\nfrom xgclient.client import ExpectedGoalsClient\n\nclient = ExpectedGoalsClient('Your API Key')\n\ncountries = client.countries() # list of countries\ntournaments = client.tournaments(country_id) # list of leagues for specified country\nseasons = client.seasons(league_id) # list of seasons for specified league\nfixtures = client.fixtures(season_id) # list of fixtures for specified season\nfixture = client.fixture(fixture_id) # get one fixture\n```\n\nCalculating xg90 (expected goals for 90 minutes) metric for every team of available seasons \n```python\nimport operator\nfrom xgclient.client import ExpectedGoalsClient\n\nclient = ExpectedGoalsClient('Your API key')\n\nfor country in client.countries():\n    for tournament in client.tournaments(country['id']):\n        for season in client.seasons(tournament['id']):\n            print(country['name'], tournament['name'], season['name'])\n            print('=====')\n\n            season_fixtures = client.fixtures(season['id'])\n\n            expected_goals = {}\n            minutes = {}\n            team_names = {}\n            for fixture in season_fixtures:\n                if not team_names.get(fixture['homeTeam']['id']):\n                    team_names[fixture['homeTeam']['id']] = fixture['homeTeam']['name']\n                    minutes[fixture['homeTeam']['id']] = 0\n\n                if not team_names.get(fixture['awayTeam']['id']):\n                    team_names[fixture['awayTeam']['id']] = fixture['awayTeam']['name']\n                    minutes[fixture['awayTeam']['id']] = 0\n\n                fixture_duration = fixture['duration']['firstHalf'] + fixture['duration']['secondHalf']\n\n                minutes[fixture['homeTeam']['id']] += fixture_duration\n                minutes[fixture['awayTeam']['id']] += fixture_duration\n\n                for event in fixture['events']:\n                    if not event['xg']:\n                        continue\n\n                    if not expected_goals.get(event['teamId']):\n                        expected_goals[event['teamId']] = 0\n\n                    expected_goals[event['teamId']] += event['xg']\n\n            result = {}\n            for team_id in expected_goals:\n                result[team_id] = (expected_goals[team_id] / minutes[team_id]) * 90\n\n            result = sorted(result.items(), key=operator.itemgetter(1), reverse=True)\n\n            for team_id, value in result:\n                print(team_names[team_id], value)\n\n            print('')\n```\n\nExample Output:\n```\nEngland Premier League 2016/2017\n=====\nManchester City 2.2112692731277535\nTottenham 2.052839403973515\nChelsea 1.826269731376351\nArsenal 1.799702725020647\nLiverpool 1.69972352778546\nManchester Utd 1.6932413793103451\nSouthampton 1.439378453038676\nEverton 1.3932328539823016\nBournemouth 1.2910729023383791\nStoke 1.2596034150371813\nLeicester 1.212548156301597\nWest Ham 1.2049150684931513\nCrystal Palace 1.1981870860927168\nSwansea 1.0498671831765367\nBurnley 0.9535088202866603\nWatford 0.9309592061742009\nWest Brom 0.9158252695604089\nSunderland 0.9000000000000007\nHull 0.8362012717721877\nMiddlesbrough 0.6971943443304693\n\nEngland Premier League 2017/2018\n=====\nManchester City 2.398823204419891\nLiverpool 1.871100993377485\nTottenham 1.8331631244824735\nArsenal 1.6883651452282165\nManchester Utd 1.5726460005535572\nChelsea 1.4510011061946915\nCrystal Palace 1.403015741507872\nLeicester 1.2518565517241396\nWatford 1.1562657534246574\nEverton 1.1204689655172415\nNewcastle 1.0640897755610998\nWest Ham 1.0446826051112954\nBournemouth 0.9957362637362651\nBrighton 0.9839266870313802\nSouthampton 0.9228472987872113\nStoke 0.8937382661512978\nBurnley 0.8835910224438907\nWest Brom 0.8344257316399778\nSwansea 0.7753942254303168\nHuddersfield 0.7536753318584073\n```\n\nPandas dataframe usage example\n\n```python\nfrom xgclient.client import ExpectedGoalsClient, create_fixtures_dataframe, create_events_dataframe, create_fixture_odds\n\nclient = ExpectedGoalsClient('Your API Key')\n\nseason_fixtures = client.fixtures(8202)\nfixtures_df = create_fixtures_dataframe(season_fixtures)\nevents_df = create_events_dataframe(season_fixtures)\nupcoming_odds_df = create_fixture_odds(client.upcoming_odds())\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python client for football (soccer) expected goals (xG) statistics API",
    "version": "0.1.10",
    "split_keywords": [
        "football",
        "soccer",
        "xg",
        "expected-goals"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "71e2828795bbd2566c8aae19906c4dc9",
                "sha256": "ff4a0df4ed73d4f1c1de05fbb177da0577a449087c3c5dd2b8baab14cd23971c"
            },
            "downloads": -1,
            "filename": "xgclient-0.1.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "71e2828795bbd2566c8aae19906c4dc9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5044,
            "upload_time": "2022-12-17T14:04:37",
            "upload_time_iso_8601": "2022-12-17T14:04:37.476924Z",
            "url": "https://files.pythonhosted.org/packages/fa/7e/0ef3871f065c973929b6df477f0c9402302b85825e6ad10617d87deda89a/xgclient-0.1.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "7b291c19412d8c149902cc7b788f43e2",
                "sha256": "2f9d668c2281f553b2dc4b009b331c43bb4741a2efd333f1de11895f5b906551"
            },
            "downloads": -1,
            "filename": "xgclient-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "7b291c19412d8c149902cc7b788f43e2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5026,
            "upload_time": "2022-12-17T14:04:39",
            "upload_time_iso_8601": "2022-12-17T14:04:39.293324Z",
            "url": "https://files.pythonhosted.org/packages/9a/7e/2c10900d9cab26c0c9adad155fcd5ae0361f36ab688c88388103176114ba/xgclient-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-17 14:04:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "oRastor",
    "github_project": "xgclient",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "requests",
            "specs": [
                [
                    "~=",
                    "2.28.0"
                ]
            ]
        },
        {
            "name": "marshmallow",
            "specs": [
                [
                    "~=",
                    "3.16.0"
                ]
            ]
        },
        {
            "name": "pandas",
            "specs": [
                [
                    "~=",
                    "1.3.5"
                ]
            ]
        }
    ],
    "lcname": "xgclient"
}
        
Elapsed time: 0.02067s