Skip to content

Commit e115724

Browse files
authored
Merge pull request #35 from jaebradley/v2.0.1
Fix issue with contest response translator and updates documentation. This is a backwards-incompatible change but it was done to fix a bug with contest response being a list instead of a list of lists, hence the `patch` upgrade instead of a major version upgrade. This also adds a `sport` field to the contest draft group response translation.
2 parents 9c4f5b2 + 5f4744d commit e115724

File tree

7 files changed

+77
-47
lines changed

7 files changed

+77
-47
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# DraftKings Python Client
22

3+
[![Build Status](https://travis-ci.org/jaebradley/draftkings_client.svg?branch=master)](https://travis-ci.org/jaebradley/draftkings_client)
4+
[![codecov](https://codecov.io/gh/jaebradley/draftkings_client/branch/master/graph/badge.svg)](https://codecov.io/gh/jaebradley/draftkings_client)
5+
![PyPI](https://img.shields.io/pypi/v/draft_kings.svg)
6+
37
## Introduction
48
DraftKings does not have a public API with documentation.
59

@@ -8,8 +12,14 @@ DraftKings - data like NFL contests, or players available for a
812
"Draft Group" (e.g. all NBA games starting at 7 PM EST tonight), along
913
with relevant metadata.
1014

15+
As DraftKings makes no guarantees about it's public API, this client makes no guarantees that existing API methods
16+
will work consistently.
17+
1118
## Install using PyPi
12-
`pip install draft_kings`
19+
20+
```bash
21+
pip install draft_kings
22+
```
1323

1424
## API
1525

@@ -26,15 +36,15 @@ contests(sport=Sport.nba)
2636
```python
2737
from draft_kings.client import available_players
2838

29-
return available_players(draft_group_id=1)
39+
available_players(draft_group_id=1)
3040
```
3141

3242
### Get Draft Group Details
3343

3444
```python
3545
from draft_kings.client import draft_group_details
3646

37-
return draft_group_details(draft_group_id=1)
47+
draft_group_details(draft_group_id=1)
3848
```
3949

4050
### Get Countries
@@ -44,7 +54,7 @@ Get all country information that DraftKings uses to make country-specific reques
4454
```python
4555
from draft_kings.client import countries
4656

47-
return countries()
57+
countries()
4858
```
4959

5060
### Get Regions
@@ -54,7 +64,7 @@ Get all region information for the specified country code that DraftKings uses t
5464
```python
5565
from draft_kings.client import regions
5666

57-
return regions(country_code='US')
67+
regions(country_code='US')
5868
```
5969

6070

@@ -65,5 +75,5 @@ Get all draftable players
6575
```python
6676
from draft_kings.client import draftables
6777

68-
return draftables(draft_group_id=1)
78+
draftables(draft_group_id=1)
6979
```

draft_kings/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
from draft_kings.data import Sport

draft_kings/client.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
11
import requests
22

33
from draft_kings import urls
4-
from draft_kings.data import Sport
54
from draft_kings.response_translators import translate_players, translate_contests, translate_countries, \
6-
translate_draft_group, translate_regions, translate_draftables
7-
8-
"""
9-
The API takes a sport query parameter that's different than then sports API endpoint
10-
"""
11-
SPORT_TO_CONTESTS_QUERY_PARAMETER = {
12-
Sport.NFL: "NFL",
13-
Sport.NHL: "NHL",
14-
Sport.NBA: "NBA",
15-
Sport.CFL: "CFL",
16-
Sport.COLLEGE_FOOTBALL: "CFB",
17-
Sport.MIXED_MARTIAL_ARTS: "MMA",
18-
Sport.NASCAR: "NAS",
19-
Sport.SOCCER: "SOC",
20-
Sport.EUROLEAGUE_BASKETBALL: "EL",
21-
Sport.MLB: "MLB",
22-
Sport.TENNIS: "TEN",
23-
Sport.LEAGUE_OF_LEGENDS: "LOL",
24-
Sport.GOLF: "GOLF",
25-
Sport.COLLEGE_BASKETBALL: "CBB"
26-
}
5+
translate_draft_group, translate_regions, translate_draftables, SPORT_TO_CONTESTS_ABBREVIATION
276

287

298
def contests(sport):
309
response = requests.get(url=urls.CONTESTS_URL,
31-
params={'sport': SPORT_TO_CONTESTS_QUERY_PARAMETER[sport]})
10+
params={'sport': SPORT_TO_CONTESTS_ABBREVIATION[sport]})
3211

3312
response.raise_for_status()
3413

draft_kings/response_translators.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
from dateutil.parser import parse as parse_datetime
22

33
from draft_kings.utilities import dig, translate_formatted_datetime, from_unix_milliseconds_to_datetime
4-
from draft_kings.data import SPORT_ID_TO_SPORT
4+
from draft_kings.data import SPORT_ID_TO_SPORT, Sport
5+
6+
"""
7+
The API takes a sport query parameter that's different than then sports API endpoint
8+
"""
9+
SPORT_TO_CONTESTS_ABBREVIATION = {
10+
Sport.NFL: "NFL",
11+
Sport.NHL: "NHL",
12+
Sport.NBA: "NBA",
13+
Sport.CFL: "CFL",
14+
Sport.COLLEGE_FOOTBALL: "CFB",
15+
Sport.MIXED_MARTIAL_ARTS: "MMA",
16+
Sport.NASCAR: "NAS",
17+
Sport.SOCCER: "SOC",
18+
Sport.EUROLEAGUE_BASKETBALL: "EL",
19+
Sport.MLB: "MLB",
20+
Sport.TENNIS: "TEN",
21+
Sport.LEAGUE_OF_LEGENDS: "LOL",
22+
Sport.GOLF: "GOLF",
23+
Sport.COLLEGE_BASKETBALL: "CBB"
24+
}
25+
26+
CONTEST_SPORT_ABBREVIATIONS_TO_SPORTS = {v: k for k, v in SPORT_TO_CONTESTS_ABBREVIATION.items()}
527

628

729
def translate_player(response):
@@ -76,23 +98,27 @@ def translate_contest(response):
7698

7799
def translate_contests(response):
78100
return {
79-
"contests": [translate_contest(contest) for contest in response.get("Contests", [])],
80-
"groups": [translate_draft_groups(response.get("DraftGroups", {}))],
101+
"contests": [
102+
translate_contest(contest)
103+
for contest in response.get("Contests", [])
104+
],
105+
"groups": [
106+
translate_contest_draft_group(draft_group)
107+
for draft_group in response.get("DraftGroups", [])
108+
],
81109
}
82110

83111

84-
def translate_draft_groups(groups):
85-
return [
86-
{
87-
"id": dig(group, "DraftGroupId"),
88-
"series_id": dig(group, "DraftGroupSeriesId"),
89-
"contest_type_id": dig(group, "ContestTypeId"),
90-
"sport_id": dig(group, "Sport"),
91-
"starts_at": dig(group, "StartDate"),
92-
"games_count": dig(group, "GameCount"),
93-
}
94-
for group in groups
95-
]
112+
def translate_contest_draft_group(draft_group):
113+
return {
114+
"id": dig(draft_group, "DraftGroupId"),
115+
"series_id": dig(draft_group, "DraftGroupSeriesId"),
116+
"contest_type_id": dig(draft_group, "ContestTypeId"),
117+
"sport_id": dig(draft_group, "Sport"),
118+
"sport": CONTEST_SPORT_ABBREVIATIONS_TO_SPORTS.get(dig(draft_group, "Sport")),
119+
"starts_at": dig(draft_group, "StartDate", transformer=parse_datetime),
120+
"games_count": dig(draft_group, "GameCount"),
121+
}
96122

97123

98124
def translate_countries(response):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="draft_kings",
8-
version="2.0.0",
8+
version="2.0.1",
99
author="Jae Bradley",
1010
author_email="jae.b.bradley@gmail.com",
1111
license="MIT",

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
22

33
from draft_kings.client import available_players, draft_group_details, draftables, regions, contests, countries
4-
from draft_kings.client import Sport
4+
from draft_kings.data import Sport
55

66

77
class TestClient(TestCase):

tests/test_response_translators.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import json
22
import os
33
from unittest import TestCase
4+
from datetime import datetime
5+
import pytz
46

57
from draft_kings.response_translators import translate_contests, translate_contest
8+
from draft_kings.data import Sport
69
from tests.config import ROOT_DIRECTORY
710

811

@@ -21,3 +24,15 @@ def test_translate_contest(self):
2124
translation = translate_contest(data)
2225
self.assertIsNotNone(translation)
2326
self.assertEqual(translation["id"], 32099545)
27+
self.assertFalse(translation["double_up"])
28+
self.assertEqual(translation["draft_group_id"], 11435)
29+
self.assertEqual(translation["entries"], {"maximum": 12261, "fee": 33.0, "total": 864})
30+
self.assertEqual(translation["fantasy_player_points"], 33)
31+
self.assertFalse(translation["fifty_fifty"])
32+
self.assertTrue(translation["guaranteed"])
33+
self.assertFalse(translation["head_to_head"])
34+
self.assertEqual(translation["name"], "NBA $350K Bird [$350,000 Guaranteed]")
35+
self.assertEqual(translation["payout"], 350000.0)
36+
self.assertEqual(translation["sport"], Sport.NBA)
37+
self.assertTrue(translation["starred"])
38+
self.assertEqual(translation["starts_at"], datetime(2016, 11, 12, 0, 0, 0, tzinfo=pytz.UTC))

0 commit comments

Comments
 (0)