Skip to content

Instantly share code, notes, and snippets.

@AlexMikhalev
Created August 21, 2022 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMikhalev/32be2e4da18dfa9c675d5de88b052498 to your computer and use it in GitHub Desktop.
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
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
email
url
}
... on Organization {
id
email
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