Hi, I am trying to make a webhook integration to discord when there is a warframe drop enabled stream goes live, i want to be notified. However, this code is not working properly can you help me to fix it?
The problem is “Failed to get stream data from Twitch API” I receive this error.
# Import necessary libraries
import requests
import time
# Set up the Twitch API client
client_id = "********I cannot find this**********"
api_url = "https://api.twitch.tv/helix/streams"
# Set up the Discord API client
discord_api_url = "https://discord.com/api/webhooks/*******************************************"
# Set up the parameters for the Twitch API request
params = {
"game_id": "33214", # Warframe game ID
"type": "live", # Only show live streams
"first": 100, # Maximum number of results to return
"language": "en" # Only show English streams
}
while True: # Loop indefinitely
# Make the Twitch API request
response = requests.get(api_url, params=params, headers={"Client-ID": client_id})
# Check the response status code
if response.status_code == 200:
# If the request is successful, get the list of streams
streams = response.json()["data"]
# Loop through the list of streams
for stream in streams:
# Check if the stream has Warframe drops enabled
if "warframe_drops_enabled" in stream and stream["warframe_drops_enabled"]:
# Get the streamer's name and the stream's title
streamer_name = stream["user_name"]
stream_title = stream["title"]
# Set up the payload for the Discord API request
payload = {
"username": "Warframe Stream Alert",
"content": f"{streamer_name} is streaming Warframe with drops enabled!\n\n{stream_title}"
}
# Make the Discord API request
requests.post(discord_api_url, json=payload)
print(f"Notified Discord of stream by {streamer_name} with drops enabled.")
else:
print("Failed to get stream data from Twitch API")
# Wait for 2 minutes before checking again
time.sleep(120)
I have edited like this but there is another problem. What is the wrong statement here?
while True: # Loop indefinitely
# Make the Twitch API request
response = requests.get(https://api.twitch.tv/helix/streams, params=&game_id=33214, headers={"p1fynh1uq4**********5lge5erl": client_id})
# Import necessary libraries
import requests
import time
client_id = 'p1fynh**************f35lge5erl'
client_secret = 'fh1zym**********3rirtgj8yc'
body = {
'client_id': client_id,
'client_secret': client_secret,
"grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)
#data output
keys = r.json()
print(keys)
headers = {
'Client-ID': client_id,
'Authorization': 'Bearer ' + keys['access_token']
}
print(headers)
discord_api_url = "https://discord.com/api/webhooks/105309019591************************************sSxBMBGvRgCCEISC9K0"
# Set up an infinite loop to check for streams with Warframe drops enabled
while True:
# Make a request to the Twitch API to get a list of live streams
response = requests.get(
"https://api.twitch.tv/helix/streams",
headers=headers
)
params = {
"game_id": "33214", # Warframe game ID
"type": "live", # Only show live streams
"first": 100, # Maximum number of results to return
"language": "en" # Only show English streams
}
# Check the response status code
if response.status_code == 200:
# If the request is successful, get the list of streams
streams = response.json()["data"]
# Loop through the list of streams
for stream in streams:
# Check if the stream has Warframe drops enabled
if "warframe_drops_enabled" in stream and stream["warframe_drops_enabled"]:
# Get the streamer's name and the stream's title
streamer_name = stream["user_name"]
stream_title = stream["title"]
# Set up the payload for the Discord API request
payload = {
"username": "Warframe Stream Alert",
"content": f"{streamer_name} is streaming Warframe with drops enabled!\n\n{stream_title}"
}
# Make the Discord API request
requests.post(discord_api_url, json=payload)
print(f"Notified Discord of stream by {streamer_name} with drops enabled.")
else:
print("Failed to get stream data from Twitch API")
# Wait for 2 minutes before checking again
time.sleep(120)