Skip to content

Commit

Permalink
twitch checker rewrite to hopefully be faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Bombg committed Oct 18, 2024
1 parent 24c5a6c commit 4d87b9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
6 changes: 1 addition & 5 deletions DefaultConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
pissedAvatar = 'images/avatars/pissedStreamer.png'
45 changes: 19 additions & 26 deletions checkers/Twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,45 @@
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:
logger.warning("SSL Error when attempting to connect to Twitch")
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:
icon = soup.find("meta", property="og:image")['content']
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

0 comments on commit 4d87b9f

Please sign in to comment.