From ec3fe70b1c3d3fdd4cfe86b208e8f3cf619dc4a3 Mon Sep 17 00:00:00 2001 From: JinchengHeRyan Date: Wed, 27 Apr 2022 19:44:26 +0800 Subject: [PATCH] Support `limit` parameter to crawl data --- README.md | 4 ++-- marvel/base.py | 5 +++-- marvel/marvel.py | 5 +++-- marvel/requester.py | 8 +++++++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fa89bb3..5d35b2e 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ This is an API wrapper for [Marvel](https://developer.marvel.com/docs). Usage ===== -Usage is as simple as it gets: +Usage is as simple as it gets, LIMIT is not required, 20 is default, 100 is the maximum can be supported: from marvel import Marvel - m = Marvel(PUBLIC_KEY, PRIVATE_KEY) + m = Marvel(PUBLIC_KEY, PRIVATE_KEY, LIMIT) Now there are six objects presented namely, `characters`, `comics`, `creators`, `events`, `series` and `stories` as listed at [Developers](https://developer.marvel.com/documentation/entity_types) diff --git a/marvel/base.py b/marvel/base.py index d876fea..47a7c5d 100644 --- a/marvel/base.py +++ b/marvel/base.py @@ -2,10 +2,11 @@ class Base: - def __init__(self, PUBLIC_KEY, PRIVATE_KEY): + def __init__(self, PUBLIC_KEY, PRIVATE_KEY, LIMIT): """ Base Class :param PUBLIC_KEY: str :param PRIVATE_KEY: str + :param LIMIT: int """ - self.requester = Requester(PUBLIC_KEY, PRIVATE_KEY) + self.requester = Requester(PUBLIC_KEY, PRIVATE_KEY, LIMIT) diff --git a/marvel/marvel.py b/marvel/marvel.py index 857814c..ce337cd 100644 --- a/marvel/marvel.py +++ b/marvel/marvel.py @@ -8,13 +8,14 @@ class Marvel(Base): - def __init__(self, PUBLIC_KEY, PRIVATE_KEY): + def __init__(self, PUBLIC_KEY, PRIVATE_KEY, LIMIT=None): """ Marvel main class to access different modules and their methods. :param PUBLIC_KEY: str :param PRIVATE_KEY: str + :param LIMIT: int """ - super().__init__(PUBLIC_KEY, PRIVATE_KEY) + super().__init__(PUBLIC_KEY, PRIVATE_KEY, LIMIT) self.characters = Characters(requester=self.requester) self.events = Events(requester=self.requester) self.series = Series(requester=self.requester) diff --git a/marvel/requester.py b/marvel/requester.py index 53f4cff..09927ae 100644 --- a/marvel/requester.py +++ b/marvel/requester.py @@ -6,7 +6,7 @@ class Requester(EndpointManager): - def __init__(self, PUBLIC_KEY, PRIVATE_KEY): + def __init__(self, PUBLIC_KEY, PRIVATE_KEY, LIMIT): """ Requester class responsible for making HTTP requests. :param PUBLIC_KEY: str @@ -16,6 +16,7 @@ def __init__(self, PUBLIC_KEY, PRIVATE_KEY): self.PUBLIC_KEY = PUBLIC_KEY self.PRIVATE_KEY = PRIVATE_KEY self.r = object + self.limit = LIMIT def request(self, endpoint_name, payload=None, sub_endpoint=None, identifier=None, raw=None): """ @@ -34,6 +35,11 @@ def request(self, endpoint_name, payload=None, sub_endpoint=None, identifier=Non url += "/" + str(identifier) if sub_endpoint is not None: url += "/" + sub_endpoint + if self.limit is not None: + assert self.limit <= 100, ( + "limit %d higher than the maximum 100 can be supported" % self.limit + ) + payload["limit"] = self.limit query = self.get_query_with_authentication_params(payload) self.r = requests.get(url, params=query) if raw: