|
12 | 12 | logger = logging.getLogger(__name__)
|
13 | 13 | logger.setLevel(Constants.SASSBOT_LOG_LEVEL)
|
14 | 14 |
|
15 |
| -def isModelOnline(twitchChannelName): |
| 15 | +def isModelOnline(twitchChannelName: str): |
| 16 | + twitchChannelName = twitchChannelName.lower() |
16 | 17 | title = "placeholder twitch title"
|
17 | 18 | tempThumbUrl = ''
|
18 | 19 | isOnline = False
|
19 | 20 | icon = Constants.defaultIcon
|
20 | 21 | 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) |
22 | 24 | 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) |
28 | 31 | reticon = getIcon(soup)
|
29 | 32 | if reticon:
|
30 | 33 | 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())) |
41 | 35 | except requests.exceptions.ConnectTimeout:
|
42 | 36 | logger.warning("connection timed out to Twitch. Bot detection or rate limited?")
|
43 | 37 | except requests.exceptions.SSLError:
|
44 | 38 | logger.warning("SSL Error when attempting to connect to Twitch")
|
45 | 39 | thumbUrl = GetThumbnail(tempThumbUrl, Constants.twitchThumbnail)
|
46 | 40 | return isOnline, title, thumbUrl, icon
|
47 | 41 |
|
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 |
| - |
57 | 42 | def getIcon(soup):
|
58 | 43 | icon = 0
|
59 | 44 | try:
|
60 | 45 | icon = soup.find("meta", property="og:image")['content']
|
61 | 46 | except IndexError:
|
62 | 47 | pass
|
63 | 48 | 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