-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
retrosheet using deprecated GitHub authentication #455
Comments
Did you add this code to the library itself or run it separately? I tried just throwing it in my notebook but am having the same issues with retrosheet |
@B-D-T what exactly did you change to get this to work? |
Hi @jackseeburger & @DattiloJohn: Below is a sample of my code. In short, I duplicated certain sections of code from the Here's an example of what I changed, using the pybaseball codedef rosters(season):
###############
# NOTE: This is the problematic part as it's looking for the environment variable
GH_TOKEN=os.getenv('GH_TOKEN', '')
###############
try:
g = Github(GH_TOKEN)
repo = g.get_repo('chadwickbureau/retrosheet')
tree = repo.get_git_tree('master')
for t in tree.tree:
if t.path == 'rosters':
subtree = t
rosters = [t.path for t in repo.get_git_tree(subtree.sha).tree if str(season) in t.path]
if len(rosters) == 0:
raise ValueError(f'Rosters not available for {season}')
except RateLimitExceededException:
warnings.warn('Github rate limit exceeded. Cannot check if the file you want exists.', UserWarning)
df_list = [_roster(team = r[:3], season = season, checked=False) for r in rosters]
return pd.concat(df_list) My codefrom github import Github, Auth as GithubAuth
import pybaseball
class GetDataBySeason:
def __init__(self) -> None:
###############
# NOTE: Authentication without using an environment variable
# Set your personal access token here
gh_personal_access_token_pybaseball: str = 'github_pat_11...'
# Pass the token to the GithubAuth.Token class
auth = GithubAuth.Token(gh_personal_access_token_pybaseball)
# Create a Github object with the token
self.gh_api_obj = Github(auth = auth)
###############
def get_retrosheet_roster_by_season(self, season):
try:
# NOTE This uses the authenticated `Github` object
repo = self.gh_api_obj.get_repo('chadwickbureau/retrosheet')
season_folder = [f.path[f.path.rfind('/')+1:] for f in repo.get_contents(f'seasons/{season}')]
rosters = [t for t in season_folder if t.endswith('.ROS')]
if len(rosters) == 0:
raise ValueError(f'Rosters not available for {season}')
except:
print('Github rate limit exceeded. Cannot check if the file you want exists.', UserWarning)
df_list = [self.get_retrosheet_roster_by_season_team(team = r[:3], season = season, checked=False) for r in rosters]
return pd.concat(df_list)
def get_retrosheet_roster_by_season_team(self, season, team, checked = False):
# Similar code to replicate pybaseball's `_roster` method
... I didn't want to replicate the methods, since that's not sustainable at scale, but doing that served my one-off purpose at the time. It's probably better to just make changes to your local Hope this helps! |
The PyGithub package uses a new(er) form of PAT authentication to access the GitHub REST API. The code in retrosheet.py needs to be updated to reflect the new approach.
Steps to replicate
Call any function that uses the
g = Github(GH_TOKEN)
line. For example,Issue details
The retrosheet.py file reads a
GH_TOKEN
environment variable that holds the user's GitHub personal access token.It then passes the token when instantiating a
Github
object:That triggers the following within the
github
library:Perhaps this is related to #414, although the notes make it look like that one is all set.
How to fix
I got it working on my end using code similar to the following:
The text was updated successfully, but these errors were encountered: