Created
August 21, 2022 14:52
-
-
Save AlexMikhalev/32be2e4da18dfa9c675d5de88b052498 to your computer and use it in GitHub Desktop.
Check if user is sponsor and it's tier via Github GraphQL API with rate_limit
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
import requests | |
from datetime import datetime | |
import time | |
import os | |
github_token = os.getenv('GITHUB_TOKEN') | |
real_requests_post = requests.post | |
def wrap_requests_post(*args, **kwargs): | |
if not 'headers' in kwargs: | |
kwargs['headers'] = {} | |
kwargs['headers']['Authorization'] = 'token ' + github_token | |
response = real_requests_post(*args, **kwargs) | |
if 'x-ratelimit-used' in response.headers._store: | |
print("ratelimit status: used %s of %s. next reset in %s minutes" % ( | |
response.headers['X-RateLimit-Used'], | |
response.headers['X-RateLimit-Limit'], | |
datetime.utcfromtimestamp(int(response.headers['X-RateLimit-Reset']) - time.time()).strftime('%M:%S') | |
)) | |
return response | |
requests.post = wrap_requests_post | |
query = """ | |
{ | |
viewer { | |
sponsorshipsAsSponsor(first: 100) { | |
nodes { | |
sponsorable { | |
... on User { | |
id | |
url | |
} | |
... on Organization { | |
id | |
name | |
url | |
} | |
} | |
tier { | |
id | |
name | |
monthlyPriceInDollars | |
monthlyPriceInCents | |
} | |
} | |
} | |
} | |
} | |
""" | |
response = requests.post('https://api.github.com/graphql', json={'query': query}) | |
data = response.json() | |
print(response._content) | |
# Find who user is sponsoring | |
# query { | |
# user(login: "cheshire137") { | |
# sponsoring(first: 10) { | |
# totalCount | |
# nodes { | |
# ... on User { login } | |
# ... on Organization { login } | |
# } | |
# } | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment