Skip to content

Commit 4d87b9f

Browse files
committed
twitch checker rewrite to hopefully be faster
1 parent 24c5a6c commit 4d87b9f

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

DefaultConstants.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,4 @@ class Constants:
231231
# Calm is default bot avatar, pissed is what it changes to after MIN_TIME_BEFORE_AVATAR_CHANGE has been met
232232
# Make them the same image if you don't want the feature to change anything
233233
calmAvatar = 'images/avatars/calmStreamer.png'
234-
pissedAvatar = 'images/avatars/pissedStreamer.png'
235-
236-
# In some cases a Twitch channel will show online when they aren't. If that's the case turn this to TRUE.
237-
# 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
238-
twitchCheckThumbnail = False
234+
pissedAvatar = 'images/avatars/pissedStreamer.png'

checkers/Twitch.py

+19-26
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,45 @@
1212
logger = logging.getLogger(__name__)
1313
logger.setLevel(Constants.SASSBOT_LOG_LEVEL)
1414

15-
def isModelOnline(twitchChannelName):
15+
def isModelOnline(twitchChannelName: str):
16+
twitchChannelName = twitchChannelName.lower()
1617
title = "placeholder twitch title"
1718
tempThumbUrl = ''
1819
isOnline = False
1920
icon = Constants.defaultIcon
2021
try:
21-
page = requests.get(f'https://www.twitch.tv/{twitchChannelName}')
22+
tempThumbUrl = f'https://static-cdn.jtvnw.net/previews-ttv/live_user_{twitchChannelName}-640x360.jpg'
23+
thumbUrlReq = requests.get(tempThumbUrl,allow_redirects=True)
2224
time.sleep(1)
23-
soup = BeautifulSoup(page.content, "html.parser")
24-
twitchJson = getTwitchJson(soup)
25-
if twitchJson:
26-
title = twitchJson['@graph'][0]['description']
27-
tempThumbUrl = twitchJson['@graph'][0]['thumbnailUrl'][2]
25+
if tempThumbUrl == thumbUrlReq.url:
26+
isOnline = True
27+
page = requests.get(f'https://www.twitch.tv/{twitchChannelName}')
28+
time.sleep(1)
29+
soup = BeautifulSoup(page.content, "html.parser")
30+
title = getTitle(soup)
2831
reticon = getIcon(soup)
2932
if reticon:
3033
icon = reticon
31-
thumbUrlReq = requests.get(tempThumbUrl,allow_redirects=True)
32-
time.sleep(1)
33-
isOnlineJson = twitchJson['@graph'][0]['publication']['isLiveBroadcast']
34-
logger.debug(f"IsOnline: {isOnlineJson}")
35-
logger.debug(f"ThumbUrl: {tempThumbUrl}")
36-
logger.debug(f"ThumbReqUrl:{thumbUrlReq.url}")
37-
thumbnailGood = tempThumbUrl == thumbUrlReq.url if Constants.twitchCheckThumbnail else True
38-
if isOnlineJson and thumbnailGood:
39-
tempThumbUrl = tempThumbUrl + "?" + str(int(time.time()))
40-
isOnline = True
34+
tempThumbUrl = tempThumbUrl + "?" + str(int(time.time()))
4135
except requests.exceptions.ConnectTimeout:
4236
logger.warning("connection timed out to Twitch. Bot detection or rate limited?")
4337
except requests.exceptions.SSLError:
4438
logger.warning("SSL Error when attempting to connect to Twitch")
4539
thumbUrl = GetThumbnail(tempThumbUrl, Constants.twitchThumbnail)
4640
return isOnline, title, thumbUrl, icon
4741

48-
def getTwitchJson(soup):
49-
twitchJson = 0
50-
try:
51-
twitchJson = soup.find_all("script", type="application/ld+json")
52-
twitchJson = json.loads(twitchJson[0].text)
53-
except IndexError:
54-
pass
55-
return twitchJson
56-
5742
def getIcon(soup):
5843
icon = 0
5944
try:
6045
icon = soup.find("meta", property="og:image")['content']
6146
except IndexError:
6247
pass
6348
return icon
49+
50+
def getTitle(soup):
51+
title = "placeholder twitch title"
52+
try:
53+
title = soup.find("meta", property="og:description")['content']
54+
except IndexError:
55+
pass
56+
return title

0 commit comments

Comments
 (0)