diff --git a/DefaultConstants.py b/DefaultConstants.py index 9d73a70..b92d29b 100644 --- a/DefaultConstants.py +++ b/DefaultConstants.py @@ -231,8 +231,4 @@ class Constants: # Calm is default bot avatar, pissed is what it changes to after MIN_TIME_BEFORE_AVATAR_CHANGE has been met # Make them the same image if you don't want the feature to change anything calmAvatar = 'images/avatars/calmStreamer.png' - pissedAvatar = 'images/avatars/pissedStreamer.png' - - # In some cases a Twitch channel will show online when they aren't. If that's the case turn this to TRUE. - # This checks the channel's thumbnail to see if it is actually online with the downside of it taking a while longer to show the streamer as online - twitchCheckThumbnail = False \ No newline at end of file + pissedAvatar = 'images/avatars/pissedStreamer.png' \ No newline at end of file diff --git a/checkers/Twitch.py b/checkers/Twitch.py index 550de5e..beb97df 100644 --- a/checkers/Twitch.py +++ b/checkers/Twitch.py @@ -12,32 +12,26 @@ logger = logging.getLogger(__name__) logger.setLevel(Constants.SASSBOT_LOG_LEVEL) -def isModelOnline(twitchChannelName): +def isModelOnline(twitchChannelName: str): + twitchChannelName = twitchChannelName.lower() title = "placeholder twitch title" tempThumbUrl = '' isOnline = False icon = Constants.defaultIcon try: - page = requests.get(f'https://www.twitch.tv/{twitchChannelName}') + tempThumbUrl = f'https://static-cdn.jtvnw.net/previews-ttv/live_user_{twitchChannelName}-640x360.jpg' + thumbUrlReq = requests.get(tempThumbUrl,allow_redirects=True) time.sleep(1) - soup = BeautifulSoup(page.content, "html.parser") - twitchJson = getTwitchJson(soup) - if twitchJson: - title = twitchJson['@graph'][0]['description'] - tempThumbUrl = twitchJson['@graph'][0]['thumbnailUrl'][2] + if tempThumbUrl == thumbUrlReq.url: + isOnline = True + page = requests.get(f'https://www.twitch.tv/{twitchChannelName}') + time.sleep(1) + soup = BeautifulSoup(page.content, "html.parser") + title = getTitle(soup) reticon = getIcon(soup) if reticon: icon = reticon - thumbUrlReq = requests.get(tempThumbUrl,allow_redirects=True) - time.sleep(1) - isOnlineJson = twitchJson['@graph'][0]['publication']['isLiveBroadcast'] - logger.debug(f"IsOnline: {isOnlineJson}") - logger.debug(f"ThumbUrl: {tempThumbUrl}") - logger.debug(f"ThumbReqUrl:{thumbUrlReq.url}") - thumbnailGood = tempThumbUrl == thumbUrlReq.url if Constants.twitchCheckThumbnail else True - if isOnlineJson and thumbnailGood: - tempThumbUrl = tempThumbUrl + "?" + str(int(time.time())) - isOnline = True + tempThumbUrl = tempThumbUrl + "?" + str(int(time.time())) except requests.exceptions.ConnectTimeout: logger.warning("connection timed out to Twitch. Bot detection or rate limited?") except requests.exceptions.SSLError: @@ -45,15 +39,6 @@ def isModelOnline(twitchChannelName): thumbUrl = GetThumbnail(tempThumbUrl, Constants.twitchThumbnail) return isOnline, title, thumbUrl, icon -def getTwitchJson(soup): - twitchJson = 0 - try: - twitchJson = soup.find_all("script", type="application/ld+json") - twitchJson = json.loads(twitchJson[0].text) - except IndexError: - pass - return twitchJson - def getIcon(soup): icon = 0 try: @@ -61,3 +46,11 @@ def getIcon(soup): except IndexError: pass return icon + +def getTitle(soup): + title = "placeholder twitch title" + try: + title = soup.find("meta", property="og:description")['content'] + except IndexError: + pass + return title