Initial commit
This commit is contained in:
79
app/main.py
Normal file
79
app/main.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
from loguru import logger
|
||||
from requests import HTTPError
|
||||
|
||||
from discord_client import DiscordClient
|
||||
from twitch_client import TwitchClient, StreamInformation
|
||||
|
||||
|
||||
class Main:
|
||||
is_live: bool = False
|
||||
_previous_stream: StreamInformation = None
|
||||
|
||||
def __init__(self):
|
||||
self.twitch_client = TwitchClient(streamer=os.environ["STREAMER_NAME"])
|
||||
self.twitch_client.update_access_token()
|
||||
self.profile_image = self.twitch_client.get_streamer_profile_picture()
|
||||
self.discord_client = DiscordClient()
|
||||
|
||||
def update_status(self):
|
||||
try:
|
||||
stream = self.twitch_client.get_stream()
|
||||
|
||||
if not stream:
|
||||
if self.is_live:
|
||||
logger.info("Streamer went offline.")
|
||||
self.is_live = False
|
||||
self.discord_client.finalize_information_on_discord(
|
||||
streamer_name=self._previous_stream.user_name,
|
||||
vod_url=self.twitch_client.get_vod(
|
||||
user_id=self._previous_stream.user_id
|
||||
),
|
||||
)
|
||||
return
|
||||
|
||||
if not self.is_live:
|
||||
logger.info("Streamer went live.")
|
||||
self.discord_client.send_information_to_discord(
|
||||
stream=stream, profile_image=self.profile_image
|
||||
)
|
||||
self.is_live = True
|
||||
else:
|
||||
logger.info("Streamer still live.")
|
||||
self.discord_client.update_information_on_discord(
|
||||
stream=stream, profile_image=self.profile_image
|
||||
)
|
||||
|
||||
self._previous_stream = stream
|
||||
except HTTPError as e:
|
||||
logger.exception(e)
|
||||
|
||||
def interrupt(self):
|
||||
if not self.is_live:
|
||||
return
|
||||
|
||||
self.is_live = False
|
||||
self.discord_client.finalize_information_on_discord(
|
||||
streamer_name=self._previous_stream.user_login,
|
||||
vod_url=self.twitch_client.get_vod(
|
||||
user_id=self._previous_stream.user_id
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger.info("Initiating main...")
|
||||
start_time = time.time()
|
||||
main = Main()
|
||||
|
||||
logger.info("Set-up looks correct, starting main loop.")
|
||||
try:
|
||||
while True:
|
||||
main.update_status()
|
||||
time.sleep(30.0 - ((time.time() - start_time) % 30.0))
|
||||
except (SystemExit, KeyboardInterrupt):
|
||||
logger.info("Caught wish to exit, interrupting and re-raising.")
|
||||
main.interrupt()
|
||||
raise
|
||||
Reference in New Issue
Block a user