You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
retrosheet using deprecated GitHub authentication #455
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,
pybaseball.rosters(2023)
# or the more explicit : pybaseball.retrosheet.rosters(2023)
Issue details
The retrosheet.py file reads a GH_TOKEN environment variable that holds the user's GitHub personal access token.
GH_TOKEN=os.getenv('GH_TOKEN', '')
It then passes the token when instantiating a Github object:
g=Github(GH_TOKEN)
That triggers the following within the github library:
Filec:\...\Lib\site-packages\github\MainClass.py:230, inGithub.__init__(self, login_or_token, password, jwt, app_auth, base_url, timeout, user_agent, per_page, verify, retry, pool_size, seconds_between_requests, seconds_between_writes, auth)
225eliflogin_or_tokenisnotNone:
226warnings.warn(
227"Argument login_or_token is deprecated, please use ""auth=github.Auth.Token(...) instead",
228category=DeprecationWarning,
229 )
-->230auth=github.Auth.Token(login_or_token)
231elifjwtisnotNone:
232warnings.warn(
233"Argument jwt is deprecated, please use "234"auth=github.Auth.AppAuth(...) or "235"auth=github.Auth.AppAuthToken(...) instead",
236category=DeprecationWarning,
237 )
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:
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
Below is a sample of my code. In short, I duplicated certain sections of code from the pybaseball library, but with the necessary changes to authenticate without using an environment variable.
Here's an example of what I changed, using the rosters() method in the pybaseball.retrosheet module as the sample.
pybaseball code
defrosters(season):
################ NOTE: This is the problematic part as it's looking for the environment variableGH_TOKEN=os.getenv('GH_TOKEN', '')
###############try:
g=Github(GH_TOKEN)
repo=g.get_repo('chadwickbureau/retrosheet')
tree=repo.get_git_tree('master')
fortintree.tree:
ift.path=='rosters':
subtree=trosters= [t.pathfortinrepo.get_git_tree(subtree.sha).treeifstr(season) int.path]
iflen(rosters) ==0:
raiseValueError(f'Rosters not available for {season}')
exceptRateLimitExceededException:
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) forrinrosters]
returnpd.concat(df_list)
My code
fromgithubimportGithub, AuthasGithubAuthimportpybaseballclassGetDataBySeason:
def__init__(self) ->None:
################ NOTE: Authentication without using an environment variable# Set your personal access token heregh_personal_access_token_pybaseball: str='github_pat_11...'# Pass the token to the GithubAuth.Token classauth=GithubAuth.Token(gh_personal_access_token_pybaseball)
# Create a Github object with the tokenself.gh_api_obj=Github(auth=auth)
###############defget_retrosheet_roster_by_season(self, season):
try:
# NOTE This uses the authenticated `Github` objectrepo=self.gh_api_obj.get_repo('chadwickbureau/retrosheet')
season_folder= [f.path[f.path.rfind('/')+1:] forfinrepo.get_contents(f'seasons/{season}')]
rosters= [tfortinseason_folderift.endswith('.ROS')]
iflen(rosters) ==0:
raiseValueError(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) forrinrosters]
returnpd.concat(df_list)
defget_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 pybaseball source code; I only didn't explore that route because I move across devices a lot, and because I didn't want to lose my changes when doing future updates to the pybaseball package.
Activity
jackseeburger commentedon Dec 3, 2024
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
DattiloJohn commentedon Dec 23, 2024
@B-D-T what exactly did you change to get this to work?
B-D-T commentedon Dec 23, 2024
Hi @jackseeburger & @DattiloJohn:
Below is a sample of my code. In short, I duplicated certain sections of code from the
pybaseballlibrary, but with the necessary changes to authenticate without using an environment variable.Here's an example of what I changed, using the
rosters()method in thepybaseball.retrosheetmodule as the sample.pybaseball code
My code
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
pybaseballsource code; I only didn't explore that route because I move across devices a lot, and because I didn't want to lose my changes when doing future updates to thepybaseballpackage.Hope this helps!
Fix deprecated GitHub authentication in retrosheet.py (jldbc#455)
Fix deprecated GitHub authentication in retrosheet.py (jldbc#455)
test: add regression tests for retrosheet modern GitHub auth (jldbc#455)
Fix deprecated GitHub authentication in retrosheet.py (jldbc#455)
test: add regression tests for retrosheet modern GitHub auth (jldbc#455)