Skip to content

Commit

Permalink
Merge pull request #28 from gunyu1019/develop
Browse files Browse the repository at this point in the history
[Deploy] Deploy v1.0.3
  • Loading branch information
gunyu1019 authored Nov 10, 2024
2 parents 17624a4 + cf5ebd6 commit ab2dcb5
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 39 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ python3 -m pip install chzzkpy
py -3 -m pip install chzzkpy
```

To install the development version.
```bash
$ git clone https://github.com/gunyu1019/chzzk_py
$ chzzk_py
$ python3 -m pip install -U .
```

## Quick Example

`chzzkpy`를 사용한 예제는 [Examples](examples)에서 확인하실 수 있습니다.<br/>
Expand Down
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.2" # version_info.to_string()
__version__ = "1.0.3" # 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=2, release_level=None, serial=0
major=1, minor=0, micro=3, release_level=None, serial=0
)
9 changes: 7 additions & 2 deletions chzzkpy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
from .blind import Blind
from .chat_client import ChatClient
from .connected import ConnectedInfo
from .donation import (
ChatDonation,
VideoDonation,
MissionDonation,
)
from .enums import ChatType, ChatCmd, UserRole
from .error import *
from .message import (
Expand All @@ -33,11 +38,11 @@
SubscriptionMessage,
SystemMessage,
Extra,
NoticeExtra,
SystemExtra,
ChatDonationExtra,
VideoDonationExtra,
MissionDonationExtra,
NoticeExtra,
SystemExtra,
)
from .profile import Profile, ActivityBadge, StreamingProperty, Badge
from .recent_chat import RecentChat
2 changes: 1 addition & 1 deletion chzzkpy/chat/chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def connect(self) -> None:
if self.chat_channel_id is None:
status = await self.live_status(channel_id=self.channel_id)
if status is None:
raise ChatConnectFailed()
raise ChatConnectFailed(self.channel_id)
self.chat_channel_id = status.chat_channel_id

if self._game_session.has_login:
Expand Down
71 changes: 71 additions & 0 deletions chzzkpy/chat/donation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""MIT License
Copyright (c) 2024 gunyu1019
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from __future__ import annotations

import datetime

from typing import Optional, Literal
from pydantic import AliasChoices, Field

from ..base_model import ChzzkModel


class DonationRank(ChzzkModel):
user_id_hash: str
nickname: str = Field(validation_alias=AliasChoices("nickname", "nickName"))
verified_mark: bool
donation_amount: int
ranking: int


class BaseDonation(ChzzkModel):
is_anonymous: bool = True
pay_type: str
pay_amount: int = 0
donation_type: str
weekly_rank_list: Optional[list[DonationRank]] = Field(default_factory=list)
donation_user_weekly_rank: Optional[DonationRank] = None


class ChatDonation(BaseDonation):
donation_type: Literal["CHAT"]


class VideoDonation(BaseDonation):
donation_type: Literal["VIDEO"]


class MissionDonation(BaseDonation):
donation_type: Literal["MISSION"]
duration_time: int = 0
mission_donation_id: Optional[str] = None
mission_donation_type: Optional[str] = None # ALONE ?

mission_created_time: datetime.datetime
mission_start_time: Optional[datetime.datetime] = None
mission_end_time: Optional[datetime.datetime] = None
mission_text: str

status: str | Literal["PENDING", "REJECTED", "APPROVED", "COMPLETED"] = None
success: bool = False
43 changes: 10 additions & 33 deletions chzzkpy/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
from typing import Optional, Literal, TypeVar, Generic, TYPE_CHECKING, Any
from pydantic import AliasChoices, Field, Json, ConfigDict

from .donation import BaseDonation, ChatDonation, VideoDonation, MissionDonation
from .enums import ChatType
from .profile import Profile
from ..base_model import ChzzkModel

if TYPE_CHECKING:
from .chat_client import ChatClient

E = TypeVar("E", bound="ExtraBase")
E = TypeVar("E", bound="ExtraBase | BaseDonation")


class ExtraBase(ChzzkModel):
Expand Down Expand Up @@ -151,40 +152,16 @@ class NoticeMessage(Message[NoticeExtra]):
pass


class DonationRank(ChzzkModel):
user_id_hash: str
nickname: str = Field(validation_alias=AliasChoices("nickname", "nickName"))
verified_mark: bool
donation_amount: int
ranking: int


class BaseDonationExtra(ExtraBase):
is_anonymous: bool = True
pay_type: str
pay_amount: int = 0
donation_type: str
weekly_rank_list: Optional[list[DonationRank]] = Field(default_factory=list)
donation_user_weekly_rank: Optional[DonationRank] = None


class ChatDonationExtra(BaseDonationExtra):
donation_type: Literal["CHAT"]
class ChatDonationExtra(ChatDonation):
pass


class VideoDonationExtra(BaseDonationExtra):
donation_type: Literal["VIDEO"]
class VideoDonationExtra(VideoDonation):
pass


class MissionDonationExtra(BaseDonationExtra):
donation_type: Literal["VIDEO"]
duration_time: Optional[int] = None
mission_donation_id: Optional[str] = None
mission_created_time: Optional[str] = None
mission_end_time: Optional[str] = None
mission_text: Optional[str] = None
status: Optional[str] = None
success: Optional[bool] = None
class MissionDonationExtra(MissionDonation):
pass


class DonationMessage(
Expand All @@ -196,8 +173,8 @@ class DonationMessage(
class SubscriptionExtra(ExtraBase):
month: int
tier_name: str
nickname: str
tier_no: int
nickname: Optional[str] = None
tier_no: Optional[int] = None


class SubscriptionMessage(MessageDetail[SubscriptionExtra]):
Expand Down
29 changes: 28 additions & 1 deletion chzzkpy/chat/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@
from typing import Callable, Any, TYPE_CHECKING, Optional

from .blind import Blind
from .donation import MissionDonation
from .enums import ChatCmd, ChatType, get_enum
from .message import ChatMessage, DonationMessage, NoticeMessage, SystemMessage
from .message import (
ChatMessage,
DonationMessage,
NoticeMessage,
SubscriptionMessage,
SystemMessage,
)
from .recent_chat import RecentChat

if TYPE_CHECKING:
Expand Down Expand Up @@ -113,6 +120,9 @@ def _parse_all_type_of_chat(self, data: list[dict[str, Any]]):
message, client=self.client
)
self.dispatch("chat", validated_data)
elif message_type == ChatType.SUBSCRIPTION:
validated_data = SubscriptionMessage.model_validate(message)
self.dispatch("subscription", validated_data)

@parsable(ChatCmd.CHAT)
@catch_exception
Expand Down Expand Up @@ -145,3 +155,20 @@ def parse_notice(self, data: dict[str, Any]):
def parse_blind(self, data: dict[str, Any]):
validated_data = Blind.model_validate(data)
self.dispatch("blind", validated_data)

@parsable(ChatCmd.EVENT)
@catch_exception
def parse_event(self, data: dict[str, Any]): # For mission donation handler
event_type = data.get("type")

if event_type == "DONATION_MISSION_IN_PROGRESS":
validated_data = MissionDonation.model_validate(data)
if validated_data.status == "COMPLETED":
self.dispatch("mission_completed", validated_data)
elif validated_data.status == "PENDING":
self.dispatch("mission_pending", validated_data)
elif validated_data.status == "APPROVED":
self.dispatch("mission_approved", validated_data)
elif validated_data.status == "REJECTED":
self.dispatch("mission_rejected", validated_data)
return

0 comments on commit ab2dcb5

Please sign in to comment.