Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Deploy] Deploy v1.0.4 #32

Merged
merged 13 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chzzkpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
__author__ = "gunyu1019"
__license__ = "MIT"
__copyright__ = "Copyright 2024-present gunyu1019"
__version__ = "1.0.3" # version_info.to_string()
__version__ = "1.0.4" # version_info.to_string()


class VersionInfo(NamedTuple):
Expand All @@ -57,5 +57,5 @@ def to_string(self) -> str:


version_info: VersionInfo = VersionInfo(
major=1, minor=0, micro=3, release_level=None, serial=0
major=1, minor=0, micro=4, release_level=None, serial=0
)
2 changes: 1 addition & 1 deletion chzzkpy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
VideoDonationExtra,
MissionDonationExtra,
SubscriptionExtra,
SystemExtraParameter,
NoticeExtra,
SystemExtra,
SystemExtraParameter,
)
from .profile import Profile, ActivityBadge, StreamingProperty, Badge
from .recent_chat import RecentChat
58 changes: 54 additions & 4 deletions chzzkpy/chat/chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

from __future__ import annotations

import aiohttp
import asyncio
import datetime
import logging
from typing import Any, Optional, Callable, Coroutine, TYPE_CHECKING

import aiohttp
from typing import Any, Optional, Callable, Coroutine, Literal, TYPE_CHECKING

from .enums import ChatCmd
from .error import ChatConnectFailed
Expand All @@ -36,6 +36,7 @@
from .state import ConnectionState
from ..client import Client
from ..error import LoginRequired
from ..live import LiveDetail, LiveStatus
from ..http import ChzzkAPISession

if TYPE_CHECKING:
Expand Down Expand Up @@ -84,6 +85,7 @@ def __init__(
dispatch=self.dispatch, handler=handler, client=self
)
self._gateway: Optional[ChzzkWebSocket] = None
self._status: Literal["OPEN", "CLOSE"] = None

def _session_initial_set(self):
self._api_session = ChzzkAPISession(loop=self.loop)
Expand Down Expand Up @@ -115,6 +117,7 @@ async def connect(self) -> None:
if status is None:
raise ChatConnectFailed(self.channel_id)
self.chat_channel_id = status.chat_channel_id
self._status = status.status

if self._game_session.has_login:
user = await self.user()
Expand All @@ -130,10 +133,31 @@ async def close(self):
self._ready.clear()

if self._gateway is not None:
await self._gateway.socket.close()
await self._gateway.close()
await self.ws_session.close()
await super().close()

async def _confirm_live_status(self):
live_status = await self.live_status(channel_id=self.channel_id)
if live_status is None:
return

if self._status != live_status.status:
self._status = live_status.status
if self._status == "OPEN":
self.dispatch("broadcast_open")
elif self._status == "CLOSE":
self.dispatch("broadcast_close")

if live_status.chat_channel_id == self.chat_channel_id:
return

_log.debug("A chat_channel_id has been updated. Reconnect websocket.")
await self._gateway.close()

self.chat_channel_id = live_status.chat_channel_id
raise ReconnectWebsocket()

async def polling(self) -> None:
session_id: Optional[str] = None
while not self.is_closed:
Expand All @@ -152,10 +176,22 @@ async def polling(self) -> None:
)
session_id = self._gateway.session_id

last_check_time = datetime.datetime.now()

while True:
await self._gateway.poll_event()

# Confirm chat-channel-id with live_status() method.
# When a streamer starts a new broadcast, a chat-channel-id will regenrated.
#
# https://github.com/gunyu1019/chzzkpy/issues/31
relative_time = datetime.datetime.now() - last_check_time
if relative_time.total_seconds() >= 59:
last_check_time = datetime.datetime.now()
await self._confirm_live_status()
except ReconnectWebsocket:
self.dispatch("disconnect")
session_id = None
continue

# Event Handler
Expand Down Expand Up @@ -411,3 +447,17 @@ async def blind_message(self, message: ChatMessage) -> None:
streaming_channel_id=message.extras.streaming_channel_id,
)
return

async def live_status(
self, channel_id: Optional[str] = None
) -> Optional[LiveStatus]:
if channel_id is None:
channel_id = self.channel_id
return await super().live_status(channel_id)

async def live_detail(
self, channel_id: Optional[str] = None
) -> Optional[LiveDetail]:
if channel_id is None:
channel_id = self.channel_id
return await super().live_detail(channel_id)
4 changes: 4 additions & 0 deletions chzzkpy/chat/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def set_hook(self, cmd: ChatCmd, coro_func: Callable[..., Any]):
def remove_hook(self, cmd: ChatCmd):
self._event_hook[cmd] = None

async def close(self):
self.session_id = None
await self.socket.close()

@classmethod
async def new_session(
cls,
Expand Down
3 changes: 3 additions & 0 deletions chzzkpy/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ChzzkpyException(Exception):

class LoginRequired(ChzzkpyException):
"""Exception that’s raised when a method need login."""

def __init__(self):
super(LoginRequired, self).__init__(
"This method(feature) needs to login. Please use `login()` method."
Expand All @@ -39,6 +40,7 @@ def __init__(self):

class NotFound(ChzzkpyException):
"""Exception that’s raised for when status code 404 occurs."""

def __init__(self, message: Optional[str] = None):
if message is None:
message = "Not Found"
Expand All @@ -47,6 +49,7 @@ def __init__(self, message: Optional[str] = None):

class HTTPException(ChzzkpyException):
"""Exception that’s raised when an HTTP request operation fails."""

def __init__(self, code: int, message: Optional[str] = None):
if message is None:
message = f"Reponsed error code ({code})"
Expand Down
Loading