├── .gitignore ├── LICENSE ├── README.md ├── query_by_player.py ├── query_by_player_terms.xlsx ├── setup.py └── stattlepy ├── Stattleship_API.py └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Stattleship 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # stattlepy 2 | 3 | Stattleship PYTHON Wrapper brought to you by [@stattleship](https://twitter.com/stattleship). 4 | 5 | Check out the [Stattleship API](https://www.stattleship.com) - The Sports Data API you've always wanted 6 | 7 | Affordable. Meaningful. Developer-Friendly. 8 | 9 | :football: :basketball: and :black_circle: with :baseball: coming soon. 10 | 11 | We're gonna need a bigger :boat:! 12 | 13 | ## Differences From R API wrapper 14 | 15 | Due to '''type''' function in python, to indicate the type of stat you would like to query, use the '''stat_type''' variable in the API call 16 | 17 | ## Install 18 | With pip: 19 | ``` 20 | pip install git+https://github.com/stattleship/stattleship-python 21 | ``` 22 | 23 | Without pip: 24 | ``` 25 | git clone https://github.com/stattleship/stattleship-python.git 26 | cd /PATH/TO/DIRECTORY/ 27 | sudo python setup.py install 28 | ``` 29 | 30 | 31 | ## Getting Started 32 | Obtain an access TOKEN from [stattleship.com](www.stattleship.com). Load Python and initialize your TOKEN for your session and load the library: 33 | 34 | ``` 35 | from stattlepy import Stattleship 36 | New_query = Stattleship() 37 | Token = New_query.set_token('YOUR_TOKEN') 38 | Output = New_query.ss_get_results(sport='basketball',league='nba',ep='game_logs',player_id='nba-stephen-curry') 39 | ``` 40 | 41 | 42 | -------------------------------------------------------------------------------- /query_by_player.py: -------------------------------------------------------------------------------- 1 | """ this is a command line tool to get specific stats by player 2 | check associated excel file query_by_player_terms.xlsx to get possible --l2 and --l4 terms to use 3 | output written to outfile.txt in same directory""" 4 | 5 | import argparse 6 | from stattlepy import Stattleship 7 | 8 | def get_level2_data(Output, l2value): 9 | level3 = (Output[0])[l2value] 10 | return level3 11 | 12 | def get_level4_data(level3, l4value): 13 | outfile = open('outfile.txt', 'w') 14 | 15 | for item in level3: 16 | outfile.write(str(item[l4value])) 17 | outfile.write("\n") 18 | 19 | outfile.close() 20 | return 21 | 22 | 23 | parser = argparse.ArgumentParser() 24 | 25 | parser.add_argument('--p', '-player_id', type=str, dest='p_id', help='Player ID', metavar='pid') 26 | parser.add_argument('--l2', '-level2_key', type=str, dest='l2key', help='Level 2 term - refer to excel sheet for terms', metavar='l2') 27 | parser.add_argument('--l4', '-level4_key', type=str, dest='l4key', help='Level 4 term - refer to excel sheet for terms', metavar='l4') 28 | parser.add_argument('--t', '-token', type=str, dest='token', help='Enter your token from Stattleship', metavar='token') 29 | 30 | args = parser.parse_args() 31 | 32 | New_query = Stattleship() 33 | 34 | Token = New_query.set_token(args.token) 35 | 36 | Output = New_query.ss_get_results(sport = "basketball", league="nba", ep="game_logs", player_id = args.p_id) 37 | 38 | 39 | #Output is a list of 1 dictionary 40 | 41 | #To get attributes 42 | #for item in Output: 43 | 44 | #for item2 in item.keys(): 45 | #print "##########", item2 46 | 47 | #for item3 in item[item2][0]: 48 | 49 | #print item3 50 | 51 | level3_data = get_level2_data(Output, args.l2key) 52 | get_level4_data(level3_data, args.l4key) 53 | -------------------------------------------------------------------------------- /query_by_player_terms.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stattleship/stattleship-python/3be646603f6d9c5f6b181a97078e97807137f763/query_by_player_terms.xlsx -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | 4 | setup( 5 | name='stattlepy', 6 | version='0.0.1', 7 | author = ['Adam Jenkins', 'Stattleship'], 8 | packages=['stattlepy'], 9 | license='MIT', 10 | long_description=open('README.md').read(), 11 | ) -------------------------------------------------------------------------------- /stattlepy/Stattleship_API.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | ### Version 0.0.1 4 | 5 | #Install: 6 | #git clone https://github.com/stattleship/stattleship-python.git 7 | #cd /PATH/TO/DIRECTORY/ 8 | #sudo python setup.py install 9 | 10 | #Usage: 11 | #New_query = Stattleship() 12 | #Token = New_query.set_token('YOUR_TOKEN') 13 | #Output = New_query.ss_get_results(sport='basketball',league='nba',ep='game_logs',player_id='nba-stephen-curry') 14 | 15 | import requests 16 | import json 17 | import math 18 | import time 19 | import re 20 | import platform 21 | import stattlepy 22 | 23 | ### Main class that all Stattleship functions will be a part of 24 | class Stattleship(object): 25 | 26 | 27 | 28 | # function to set the token 29 | def set_token(self, pro_token): 30 | if pro_token is None or not isinstance(pro_token,basestring): 31 | warnings.warn('Stattleship API token must be provided in order to access the Stattleship API.') 32 | else: 33 | global token 34 | token = pro_token 35 | 36 | ### function to get the results for them Stattleship API 37 | def ss_get_results(self,**kwargs): 38 | 39 | # initial defaults for all variables 40 | sport = "hockey" 41 | league = "nhl" 42 | ep = "teams" 43 | query = list() 44 | version = 1 45 | walk = False 46 | page = 1 47 | verbose = True 48 | place = None 49 | stat_type = None 50 | param = {} 51 | 52 | # loop through inputs and 53 | for key, value in kwargs.iteritems(): 54 | if str(key) == 'sport': 55 | sport = value 56 | elif str(key) == 'league': 57 | league = value 58 | elif str(key) == 'ep': 59 | ep = value 60 | elif str(key) == 'version': 61 | version = value 62 | elif str(key) == 'walk': 63 | walk = value 64 | elif str(key) == 'page': 65 | page = value 66 | elif str(key) == 'verbose': 67 | verbose = value 68 | elif str(key) == 'stat_type': 69 | param['type'] = value 70 | else: 71 | param[key] = value 72 | 73 | ### initial verbose to indicate request occurring 74 | if verbose: 75 | print'Making Initial API Request' 76 | 77 | ### initial query 78 | tmp, return_header = self.query_api(sport, league, ep, param, version, walk, page, verbose, token) 79 | 80 | ### make response list 81 | response = list() 82 | 83 | ### set the original first parsed 84 | response.append(tmp) 85 | 86 | ### walk function using REGEX to idenify the next link in the header to pull next request 87 | if(walk): 88 | while 'link' in return_header: 89 | 90 | ### Next link to request from the API 91 | next_link = re.findall( 'rel="last", <(.*?)>; rel="next"', return_header['link'], re.MULTILINE) 92 | 93 | ### Use try and except to see if the next link was found within the header 94 | try: 95 | if verbose: 96 | print 'Next link sent to API:' 97 | print next_link[0] 98 | 99 | headers = { 100 | 'Authorization': token, 101 | 'Accept':'application/vnd.stattleship.com; version=%s' %version, 102 | 'Content-Type':'application/json', 103 | 'User-Agent':'Stattleship Python/{} ({})'.format(stattlepy.__version__,platform.platform()) 104 | } 105 | 106 | print headers 107 | 108 | res = requests.get(next_link[0], headers = headers) 109 | 110 | content = json.loads(res.content) 111 | 112 | response.append(content) 113 | 114 | return_header = res.headers 115 | 116 | ### delay in making call 117 | time.sleep(0.1) 118 | 119 | except IndexError: 120 | break 121 | 122 | print 'Stattleship API request complete' 123 | return(response) 124 | 125 | def query_api(self, sport, league, ep, param, version, walk, page, verbose, token): 126 | 127 | ### make sure that the sport, league and ep are all lower case 128 | sport = sport.lower() 129 | league = league.lower() 130 | ep = ep.lower() 131 | 132 | ### base url to make the request from 133 | url = 'https://api.stattleship.com/{}/{}/{}'.format(sport, league, ep) 134 | 135 | ### depends on page being requested 136 | if page >= 1: 137 | param['page'] = page 138 | 139 | headers = { 140 | 'Authorization': token, 141 | 'Accept':'application/vnd.stattleship.com; version=%s' %version, 142 | 'Content-Type':'application/json', 143 | 'User-Agent':'Stattleship Python/{} ({})'.format(stattlepy.__version__,platform.platform()) 144 | } 145 | 146 | res = requests.get(url,params=param, headers = headers) 147 | 148 | if verbose: 149 | print res 150 | print res.url 151 | 152 | content = json.loads(res.content) 153 | 154 | return(content, res.headers) 155 | -------------------------------------------------------------------------------- /stattlepy/__init__.py: -------------------------------------------------------------------------------- 1 | ### Import Statteleship class upon initiation of the program 2 | from .Stattleship_API import Stattleship 3 | 4 | from pkg_resources import get_distribution 5 | 6 | __version__ = get_distribution('stattlepy').version --------------------------------------------------------------------------------