Skip to content

Instantly share code, notes, and snippets.

@chowder
Created May 3, 2021 17:22
Show Gist options
  • Save chowder/85436a9de9eacfce5621b189bb3e14a7 to your computer and use it in GitHub Desktop.
Save chowder/85436a9de9eacfce5621b189bb3e14a7 to your computer and use it in GitHub Desktop.
Get Twitch channel redemptions via GraphQL API
import copy
import requests
import pprint
import os
GQL_URL = "https://gql.twitch.tv/gql"
GQL_QUERY = {
"operationName": "ChannelPointsContext",
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "1530a003a7d374b0380b79db0be0534f30ff46e61cffa2bc0e2468a909fbc024",
}
},
}
TWITCH_CLIENT_ID = os.environ['TWITCH_CLIENT_ID']
if not TWITCH_CLIENT_ID:
raise Exception("Environment variable TWITCH_CLIENT_ID not set!")
# Construct request
query = copy.deepcopy(GQL_QUERY)
query["variables"] = {"channelLogin": "correalian"}
# Post request
try:
response = requests.post(
GQL_URL,
json=query,
headers={
"Client-Id": TWITCH_CLIENT_ID,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
},
)
response = response.json()
redemptions = response["data"]["community"]["channel"]["communityPointsSettings"]["customRewards"]
pprint.pprint(redemptions)
except requests.exceptions.RequestException as e:
print(f"Error with GQL: ({query['operationName']}) {e}")
except KeyError as e:
print(f"Error with GQL: ({query['operationName']}) {response}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment