-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTwitch.py
63 lines (59 loc) · 2.26 KB
/
Twitch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
import json
from bs4 import BeautifulSoup
import time
import logging
try:
from AppConstants import Constants as Constants
except ImportError:
from DefaultConstants import Constants as Constants
from utils.StaticMethods import GetThumbnail
logger = logging.getLogger(__name__)
logger.setLevel(Constants.SASSBOT_LOG_LEVEL)
def isModelOnline(twitchChannelName):
title = "placeholder twitch title"
tempThumbUrl = ''
isOnline = False
icon = Constants.defaultIcon
try:
page = requests.get(f'https://www.twitch.tv/{twitchChannelName}')
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]
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
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