Skip to content

Commit

Permalink
fix vod
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Feb 9, 2024
1 parent 8927b82 commit 38f93c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
59 changes: 22 additions & 37 deletions src/youtube/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ impl<'r> ActionChunk<'r> {
let continuation_token = match &response.continuation_contents.live_chat_continuation.continuations[0] {
Continuation::Invalidation { continuation, .. } => continuation.to_owned(),
Continuation::Timed { continuation, .. } => continuation.to_owned(),
Continuation::Replay { continuation, .. } => continuation.to_owned()
Continuation::Replay { continuation, .. } => continuation.to_owned(),
Continuation::PlayerSeek { .. } => return Err(Error::EndOfContinuation)
};
let signaler_topic = match &response.continuation_contents.live_chat_continuation.continuations[0] {
Continuation::Invalidation { invalidation_id, .. } => Some(invalidation_id.topic.to_owned()),
Expand All @@ -93,7 +94,7 @@ impl<'r> ActionChunk<'r> {
.ok_or(Error::EndOfContinuation)?
.into_iter()
.flat_map(|f| match f.action {
Action::ReplayChat { actions, .. } => actions,
Action::ReplayChat { actions, .. } => actions.into_iter().map(|f| f.action).collect(),
f => vec![f]
})
.collect()
Expand Down Expand Up @@ -159,8 +160,8 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
if let Action::AddChatItem { .. } = action.action {
yield_tx.send(Ok(action.action.to_owned())).await;
}
}
}
Expand All @@ -187,8 +188,8 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
if let Action::AddChatItem { .. } = action.action {
yield_tx.send(Ok(action.action.to_owned())).await;
}
}
}
Expand Down Expand Up @@ -232,8 +233,8 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
if let Action::AddChatItem { .. } = action.action {
yield_tx.send(Ok(action.action.to_owned())).await;
}
}
}
Expand All @@ -255,38 +256,17 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
}
Continuation::Replay { .. } => {
let chunk = ActionChunk::new(initial_chat, options).unwrap();
for action in chunk.iter() {
match action {
Action::AddChatItem { .. } => {
yield_tx.send(Ok(action.to_owned())).await;
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
}
}
}
action => {
yield_tx.send(Ok(action.to_owned())).await;
}
}
}

while let Some(Ok(chunk)) = chunk.cont().await {
let mut chunk = ActionChunk::new(initial_chat, options).unwrap();
loop {
for action in chunk.iter() {
match action {
Action::AddChatItem { item, .. } => {
if !seen_messages.contains(item.id()) {
yield_tx.send(Ok(action.to_owned())).await;
seen_messages.insert(item.id().to_owned());
}
Action::AddChatItem { .. } => {
yield_tx.send(Ok(action.to_owned())).await;
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
if let Action::AddChatItem { .. } = action.action {
yield_tx.send(Ok(action.action.to_owned())).await;
}
}
}
Expand All @@ -295,6 +275,10 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
}
}
match chunk.cont().await {
Some(Ok(e)) => chunk = e,
_ => break
}
}
}
Continuation::Timed { timeout_ms, .. } => {
Expand All @@ -311,8 +295,8 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
Action::ReplayChat { actions, .. } => {
for action in actions {
if let Action::AddChatItem { .. } = action {
yield_tx.send(Ok(action.to_owned())).await;
if let Action::AddChatItem { .. } = action.action {
yield_tx.send(Ok(action.action.to_owned())).await;
}
}
}
Expand All @@ -328,6 +312,7 @@ pub async fn stream(options: &ChatContext) -> Result<Pin<Box<impl Stream<Item =
}
}
}
Continuation::PlayerSeek { .. } => panic!("player seek should not be first continuation")
}
})))
}
25 changes: 22 additions & 3 deletions src/youtube/types/get_live_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct LiveChatContinuation {
pub actions: Option<Vec<ActionContainer>>
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ActionContainer {
#[serde(flatten)]
Expand All @@ -115,7 +115,10 @@ pub enum Continuation {
Timed { timeout_ms: usize, continuation: String },
#[serde(rename = "liveChatReplayContinuationData")]
#[serde(rename_all = "camelCase")]
Replay { time_until_last_message_msec: usize, continuation: String }
Replay { time_until_last_message_msec: usize, continuation: String },
#[serde(rename = "playerSeekContinuationData")]
#[serde(rename_all = "camelCase")]
PlayerSeek { continuation: String }
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -150,7 +153,7 @@ pub enum Action {
#[serde(rename = "replayChatItemAction")]
#[serde(rename_all = "camelCase")]
ReplayChat {
actions: Vec<Action>,
actions: Vec<ActionContainer>,
#[serde(deserialize_with = "deserialize_number_from_string")]
video_offset_time_msec: i64
},
Expand Down Expand Up @@ -243,6 +246,20 @@ pub enum ChatItem {
background_color: isize,
author_name_text_color: isize
},
#[serde(rename = "liveChatSponsorshipsGiftPurchaseAnnouncementRenderer")]
#[serde(rename_all = "camelCase")]
MembershipGift {
id: String,
#[serde(flatten)]
data: simd_json::OwnedValue
},
#[serde(rename = "liveChatSponsorshipsGiftRedemptionAnnouncementRenderer")]
#[serde(rename_all = "camelCase")]
MembershipGiftRedemption {
id: String,
#[serde(flatten)]
data: simd_json::OwnedValue
},
#[serde(rename = "liveChatViewerEngagementMessageRenderer")]
ViewerEngagement { id: String }
}
Expand All @@ -254,6 +271,8 @@ impl ChatItem {
ChatItem::PaidSticker { message_renderer_base, .. } => &message_renderer_base.id,
ChatItem::Superchat { message_renderer_base, .. } => &message_renderer_base.id,
ChatItem::TextMessage { message_renderer_base, .. } => &message_renderer_base.id,
ChatItem::MembershipGift { id, .. } => id,
ChatItem::MembershipGiftRedemption { id, .. } => id,
ChatItem::ViewerEngagement { id } => id
}
}
Expand Down

0 comments on commit 38f93c0

Please sign in to comment.