Skip to content

Commit

Permalink
Remove debug, add WebcastBlocked200Error for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
isaackogan committed Sep 19, 2024
1 parent 64f3b05 commit adb5ba9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
7 changes: 7 additions & 0 deletions TikTokLive/client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ class WebsocketURLMissingError(RuntimeError):
Thrown when the websocket URL to connect to TikTok is missing (blocked)
"""


class WebcastBlocked200Error(RuntimeError):
"""
Thrown when the webcast is blocked by TikTok with a 200 status code (detected)
"""
1 change: 1 addition & 0 deletions TikTokLive/client/web/web_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"app_name": "tiktok_web",
"browser_language": Location["lang_country"],
"browser_name": Device["browser_name"],
"browser_version": Device["browser_version"],
"browser_online": "true",
"cookie_enabled": "true",
"imprp": "",
Expand Down
6 changes: 0 additions & 6 deletions TikTokLive/client/ws/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ def build_connection_args(
}
)

#base_config["uri"] = input("Enter the URI: ")
#base_config["extra_headers"]["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"

print(base_config['uri'])
print(base_config['extra_headers'])

if self._ws_proxy is not None:
base_config["proxy_conn_timeout"] = 10.0
base_config["proxy"] = self._convert_proxy()
Expand Down
22 changes: 16 additions & 6 deletions TikTokLive/client/ws/ws_connect.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Optional, AsyncIterator

from httpx import Proxy
from websockets import WebSocketClientProtocol
from websockets import WebSocketClientProtocol, InvalidStatusCode
from websockets.legacy.client import Connect
from websockets_proxy.websockets_proxy import ProxyConnect

from TikTokLive.client.errors import WebcastBlocked200Error
from TikTokLive.client.logger import TikTokLiveLogHandler


Expand All @@ -14,8 +15,13 @@ async def __aiter__(self) -> AsyncIterator[WebSocketClientProtocol]:
"""Custom implementation of async iterator that disables exception"""

while True:
async with self as protocol:
yield protocol
try:
async with self as protocol:
yield protocol
except InvalidStatusCode as ex:
if ex.status_code == 200:
raise WebcastBlocked200Error("WebSocket rejected by TikTok with a 200 status code, implying detection.") from ex
raise


class WebcastProxyConnect(ProxyConnect):
Expand All @@ -28,6 +34,10 @@ async def __aiter__(self) -> AsyncIterator[WebSocketClientProtocol]:
"""Custom implementation of async iterator that disables exception"""

while True:
async with self as protocol:
yield protocol

try:
async with self as protocol:
yield protocol
except InvalidStatusCode as ex:
if ex.status_code == 200:
raise WebcastBlocked200Error("WebSocket rejected by TikTok with a 200 status code, implying detection.") from ex
raise

0 comments on commit adb5ba9

Please sign in to comment.