Skip to content

Commit

Permalink
Temp log InvalidURL occurences
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilXD committed Jul 24, 2024
1 parent 0f3a784 commit a708816
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import asyncio
import logging
from typing import Any, SupportsInt, TYPE_CHECKING
from typing import Any, SupportsInt, cast, TYPE_CHECKING

import aiohttp
from yarl import URL

from utils import Game
from exceptions import MinerException
Expand Down Expand Up @@ -81,16 +84,21 @@ async def get_stream_url(self) -> URLType:
token_value = token_data["value"]
token_signature = token_data["signature"]
# using the token, query Twitch for a list of all available stream qualities
async with self.channel._twitch.request(
"GET",
URLType(
"https://usher.ttvnw.net/api/channel/hls/"
f"{self.channel._login}.m3u8?sig={token_signature}&token={token_value}"
),
) as qualities_response:
available_qualities: str = await qualities_response.text()
# pick the last URL from the list, usually with the lowest quality stream
self._stream_url = URLType(available_qualities.strip().split("\n")[-1])
available_qualities: str = ''
try:
async with self.channel._twitch.request(
"GET",
URL(
"https://usher.ttvnw.net/api/channel/hls/"
f"{self.channel._login}.m3u8?sig={token_signature}&token={token_value}"
),
) as qualities_response:
available_qualities = await qualities_response.text()
# pick the last URL from the list, usually with the lowest quality stream
self._stream_url = cast(URLType, URL(available_qualities.strip().split("\n")[-1]))
except (aiohttp.InvalidURL, ValueError):
self.channel._twitch.print(available_qualities)
raise
return self._stream_url


Expand Down Expand Up @@ -358,19 +366,25 @@ async def send_watch(self) -> bool:
# fetch a list of chunks available to download for the stream
# NOTE: the CDN is configured to forcibly disconnect shortly after serving the list,
# if we don't do it yourselves. Lets help it by actually doing it ourselves instead.
async with self._twitch.request(
"GET", stream_url, headers={"Connection": "close"}
) as chunks_response:
available_chunks: str = await chunks_response.text()
# the list contains ~10-13 chunks of the stream at 2s intervals,
# pick the last chunk URL available. Ensure it's not the end-of-stream tag,
# otherwise use the 2nd to last line.
chunks_list: list[str] = available_chunks.strip().split("\n")
selected_chunk: str = chunks_list[-1]
if selected_chunk == "#EXT-X-ENDLIST":
selected_chunk = chunks_list[-2]
stream_chunk_url: URLType = URLType(selected_chunk)
# sending a HEAD request is enough to advance the drops,
# without downloading the actual stream data
async with self._twitch.request("HEAD", stream_chunk_url) as head_response:
return head_response.status == 200
available_chunks: str = ''
try:
async with self._twitch.request(
"GET", stream_url, headers={"Connection": "close"}
) as chunks_response:
available_chunks = await chunks_response.text()
# the list contains ~10-13 chunks of the stream at 2s intervals,
# pick the last chunk URL available. Ensure it's not the end-of-stream tag,
# otherwise use the 2nd to last line.
chunks_list: list[str] = available_chunks.strip().split("\n")
selected_chunk: str = chunks_list[-1]
if selected_chunk == "#EXT-X-ENDLIST":
selected_chunk = chunks_list[-2]
stream_chunk_url: URLType = URLType(selected_chunk)
# sending a HEAD request is enough to advance the drops,
# without downloading the actual stream data
async with self._twitch.request("HEAD", stream_chunk_url) as head_response:
return head_response.status == 200
except aiohttp.InvalidURL:
# Temporarily log the entire response into the output
self._twitch.print(available_chunks)
raise

0 comments on commit a708816

Please sign in to comment.