From 81b257ea2d5e66a68f045f4413f753fcddb482e4 Mon Sep 17 00:00:00 2001 From: Lakasabasz Date: Thu, 23 Aug 2018 14:19:25 +0200 Subject: [PATCH] Pop up notification --- start-withgui.bat | 5 +++ start.bat | 2 +- twitchlivenotifier/__init__.py | 65 ++++++++++++++++++++-------------- withoutGUI.pyw | 17 +++++++++ 4 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 start-withgui.bat create mode 100644 withoutGUI.pyw diff --git a/start-withgui.bat b/start-withgui.bat new file mode 100644 index 0000000..3ca3a6f --- /dev/null +++ b/start-withgui.bat @@ -0,0 +1,5 @@ +REM Debug +python withoutGUI.pyw + +REM Release +REM start withoutGUI.pyw \ No newline at end of file diff --git a/start.bat b/start.bat index 3a4fd1e..9a5bff7 100644 --- a/start.bat +++ b/start.bat @@ -1 +1 @@ -start twitchlivenotifier\__init__.py +python twitchlivenotifier\__init__.py diff --git a/twitchlivenotifier/__init__.py b/twitchlivenotifier/__init__.py index acfe969..e97f208 100644 --- a/twitchlivenotifier/__init__.py +++ b/twitchlivenotifier/__init__.py @@ -30,6 +30,7 @@ twitch_user = None stream_api_url = None stream_url = None +discord_enable = None discord_url = None discord_message = None lock = None @@ -70,6 +71,14 @@ def config(): sys.exit() global discord_url + try: + discord_enable = discord_config['Enable'] + except: + print('Enable not found in Discord section of config file. Please set Url under [Discord] in config.ini') + print('This can be found by editing a Discord channel, selecting Webhooks, and creating a hook.') + sys.exit() + + global discord_message try: discord_url = discord_config['Url'] except: @@ -105,32 +114,7 @@ def lock(): print("Failed to acquire lock, terminating...") sys.exit() - -def main(): - twitch_json = {} - while twitch_json.get('stream', None) is None: - twitch_headers = {'Client-ID': twitch_client_id} - twitch_request = requests.get(stream_api_url, headers=twitch_headers) - twitch_json = twitch_request.json() - - if twitch_json['stream'] is not None: - print("Stream is live.") - - stream_title = twitch_json['stream']['channel']['status'] - stream_game = twitch_json['stream']['channel']['game'] - stream_logo = twitch_json['stream']['channel']['logo'] - - game_search_url = "https://api.twitch.tv/kraken/search/games?query=" + urllib.parse.quote_plus(stream_game) - game_headers = {'Client-ID': twitch_client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} - game_logo_request = requests.get(game_search_url, headers=game_headers) - search_response = game_logo_request.json() - if search_response.get('games'): - if len(search_response.get('games')) > 0: - game_logo = search_response.get('games')[0]['box']['large'] - logo_request = requests.get(game_logo) - if '404' not in logo_request.url: - stream_logo = game_logo - +def discord(stream_title, stream_game, stream_logo): # Scrub ./ from the boxart URL if present so it works with the Discord API properly stream_logo = stream_logo.replace('./', '') @@ -166,6 +150,35 @@ def main(): else: print("Failed to call Discord API. Waiting 5 seconds to retry...") time.sleep(5) + +def main(): + twitch_json = {} + while twitch_json.get('stream', None) is None: + twitch_headers = {'Client-ID': twitch_client_id} + twitch_request = requests.get(stream_api_url, headers=twitch_headers) + twitch_json = twitch_request.json() + + if twitch_json['stream'] is not None: + print("Stream is live.") + + stream_title = twitch_json['stream']['channel']['status'] + stream_game = twitch_json['stream']['channel']['game'] + stream_logo = twitch_json['stream']['channel']['logo'] + + game_search_url = "https://api.twitch.tv/kraken/search/games?query=" + urllib.parse.quote_plus(stream_game) + game_headers = {'Client-ID': twitch_client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} + game_logo_request = requests.get(game_search_url, headers=game_headers) + search_response = game_logo_request.json() + if search_response.get('games'): + if len(search_response.get('games')) > 0: + game_logo = search_response.get('games')[0]['box']['large'] + logo_request = requests.get(game_logo) + if '404' not in logo_request.url: + stream_logo = game_logo + + if "Enable" == discord_enable: + discord(stream_title, stream_game, stream_logo) + else: print("Stream is not live. Waiting 5 seconds to retry...") time.sleep(5) diff --git a/withoutGUI.pyw b/withoutGUI.pyw new file mode 100644 index 0000000..82896ee --- /dev/null +++ b/withoutGUI.pyw @@ -0,0 +1,17 @@ +import twitchlivenotifier as tln +import tkinter as tk + +def popUp(): + root = tk.Tk() + tk.Label(root, text="Your streamer is live", fg="Red", font="Arial 20 bold").pack() + root.mainloop() + +tln.config() +tln.lock() + +root = tk.Tk() +tk.Label(root, text="Twitch Live Notivier is active, now you can close window", fg="Green", font="Arial 15").pack() +root.mainloop() + +tln.main() +popUp()