Skip to content

Commit

Permalink
Update to v0.3 to support Twitch OAuth requirement via App Access Token.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunsmithy committed Jun 21, 2020
1 parent bb365bb commit 8e9f10a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ The included config_example.ini should give you a good idea of what the config.i

### Twitch
#### User
This is simply the username/handle of the streamer/broadcaster. It can be written in whatever case you would like it to appear in the below Discord message/description placeholders, as it will be converted to lowercase automatically for internal functionality.
This is simply the username/handle of the streamer/broadcaster.
It can be written in whatever case you would like it to appear in the below Discord message/description placeholders, as it will be converted to lowercase automatically for internal functionality.
#### ImagePriority
This is what image should be attempted to be used first for the message, Game or Preview. If the game logo or stream preview cannot be loaded, it will fall back to the user logo.
This is what image should be attempted to be used first for the message, Game or Preview.
If the game logo or stream preview cannot be loaded, it will fall back to the user logo.
#### ClientId
This is the Client ID you can get from the [Twitch Developers console.](https://dev.twitch.tv/login)
#### ClientSecret
This is the Client Secret you can get from the [Twitch Developers console.](https://dev.twitch.tv/login)

If you do not have the above ID and Secret, go to the [Twitch Developers console](https://dev.twitch.tv/login), log in, and register an application.
You can provide anything for the name and redirect URL, select any category, and upon creation you will get the ID and secret.
![Example Registration](https://i.imgur.com/ZKqJID9.png)

### Discord
#### Url
Expand Down
2 changes: 2 additions & 0 deletions config_example.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[Twitch]
User = Sasslyn
ImagePriority = Game
ClientId = abcdefghijklmnopqrstuvwxyz1234
ClientSecret = abcdefghijklmnopqrstuvwxyz1234

[Discord]
Url = https://discordapp.com/api/webhooks/ABunchOfRandomNumbers/ABunchOfRandomLettersAndNumbers
Expand Down
95 changes: 77 additions & 18 deletions twitchlivenotifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
Python script to notify a Discord server when the streamer goes live, with the current game and box art.
:copyright: (c) 2017-2018 Dylan Kauling
:copyright: (c) 2017-2020 Dylan Kauling
:license: GPLv3, see LICENSE for more details.
"""

__title__ = 'twitchlivenotifier'
__author__ = 'Dylan Kauling'
__license__ = 'GPLv3'
__copyright__ = 'Copyright 2017-2019 Dylan Kauling'
__version__ = '0.2'
__copyright__ = 'Copyright 2017-2020 Dylan Kauling'
__version__ = '0.3'

import time
import sys
Expand All @@ -24,12 +24,16 @@
import requests
import zc.lockfile

twitch_client_id = 'r5og8xrcb7c4r0b53tyijq2gvxgryp'
twitch_user = None
stream_api_url = None
stream_url = None
discord_url = None
discord_message = None
twitch_client_id = ''
twitch_secret_key = ''
twitch_app_token_json = {}
twitch_user = ''
image_priority = ''
stream_api_url = ''
stream_url = ''
discord_url = ''
discord_message = ''
discord_description = ''
lock = None


Expand Down Expand Up @@ -62,6 +66,25 @@ def config():
print('If the game logo or stream preview cannot be loaded, it will fall back to the user logo.')
sys.exit()

global twitch_client_id
try:
twitch_client_id = twitch_config['ClientId']
except KeyError:
print('ClientId not found in Twitch section of config file. Please set ClientId under [Twitch] in config.ini')
print('This is the Client ID you receive when registering an application as a Twitch developer.')
print('Please check the README for more instructions.')
sys.exit()

global twitch_secret_key
try:
twitch_secret_key = twitch_config['ClientSecret']
except KeyError:
print('ClientSecret not found in Twitch section of config file. Please set ClientSecret under [Twitch] in '
'config.ini')
print('This is the Client Secret you receive when registering an application as a Twitch developer.')
print('Please check the README for more instructions.')
sys.exit()

global stream_api_url
stream_api_url = "https://api.twitch.tv/helix/streams"

Expand Down Expand Up @@ -112,13 +135,34 @@ def get_lock():
sys.exit()


def authorize():
token_params = {
'client_id': twitch_client_id,
'client_secret': twitch_secret_key,
'grant_type': 'client_credentials',
}
app_token_request = requests.post('https://id.twitch.tv/oauth2/token', params=token_params)
global twitch_app_token_json
twitch_app_token_json = app_token_request.json()


def main():
twitch_json = {'data': []}
while len(twitch_json['data']) == 0:
twitch_headers = {'Client-ID': twitch_client_id}
twitch_headers = {
'Client-ID': twitch_client_id,
'Authorization': 'Bearer ' + twitch_app_token_json['access_token'],
}
twitch_params = {'user_login': twitch_user.lower()}
twitch_request = requests.get(stream_api_url, headers=twitch_headers, params=twitch_params)
twitch_json = twitch_request.json()
request_status = 401
while request_status == 401:
twitch_request = requests.get(stream_api_url, headers=twitch_headers, params=twitch_params)
request_status = twitch_request.status_code
if request_status == 401:
authorize()
twitch_headers['Authorization'] = 'Bearer ' + twitch_app_token_json['access_token']
continue
twitch_json = twitch_request.json()

if len(twitch_json['data']) == 1:
print("Stream is live.")
Expand All @@ -136,10 +180,17 @@ def main():
stream_preview = None

game_search_url = "https://api.twitch.tv/helix/games"
game_headers = {'Client-ID': twitch_client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
game_params = {'id': stream_game_id}
game_request = requests.get(game_search_url, headers=game_headers, params=game_params)
search_response = game_request.json()
search_response = {}
request_status = 401
while request_status == 401:
game_request = requests.get(game_search_url, headers=twitch_headers, params=game_params)
request_status = game_request.status_code
if request_status == 401:
authorize()
twitch_headers['Authorization'] = 'Bearer ' + twitch_app_token_json['access_token']
continue
search_response = game_request.json()

stream_game = "something"
game_logo = None
Expand All @@ -155,10 +206,17 @@ def main():
game_logo = game_logo_temp.replace('./', '')

user_search_url = "https://api.twitch.tv/helix/users"
user_headers = {'Client-ID': twitch_client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
user_params = {'login': twitch_user.lower()}
user_request = requests.get(user_search_url, headers=user_headers, params=user_params)
user_response = user_request.json()
user_response = {}
request_status = 401
while request_status == 401:
user_request = requests.get(user_search_url, headers=twitch_headers, params=user_params)
request_status = user_request.status_code
if request_status == 401:
authorize()
twitch_headers['Authorization'] = 'Bearer ' + twitch_app_token_json['access_token']
continue
user_response = user_request.json()

user_logo = None
print(str(user_response))
Expand Down Expand Up @@ -227,4 +285,5 @@ def main():
if __name__ == "__main__":
config()
get_lock()
authorize()
main()

0 comments on commit 8e9f10a

Please sign in to comment.