Skip to content

Commit

Permalink
Merge pull request #77 from MerleLiuKun/feat-ratelimit
Browse files Browse the repository at this point in the history
Feat ratelimit
  • Loading branch information
MerleLiuKun authored Aug 30, 2021
2 parents 8874106 + be80eda commit 9a1c019
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
41 changes: 41 additions & 0 deletions pytwitter/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Endpoint:
# For different auth type and endpoint
LIMIT_APP_GET: int = 15
LIMIT_USER_GET: int = 15
LIMIT_APP_POST: int = 0
LIMIT_USER_POST: int = 0
LIMIT_USER_PUT: int = 0
LIMIT_USER_DELETE: int = 0
Expand Down Expand Up @@ -187,6 +188,40 @@ def get_limit(self, auth_type, method="GET"):
LIMIT_APP_GET=300,
)

SPACE_BY_ID = Endpoint(
resource="/spaces/:id",
regex=re.compile(r"/spaces/\w+"),
LIMIT_APP_GET=300,
)
SPACES_BY_IDS = Endpoint(
resource="/spaces",
regex=re.compile(r"/spaces"),
LIMIT_APP_GET=300,
)
SPACES_BY_CREATORS = Endpoint(
resource="/spaces/by/creator_ids",
regex=re.compile(r"/spaces/by/creator_ids"),
LIMIT_APP_GET=300,
)
SPACES_SEARCH = Endpoint(
resource="/spaces/search",
regex=re.compile("/spaces/search"),
LIMIT_APP_GET=300,
)

COMPLIANCE_JOB_BY_ID = Endpoint(
resource="/compliance/jobs/:job_id",
regex=re.compile(r"/compliance/jobs/\d+"),
LIMIT_APP_GET=150,
)
COMPLIANCE_JOBS = Endpoint(
resource="/compliance/jobs",
regex=re.compile(r"/compliance/jobs"),
LIMIT_APP_GET=150,
LIMIT_APP_POST=150,
)


PATH_VAR_ENDPOINTS = [
USER_BY_ID,
USERS_BY_ID,
Expand All @@ -211,6 +246,12 @@ def get_limit(self, auth_type, method="GET"):
USER_TWEET_LIKE_REMOVE,
TWEET_HIDDEN,
TWEET_COUNTS,
SPACE_BY_ID,
SPACES_BY_IDS,
SPACES_BY_CREATORS,
SPACES_SEARCH,
COMPLIANCE_JOB_BY_ID,
COMPLIANCE_JOBS,
]


Expand Down
3 changes: 0 additions & 3 deletions tests/test_rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def test_setter(self):
d = user_rate_limit.get_limit(following_url, method="POST")
assert d.limit == 15

d = rate_limit.get_limit(following_url, method="POST")
assert d.limit == 0

d = rate_limit.set_limit(
url="https://api.twitter.com/2/users/123456/following",
headers=self.generate_headers(15, 10, 1612522029),
Expand Down

0 comments on commit 9a1c019

Please sign in to comment.