Introduced authenticator
This commit is contained in:
3
analyser/chat.py
Normal file
3
analyser/chat.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class ChatAnalyser:
|
||||||
|
def __init__(self, streamer_name, ):
|
||||||
|
pass
|
||||||
28
clipper/auth.py
Normal file
28
clipper/auth.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
TOKEN_URL = "https://id.twitch.tv/oauth2/token?client_id={0}&client_secret={1}&grant_type=client_credentials"
|
||||||
|
|
||||||
|
|
||||||
|
class TwitchAuthenticator:
|
||||||
|
cached_token = None
|
||||||
|
|
||||||
|
def __init__(self, client_id, client_secret):
|
||||||
|
self.client_id = client_id
|
||||||
|
self.client_secret = client_secret
|
||||||
|
self.token_url = TOKEN_URL.format(self.client_id, self.client_secret)
|
||||||
|
|
||||||
|
def get_token(self):
|
||||||
|
if self.cached_token is None:
|
||||||
|
self._fetch_token()
|
||||||
|
|
||||||
|
return self.cached_token
|
||||||
|
|
||||||
|
def refresh_token(self):
|
||||||
|
self._fetch_token()
|
||||||
|
return self.cached_token
|
||||||
|
|
||||||
|
def _fetch_token(self):
|
||||||
|
token_response = requests.post(self.token_url, timeout=15)
|
||||||
|
token_response.raise_for_status()
|
||||||
|
token = token_response.json()
|
||||||
|
self.cached_token = token["access_token"]
|
||||||
@@ -3,10 +3,9 @@ import enum
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
|
||||||
import requests
|
import requests
|
||||||
|
import time
|
||||||
|
|
||||||
TOKEN_URL = "https://id.twitch.tv/oauth2/token?client_id={0}&client_secret={1}&grant_type=client_credentials"
|
|
||||||
HELIX_STREAM_URL = "https://api.twitch.tv/helix/streams?user_login={0}"
|
HELIX_STREAM_URL = "https://api.twitch.tv/helix/streams?user_login={0}"
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -23,7 +22,7 @@ class TwitchStreamerStatus(enum.Enum):
|
|||||||
class TwitchRecorder:
|
class TwitchRecorder:
|
||||||
access_token = None
|
access_token = None
|
||||||
|
|
||||||
def __init__(self, client_id, client_secret, username, output_path, quality="480p", on_download=None):
|
def __init__(self, authenticator, streamer_name, output_path, quality="480p", on_download=None):
|
||||||
# global configuration
|
# global configuration
|
||||||
self.disable_ffmpeg = False
|
self.disable_ffmpeg = False
|
||||||
self.refresh_timeout = 15
|
self.refresh_timeout = 15
|
||||||
@@ -32,21 +31,13 @@ class TwitchRecorder:
|
|||||||
self.on_download = on_download
|
self.on_download = on_download
|
||||||
|
|
||||||
# twitch configuration
|
# twitch configuration
|
||||||
self.username = username
|
self.streamer_name = streamer_name
|
||||||
self.quality = quality
|
self.quality = quality
|
||||||
self.client_id = client_id
|
self.authenticator = authenticator
|
||||||
self.client_secret = client_secret
|
|
||||||
self.token_url = TOKEN_URL.format(self.client_id, self.client_secret)
|
|
||||||
|
|
||||||
def fetch_access_token(self):
|
|
||||||
token_response = requests.post(self.token_url, timeout=15)
|
|
||||||
token_response.raise_for_status()
|
|
||||||
token = token_response.json()
|
|
||||||
return token["access_token"]
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# path to recorded stream
|
# path to recorded stream
|
||||||
recording_path = os.path.join(self.output_path, "recorded", self.username)
|
recording_path = os.path.join(self.output_path, "recorded", self.streamer_name)
|
||||||
|
|
||||||
# create directory for recordedPath and processedPath if not exist
|
# create directory for recordedPath and processedPath if not exist
|
||||||
if os.path.isdir(recording_path) is False:
|
if os.path.isdir(recording_path) is False:
|
||||||
@@ -58,29 +49,29 @@ class TwitchRecorder:
|
|||||||
self.refresh_timeout = 15
|
self.refresh_timeout = 15
|
||||||
logger.warning("check interval set to 15 seconds")
|
logger.warning("check interval set to 15 seconds")
|
||||||
|
|
||||||
self.access_token = self.fetch_access_token()
|
|
||||||
self.loop_check(recording_path)
|
self.loop_check(recording_path)
|
||||||
|
|
||||||
def loop_check(self, recording_path):
|
def loop_check(self, recording_path):
|
||||||
logger.info("checking for %s every %s seconds, recording with %s quality",
|
logger.info("checking for %s every %s seconds, recording with %s quality",
|
||||||
self.username, self.refresh_timeout, self.quality)
|
self.streamer_name, self.refresh_timeout, self.quality)
|
||||||
while True:
|
while True:
|
||||||
status, info = self.check_user()
|
status, info = self.check_user()
|
||||||
if status == TwitchStreamerStatus.NOT_FOUND:
|
if status == TwitchStreamerStatus.NOT_FOUND:
|
||||||
logger.error("username not found, invalid username or typo")
|
logger.error("streamer_name not found, invalid streamer_name or typo")
|
||||||
time.sleep(self.refresh_timeout)
|
time.sleep(self.refresh_timeout)
|
||||||
elif status == TwitchStreamerStatus.ERROR:
|
elif status == TwitchStreamerStatus.ERROR:
|
||||||
logger.error("%s unexpected error. will try again in 5 minutes",
|
logger.error("%s unexpected error. will try again in 5 minutes",
|
||||||
datetime.datetime.now().strftime("%Hh%Mm%Ss"))
|
datetime.datetime.now().strftime("%Hh%Mm%Ss"))
|
||||||
time.sleep(300)
|
time.sleep(300)
|
||||||
elif status == TwitchStreamerStatus.OFFLINE:
|
elif status == TwitchStreamerStatus.OFFLINE:
|
||||||
logger.info("%s currently offline, checking again in %s seconds", self.username, self.refresh_timeout)
|
logger.info("%s currently offline, checking again in %s seconds", self.streamer_name,
|
||||||
|
self.refresh_timeout)
|
||||||
time.sleep(self.refresh_timeout)
|
time.sleep(self.refresh_timeout)
|
||||||
elif status == TwitchStreamerStatus.UNAUTHORIZED:
|
elif status == TwitchStreamerStatus.UNAUTHORIZED:
|
||||||
logger.info("unauthorized, will attempt to log back in immediately")
|
logger.info("unauthorized, will attempt to log back in immediately")
|
||||||
self.access_token = self.fetch_access_token()
|
self.access_token = self.authenticator.refresh_token()
|
||||||
elif status == TwitchStreamerStatus.ONLINE:
|
elif status == TwitchStreamerStatus.ONLINE:
|
||||||
logger.info("%s online, stream recording in session", self.username)
|
logger.info("%s online, stream recording in session", self.streamer_name)
|
||||||
|
|
||||||
channels = info["data"]
|
channels = info["data"]
|
||||||
channel = next(iter(channels), None)
|
channel = next(iter(channels), None)
|
||||||
@@ -98,8 +89,9 @@ class TwitchRecorder:
|
|||||||
info = None
|
info = None
|
||||||
status = TwitchStreamerStatus.ERROR
|
status = TwitchStreamerStatus.ERROR
|
||||||
try:
|
try:
|
||||||
headers = {"Client-ID": self.client_id, "Authorization": "Bearer " + self.access_token}
|
headers = {"Client-ID": self.authenticator.client_id,
|
||||||
r = requests.get(HELIX_STREAM_URL.format(self.username), headers=headers, timeout=15)
|
"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()
|
r.raise_for_status()
|
||||||
info = r.json()
|
info = r.json()
|
||||||
if info is None or not info["data"]:
|
if info is None or not info["data"]:
|
||||||
@@ -115,7 +107,7 @@ class TwitchRecorder:
|
|||||||
return status, info
|
return status, info
|
||||||
|
|
||||||
def download_stream(self, channel, recording_path):
|
def download_stream(self, channel, recording_path):
|
||||||
filename = self.username + " - " + datetime.datetime.now() \
|
filename = self.streamer_name + " - " + datetime.datetime.now() \
|
||||||
.strftime("%Y-%m-%d %Hh%Mm%Ss") + " - " + channel.get("title") + ".mp4"
|
.strftime("%Y-%m-%d %Hh%Mm%Ss") + " - " + channel.get("title") + ".mp4"
|
||||||
|
|
||||||
filename = "".join(x for x in filename if x.isalnum() or x in [" ", "-", "_", "."])
|
filename = "".join(x for x in filename if x.isalnum() or x in [" ", "-", "_", "."])
|
||||||
@@ -125,7 +117,7 @@ class TwitchRecorder:
|
|||||||
subprocess.call([
|
subprocess.call([
|
||||||
"streamlink",
|
"streamlink",
|
||||||
"--twitch-disable-ads",
|
"--twitch-disable-ads",
|
||||||
"twitch.tv/" + self.username,
|
"twitch.tv/" + self.streamer_name,
|
||||||
self.quality,
|
self.quality,
|
||||||
"-o",
|
"-o",
|
||||||
recorded_filename
|
recorded_filename
|
||||||
6
main.py
6
main.py
@@ -3,7 +3,8 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from stream.downloader import TwitchRecorder
|
from clipper.auth import TwitchAuthenticator
|
||||||
|
from clipper.recorder import TwitchRecorder
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
def parse_arguments():
|
||||||
@@ -27,7 +28,8 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
args = parse_arguments()
|
args = parse_arguments()
|
||||||
|
|
||||||
rec = TwitchRecorder(args.tw_client, args.tw_secret, args.tw_streamer, args.output_path,
|
authenticator = TwitchAuthenticator(args.tw_client, args.tw_secret)
|
||||||
|
rec = TwitchRecorder(authenticator, args.tw_streamer, args.output_path,
|
||||||
args.tw_quality, on_download=on_downloaded)
|
args.tw_quality, on_download=on_downloaded)
|
||||||
|
|
||||||
rec.run()
|
rec.run()
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
requests==2.28.1
|
requests==2.28.1
|
||||||
streamlink==4.2.0
|
streamlink==4.2.0
|
||||||
|
twitch-python==0.0.20
|
||||||
Reference in New Issue
Block a user