Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions hexproof/mtgjson/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
* MTGJSON Request Handling
"""
import os
# Standard Library Imports
from typing import Callable, Optional
from pathlib import Path
from typing import Callable, Optional

# Third Party Imports
import requests
import yarl
from ratelimit import sleep_and_retry, RateLimitDecorator
from backoff import on_exception, expo
from omnitils.fetch import request_header_default, download_file
from backoff import expo, on_exception
from limits import RateLimitItemPerHour, RateLimitItemPerSecond
from limits.storage import MemoryStorage
from limits.strategies import MovingWindowRateLimiter
from omnitils.fetch import download_file, request_header_default
from omnitils.files.archive import unpack_tar_gz
from omnitils.rate_limit import rate_limit

# Local Imports
from hexproof.mtgjson.enums import MTGJsonURL
from hexproof.mtgjson import schema as MTGJsonTypes
from hexproof.mtgjson.enums import MTGJsonURL

# Rate limiter to safely limit MTGJSON requests
mtgjson_rate_limit = RateLimitDecorator(calls=20, period=1)
mtgjson_gql_rate_limit = RateLimitDecorator(calls=20, period=1)
_rate_limit_storage = MemoryStorage()
_rate_limiter = MovingWindowRateLimiter(_rate_limit_storage)
_mtgjson_rate_limit = RateLimitItemPerSecond(20)
_mtgjson_gql_rate_limit = RateLimitItemPerHour(500)


"""
Expand All @@ -35,8 +37,7 @@ def request_handler_mtgjson(func) -> Callable:
There are no known rate limits for requesting JSON file resources.
We include a 20-per-second rate limit just to be nice.
"""
@sleep_and_retry
@mtgjson_rate_limit
@rate_limit(limiter=_rate_limiter, limit=_mtgjson_rate_limit)
@on_exception(expo, requests.exceptions.RequestException, max_tries=2, max_time=1)
def decorator(*args, **kwargs):
return func(*args, **kwargs)
Expand All @@ -50,8 +51,7 @@ def request_handler_mtgjson_gql(func) -> Callable:
MTGJSON GraphQL requests are capped at 500 per-hour per-token at the moment.
https://mtgjson.com/mtggraphql/#rate-limits
"""
@sleep_and_retry
@mtgjson_gql_rate_limit
@rate_limit(limiter=_rate_limiter, limit=_mtgjson_gql_rate_limit)
@on_exception(expo, requests.exceptions.RequestException, max_tries=2, max_time=1)
def decorator(*args, **kwargs):
return func(*args, **kwargs)
Expand Down
2 changes: 0 additions & 2 deletions hexproof/mtgjson/schema/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,3 @@ class CardToken(Schema):
"""

Card = Union[CardAtomic, CardDeck, CardSet, CardSetDeck, CardToken]
Card.__doc__ = ('A Card is a data structure with variations of Data Models that is found within files '
'that reference cards, and is not a Data Model itself.')
16 changes: 9 additions & 7 deletions hexproof/mtgpics/fetch.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"""
* MTGPics Data Requests
"""
# Standard Libray Imports
from typing import Callable, Optional

# Third Party Imports
import requests
import yarl
from backoff import expo, on_exception
from limits import RateLimitItemPerSecond
from limits.storage import MemoryStorage
from limits.strategies import MovingWindowRateLimiter
from omnitils.exceptions import return_on_exception
from omnitils.fetch import request_header_default
from ratelimit import sleep_and_retry, RateLimitDecorator
from omnitils.rate_limit import rate_limit

"""
* MTGPics Request Handlers
"""

# Rate limiter to safely limit Scryfall requests
mtgp_rate_limit = RateLimitDecorator(calls=20, period=1)
# Rate limiter to safely limit MTGPics requests
_rate_limit_storage = MemoryStorage()
_rate_limiter = MovingWindowRateLimiter(_rate_limit_storage)
_rate_limit = RateLimitItemPerSecond(20)


def request_handler_mtgpics(func: Callable) -> Callable:
Expand All @@ -30,8 +33,7 @@ def request_handler_mtgpics(func: Callable) -> Callable:
Wrapped function.
"""

@sleep_and_retry
@mtgp_rate_limit
@rate_limit(limiter=_rate_limiter, limit=_rate_limit)
@on_exception(expo, requests.exceptions.RequestException, max_tries=2, max_time=1)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions hexproof/scryfall/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class SetType(StrConstant):
Core = 'core'
Expansion = 'expansion'
Masters = 'masters'
Eternal = 'eternal'
Alchemy = 'alchemy'
Masterpiece = 'masterpiece'
Arsenal = 'arsenal'
Expand Down
23 changes: 12 additions & 11 deletions hexproof/scryfall/fetch.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
"""
* Scryfall Request Handling
"""
# Third Party Imports
from pathlib import Path
from typing import Callable, Optional

# Third Party Imports
import requests
import yarl
from omnitils.fetch import request_header_default, download_file
from backoff import expo, on_exception
from limits import RateLimitItemPerSecond
from limits.storage import MemoryStorage
from limits.strategies import MovingWindowRateLimiter
from omnitils.fetch import download_file, request_header_default
from omnitils.rate_limit import rate_limit
from omnitils.strings import normalize_str
from ratelimit import sleep_and_retry, RateLimitDecorator
from backoff import on_exception, expo
from requests import RequestException

# Local Imports
from hexproof.scryfall.enums import ScryURL
from hexproof.scryfall import schema as ScrySchema
from hexproof.scryfall.enums import ScryURL

# Rate limiter to safely limit MTGJSON requests
scryfall_rate_limit = RateLimitDecorator(calls=20, period=1)
# Rate limiter to safely limit Scryfall requests
_rate_limit_storage = MemoryStorage()
_rate_limiter = MovingWindowRateLimiter(_rate_limit_storage)
_rate_limit = RateLimitItemPerSecond(20)


"""
Expand All @@ -42,8 +44,7 @@ def request_handler_scryfall(func) -> Callable:
Returns:
The wrapped function.
"""
@sleep_and_retry
@scryfall_rate_limit
@rate_limit(limiter=_rate_limiter, limit=_rate_limit)
@on_exception(expo, RequestException, max_tries=2, max_time=1)
def decorator(*args, **kwargs):
return func(*args, **kwargs)
Expand Down
Loading