Skip to content

Commit

Permalink
Add documentation, add new LivePausedEvent, LiveUnpausedEvent events
Browse files Browse the repository at this point in the history
  • Loading branch information
isaackogan committed Feb 23, 2024
1 parent 83d888d commit aeec4e7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 34 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ Both belong to the TikTokLive `Event` type and can be listened to. The following

### Custom Events

- `UnknownEvent` - Events not currently tracked by TikTokLive as they have not been reverse engineered
- `FollowEvent` - Triggered when a user in the livestream follows the streamer
- `ShareEvent` - Triggered when a user shares the livestream
- `LiveEndEvent` - Triggered when the livestream ends
- `ConnectEvent` - Triggered when the Webcast connection is initiated
- `DisconnectEvent` - Triggered when the Webcast connection closes (including the livestream ending)
- `LiveEndEvent` - Triggered when the livestream ends
- `LivePausedEvent` - Triggered when the livestream is paused
- `LiveUnpausedEvent` - Triggered when the livestream is unpaused
- `FollowEvent` - Triggered when a user in the livestream follows the streamer
- `ShareEvent` - Triggered when a user shares the livestream
- `UnknownEvent` - Events not currently tracked by TikTokLive as they have not been reverse-engineered


### Proto Events

Expand All @@ -168,27 +171,27 @@ If you know what an event does, [make a pull request](https://github.com/isaacko
- `GoalUpdateEvent` - Triggered when the subscriber goal is updated
- `ControlEvent` - Triggered when a stream action occurs (e.g. Livestream start, end)
- `LikeEvent` - Triggered when the stream receives a like
- `SubNotifyEvent` - Triggered when someone subscribes to the TikTok creator
- `RoomEvent`
- `SubscribeEvent` - Triggered when someone subscribes to the TikTok creator
- `PollEvent` - Triggered when the creator launches a new poll
- `CommentEvent` - Triggered when a comment is sent in the stream
- `RoomEvent` - Messages broadcasted to all users in the room (e.g. "Welcome to TikTok LIVE!")
- `EmoteChatEvent` - Triggered when a custom emote is sent in the chat
- `EnvelopeEvent` - Triggered every time someone sends a treasure chest
- `SocialEvent` - Triggered when a user shares the stream or follows the host
- `QuestionNewEvent` - Triggered every time someone asks a new question via the question feature.
- `LiveIntroEvent` - Triggered when a live intro message appears
- `LinkMicArmiesEvent` - Triggered when a TikTok battle user receives points
- `LinkMicBattleEvent` - Triggered when a TikTok battle is started
- `JoinEvent` - Triggered when a user joins the livestream
- `LinkMicFanTicketMethodEvent`
- `LinkMicMethodEvent`
- `BarrageEvent`
- `CaptionEvent`
- `CommentEvent`
- `EmoteChatEvent`
- `EnvelopeEvent`
- `ImDeleteEvent`
- `RoomUserSeqEvent`
- `SocialEvent`
- `RankUpdateEvent`
- `MemberEvent`
- `PollEvent`
- `QuestionNewEvent`
- `RankTextEvent`
- `HourlyRankEvent`
- `LinkMicArmiesEvent`
- `LinkMicBattleEvent`
- `LinkMicFanTicketMethodEvent`
- `LinkMicMethodEvent`
- `LiveIntroEvent`
- `UnauthorizedMemberEvent`
- `MessageDetectEvent`
- `OecLiveShoppingEvent`
Expand Down Expand Up @@ -235,7 +238,7 @@ async def on_gift(event: GiftEvent):

```

### `SubNotifyEvent`
### `SubscribeEvent`

This event will only fire when a session ID (account login) is passed to the HTTP client *before* connecting to TikTok LIVE.
You can set the session ID with [`client.web.set_session_id(...)`](https://github.com/isaackogan/TikTokLive/blob/master/examples/logged_in.py).
Expand Down
17 changes: 13 additions & 4 deletions TikTokLive/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from TikTokLive.client.ws.ws_client import WebcastWSClient
from TikTokLive.events import Event, EventHandler
from TikTokLive.events.custom_events import UnknownEvent, ConnectEvent, FollowEvent, ShareEvent, LiveEndEvent, \
DisconnectEvent
DisconnectEvent, LivePausedEvent, LiveUnpausedEvent
from TikTokLive.events.proto_events import EVENT_MAPPINGS, ProtoEvent, ControlEvent
from TikTokLive.proto import WebcastResponse, WebcastResponseMessage, ControlAction

Expand Down Expand Up @@ -250,9 +250,18 @@ def _parse_webcast_response(self, response: Optional[WebcastResponseMessage]) ->

event: Event = event_type().parse(response.payload)

# Handle stream ending
if isinstance(event, ControlEvent) and event.action == ControlAction.STREAM_ENDED:
return [LiveEndEvent().parse(response.payload), event]
# Handle stream control events
if isinstance(event, ControlEvent):
return_events: List[Event] = [event]

if event.action == ControlAction.STREAM_ENDED:
return_events.append(LiveEndEvent().parse(response.payload))
elif event.action == ControlAction.STREAM_PAUSED:
return_events.append(LivePausedEvent().parse(response.payload))
elif event.action == ControlAction.STREAM_PAUSED:
return_events.append(LiveUnpausedEvent().parse(response.payload))

return return_events

# Handle follow & share events
if self.has_listener(FollowEvent, ShareEvent):
Expand Down
19 changes: 16 additions & 3 deletions TikTokLive/events/custom_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from TikTokLive.proto import WebcastResponseMessage


@dataclass()
class UnknownEvent(WebcastResponseMessage, BaseEvent):
"""
Thrown when a Webcast message is received that is NOT tracked by TikTokLive yet.
Expand All @@ -27,22 +26,34 @@ class ConnectEvent(BaseEvent):
room_id: str


@dataclass()
class DisconnectEvent(BaseEvent):
"""
Thrown when disconnecting from a stream
"""


@dataclass()
class LiveEndEvent(ControlEvent):
"""
Thrown when the stream ends
"""


class LivePausedEvent(ControlEvent):
"""
Thrown when the stream is paused
"""


class LiveUnpausedEvent(ControlEvent):
"""
Thrown when a paused stream is unpaused
"""


class FollowEvent(SocialEvent):
"""
A SocialEvent, but we give it its own class for clarity's sake.
Expand Down Expand Up @@ -87,6 +98,8 @@ def users_joined(self) -> Optional[int]:
"FollowEvent",
"ShareEvent",
"LiveEndEvent",
"LivePausedEvent",
"LiveUnpausedEvent",
"CustomEvent",
"DisconnectEvent"
]
Loading

0 comments on commit aeec4e7

Please sign in to comment.