Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit d1e89ed

Browse files
authored
Merge pull request #25 from teampheenix/new-twitch-api
Use new Twitch helix API to update stream title
2 parents 3540986 + d7b6f8a commit d1e89ed

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

scctool/tasks/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_auth_url_twitch(cls, state):
9797
"response_type": "token",
9898
"state": state,
9999
"redirect_uri": TWITCH_REDIRECT_URI,
100-
"scope": "channel_editor"}
100+
"scope": "channel:manage:broadcast"}
101101
url = "https://id.twitch.tv/oauth2/authorize?" + \
102102
urllib.parse.urlencode(params)
103103
return url

scctool/tasks/twitch.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33

44
import requests
5+
from requests.api import request
56

67
import scctool.settings
78
import scctool.settings.translation
@@ -17,26 +18,28 @@
1718
def updateTitle(newTitle):
1819
"""Update the twitch title to the title specified in the config file."""
1920
global previousTitle
21+
twitchChannel = scctool.settings.config.parser.get(
22+
"Twitch", "Channel").strip()
2023

2124
try:
22-
twitchChannel = scctool.settings.config.parser.get(
23-
"Twitch", "Channel").strip()
2425
userID = getUserID(twitchChannel)
2526

2627
clientID = scctool.settings.safe.get('twitch-client-id')
2728
oauth = scctool.settings.config.parser.get("Twitch", "oauth")
2829

2930
headers = {'Accept': 'application/vnd.twitchtv.v5+json',
30-
'Authorization': f'OAuth {oauth}',
31-
'Client-ID': clientID}
31+
'Authorization': f'Bearer {oauth}',
32+
'Client-Id': clientID}
3233

33-
params = {'channel[status]': newTitle}
34+
data = {'title': newTitle}
35+
36+
params = {'broadcaster_id': userID}
3437

3538
if scctool.settings.config.parser.getboolean("Twitch", "set_game"):
36-
params['channel[game]'] = 'StarCraft II'
39+
data['game_id'] = '490422'
3740

38-
requests.put(f'https://api.twitch.tv/kraken/channels/{userID}',
39-
headers=headers, params=params).raise_for_status()
41+
requests.patch(
42+
url=f'https://api.twitch.tv/helix/channels', headers=headers, params=params, json=data).raise_for_status()
4043
msg = _('Updated Twitch title of {} to: "{}"').format(
4144
twitchChannel, newTitle)
4245
success = True
@@ -72,12 +75,16 @@ def getUserID(login):
7275
client_id = scctool.settings.safe.get('twitch-client-id')
7376
url = 'https://api.twitch.tv/helix/users'
7477
oauth = scctool.settings.config.parser.get("Twitch", "oauth")
75-
headers = {'Client-ID': client_id, 'Authorization': f'Bearer {oauth}'}
78+
headers = {'Client-Id': client_id, 'Authorization': f'Bearer {oauth}'}
7679
params = {'login': login}
7780

7881
r = requests.get(url, headers=headers, params=params)
7982
r.raise_for_status()
80-
return r.json().get('data')[0]['id']
83+
try:
84+
return r.json().get('data')[0]['id']
85+
except IndexError:
86+
raise ValueError(
87+
f'Twitch channel "{login}" not found - please check your settings.')
8188

8289

8390
# def addCommunity(channelID):

0 commit comments

Comments
 (0)