Added first working chat recorder
This commit is contained in:
@@ -1,24 +1,11 @@
|
||||
import datetime
|
||||
import enum
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import requests
|
||||
import time
|
||||
|
||||
HELIX_STREAM_URL = "https://api.twitch.tv/helix/streams?user_login={0}"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TwitchStreamStatus(enum.Enum):
|
||||
ONLINE = 0
|
||||
OFFLINE = 1
|
||||
NOT_FOUND = 2
|
||||
UNAUTHORIZED = 3
|
||||
ERROR = 4
|
||||
|
||||
|
||||
class TwitchVideoRecorder:
|
||||
access_token = None
|
||||
|
||||
@@ -49,64 +36,7 @@ class TwitchVideoRecorder:
|
||||
self.refresh_timeout = 15
|
||||
logger.warning("check interval set to 15 seconds")
|
||||
|
||||
self.loop_check(recording_path)
|
||||
|
||||
def loop_check(self, recording_path):
|
||||
logger.info("checking for %s every %s seconds, recording with %s quality",
|
||||
self.streamer_name, self.refresh_timeout, self.quality)
|
||||
while True:
|
||||
status, info = self.check_user()
|
||||
if status == TwitchStreamStatus.NOT_FOUND:
|
||||
logger.error("streamer_name not found, invalid streamer_name or typo")
|
||||
time.sleep(self.refresh_timeout)
|
||||
elif status == TwitchStreamStatus.ERROR:
|
||||
logger.error("%s unexpected error. will try again in 5 minutes",
|
||||
datetime.datetime.now().strftime("%Hh%Mm%Ss"))
|
||||
time.sleep(300)
|
||||
elif status == TwitchStreamStatus.OFFLINE:
|
||||
logger.info("%s currently offline, checking again in %s seconds", self.streamer_name,
|
||||
self.refresh_timeout)
|
||||
time.sleep(self.refresh_timeout)
|
||||
elif status == TwitchStreamStatus.UNAUTHORIZED:
|
||||
logger.info("unauthorized, will attempt to log back in immediately")
|
||||
self.access_token = self.authenticator.refresh_token()
|
||||
elif status == TwitchStreamStatus.ONLINE:
|
||||
logger.info("%s online, stream recording in session", self.streamer_name)
|
||||
|
||||
channels = info["data"]
|
||||
channel = next(iter(channels), None)
|
||||
|
||||
recorded_filename = self.record_stream(channel, recording_path)
|
||||
|
||||
logger.info("recording stream is done")
|
||||
|
||||
if self.on_finish is not None:
|
||||
self.on_finish(channel, recorded_filename)
|
||||
|
||||
time.sleep(self.refresh_timeout)
|
||||
|
||||
# TODO use twitch library instead of pure requests
|
||||
def check_user(self):
|
||||
|
||||
info = None
|
||||
status = TwitchStreamStatus.ERROR
|
||||
try:
|
||||
headers = {"Client-ID": self.authenticator.client_id,
|
||||
"Authorization": "Bearer {}".format(self.authenticator.get_token())}
|
||||
r = requests.get(HELIX_STREAM_URL.format(self.streamer_name), headers=headers, timeout=15)
|
||||
r.raise_for_status()
|
||||
info = r.json()
|
||||
if info is None or not info["data"]:
|
||||
status = TwitchStreamStatus.OFFLINE
|
||||
else:
|
||||
status = TwitchStreamStatus.ONLINE
|
||||
except requests.exceptions.RequestException as e:
|
||||
if e.response:
|
||||
if e.response.status_code == 401:
|
||||
status = TwitchStreamStatus.UNAUTHORIZED
|
||||
if e.response.status_code == 404:
|
||||
status = TwitchStreamStatus.NOT_FOUND
|
||||
return status, info
|
||||
self.record_stream(self.streamer_name, recording_path)
|
||||
|
||||
def record_stream(self, channel, recording_path):
|
||||
filename = self.streamer_name + " - " + datetime.datetime.now() \
|
||||
|
||||
Reference in New Issue
Block a user