-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update python versions + add bref session + various test, bug, and ty…
…ping fixes (#318) * Add 3.10 and 3.11 and remove 3.8 and 3.9 from test versions * Update python versions + add bref session + various test, bug, and typing fixes --------- Co-authored-by: Moshe Schorr <mosheraphael@gmail.com>
- Loading branch information
Showing
36 changed files
with
1,474 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import datetime | ||
from time import sleep | ||
from typing import Any, Optional | ||
|
||
import requests | ||
|
||
from ..datahelpers import singleton | ||
|
||
|
||
class BRefSession(singleton.Singleton): | ||
""" | ||
This is needed because Baseball Reference has rules against bots. | ||
Current policy says no more than 20 requests per minute, but in testing | ||
anything more than 10 requests per minute gets you blocked for one hour. | ||
So this global session will prevent a user from getting themselves blocked. | ||
""" | ||
|
||
def __init__(self, max_requests_per_minute: int = 10) -> None: | ||
self.max_requests_per_minute = max_requests_per_minute | ||
self.last_request: Optional[datetime.datetime] = None | ||
self.session = requests.Session() | ||
|
||
def get(self, url: str, **kwargs: Any) -> requests.Response: | ||
if self.last_request: | ||
delta = datetime.datetime.now() - self.last_request | ||
sleep_length = (60 / self.max_requests_per_minute) - delta.total_seconds() | ||
if sleep_length > 0: | ||
sleep(sleep_length) | ||
|
||
self.last_request = datetime.datetime.now() | ||
|
||
return self.session.get(url, **kwargs) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.