Skip to content

Instantly share code, notes, and snippets.

@derek-adair
Created July 4, 2020 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derek-adair/d64f6c076aa8eb90f0f08a6b1a2e3c4c to your computer and use it in GitHub Desktop.
Save derek-adair/d64f6c076aa8eb90f0f08a6b1a2e3c4c to your computer and use it in GitHub Desktop.
Uses new NFL API to pull down 2020 week 1 schedule
import json
import requests
url = "https://api.nfl.com/v1/reroute"
# TODO: resolve if DNT or x-domain-id are necessary. pulled them from chrome inspector
payload = 'grant_type=client_credentials'
headers = {
'DNT': '1',
'x-domain-id': '100',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data = payload)
access_token = json.loads(response.content)['access_token']
# this query can be customized to return different data
# the line below will return 2019 week 1 results
# url = "https://api.nfl.com/v3/shield/?query=query{viewer{gameDetailsByIds(ids:[\"10160000-0579-156c-c1e2-24a16164d808\",\"10160000-0579-1456-122e-a499416e9643\",\"10160000-0579-1386-265b-4eb8c397806f\",\"10160000-0579-12d8-bb4a-d7a447161b3e\",\"10160000-0579-118b-3c8b-c2da80430da7\",\"10160000-0579-1049-369d-40104333e452\",\"10160000-0579-0943-6390-152ae4e3070f\",\"10160000-0579-0857-d2d2-30a8e1143954\",\"10160000-0579-0745-9311-a4adde960c3f\",\"10160000-0579-0647-b2a2-599c7c0b47ed\",\"10160000-0579-055e-7350-c0a2552a523f\",\"10160000-0579-0420-7eda-cfc27c838aeb\",\"10160000-0579-0376-f060-48ba658e0b31\",\"10160000-0579-02bc-e447-a1723e8641f6\",\"10160000-0579-01d7-059f-06001dedfbc7\",\"10160000-0579-00cc-9a8c-d465ab211561\"]){id,phase,homePointsTotal,visitorPointsTotal,phase gameClock period possessionTeam{abbreviation}yardsToGo down distance stadium weather{location}yardLine redzone}}}&variables=null"
# this will return the 2020 week 1 schedule
url = "https://api.nfl.com/v3/shield/?query=query{viewer{league{games(first:100,week_seasonValue:2020,week_seasonType:REG,week_weekValue:1,){edges{cursor node{id esbId gameDetailId gameTime gsisId networkChannels venue{fullName city state}awayTeam{nickName id abbreviation franchise{currentLogo{url}}}homeTeam{nickName id abbreviation franchise{currentLogo{url}}}slug}}}}}}&variables=null"
payload = {}
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}'
}
sched_resp = requests.request("GET", url, headers=headers, data = payload)
@RustyBower
Copy link

i'm playing with this now, but do you know how to find the gameDetailsByIds? digging through Chrome inspector doesn't show those 1016xxxx IDs anywhere

@derek-adair
Copy link
Author

I haven't used or maintained any NFL related code for some time now.

@RustyBower
Copy link

RustyBower commented Sep 15, 2021 via email

@heyyouca
Copy link

All good :) I got it all sorted, it’s just a bit tedious working with their API

As far as I can tell, between last July and now, the NFL deprecated part of the api at https://api.nfl.com/v3/shield/, root.games and league.games, which I had used to get the detailedids (for a given season/week, etc.) necessary to used the ...byIds queries.

@RustyBower: Have you found these? Is that what you mean by "all good"? If so, do you mind sharing what you found?

@RustyBower
Copy link

I pulled all the changes I needed into https://github.com/RustyBower/sopel-sports/blob/master/sopel_modules/sports/nfl.py

I'm still working on figuring out how to grab schedule/score from arbitrary years/weeks

@derek-adair
Copy link
Author

Sopel Sports looks pretty legit. How far do you intend on taking this? is there any reason you're building this as a sopel app and not just a generic tool like nflgame?

@RustyBower
Copy link

It's a plugin for an IRC bot I help maintain, so just easier to keep it all maintained in one spot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment