Hey everyone! I’m trying to make a Python script in PyCharm that automatically records my favorite Twitch streamer when they go live. Here’s what I’ve got so far:
import requests
import time
import os
from datetime import datetime
streamer = 'coolstreamer123'
video_quality = 'high'
save_location = 'D:/TwitchVODs'
os.makedirs(save_location, exist_ok=True)
while True:
try:
stream_info = requests.get(f'https://api.twitch.tv/helix/streams?user_login={streamer}').json()
if stream_info['data']:
stream_link = f'https://www.twitch.tv/{streamer}'
filename = f'{streamer}_{datetime.now().strftime("%Y%m%d_%H%M%S")}.mp4'
full_path = os.path.join(save_location, filename)
print('Stream is live! Starting to record...')
# Code to start recording goes here
else:
print('Stream is offline. Checking again in a minute...')
time.sleep(60)
except Exception as e:
print(f'Error occurred: {e}')
time.sleep(60)
The problem is, it keeps saying the stream is offline even when I know for sure the streamer is live. I’ve already tried reinstalling Python and all the necessary packages, but no luck. Any ideas what might be going wrong? Thanks in advance for any help!
I encountered a similar issue when working on a Twitch recording project. The problem likely stems from incorrect API authentication. Twitch recently updated their API requirements, and now you need both a Client-ID and an OAuth token.
To fix this, first register your application on the Twitch Developer Console to obtain a Client-ID. Then, generate an OAuth token using that Client-ID. Include both in your request headers:
headers = {
‘Client-ID’: ‘your_client_id’,
‘Authorization’: ‘Bearer your_oauth_token’
}
Use these headers in your API request. This should resolve the ‘offline’ issue you’re experiencing.
For recording, consider using streamlink or youtube-dl libraries. They’re robust and handle various stream qualities effectively.
I’ve been working on a similar project, and I can tell you that the Twitch API can be tricky. Besides the Client-ID that bellagarcia mentioned, you’ll also need an OAuth token. Here’s what worked for me:
- Register your app on the Twitch Developer Console to get a Client-ID.
- Generate an OAuth token using your Client-ID.
- Include both in your headers like this:
headers = {
‘Client-ID’: ‘your_client_id’,
‘Authorization’: ‘Bearer your_oauth_token’
}
Then use these headers in your request. Also, make sure you’re handling rate limits properly. Twitch can be pretty strict about that.
One more thing - for recording, I found StreamLink to be really reliable. It handles quality selection and connection issues well. Good luck with your project!
hey there! looks like ur missing a crucial part - the twitch API key. u need to authenticate ur requests to get accurate stream info. try adding a client-id header to ur request:
requests.get(f’https://api.twitch.tv/helix/streams?user_login={streamer}', headers={‘Client-ID’: ‘your_client_id_here’})
hope this helps! lemme kno if u need more help 