fix: add visitor_data to recommendation paginator
This commit is contained in:
parent
39fca50731
commit
3a0db09e23
18 changed files with 155 additions and 124 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use crate::error::{Error, ExtractionError};
|
||||
use crate::model::{Comment, ContinuationEndpoint, Paginator, PlaylistVideo, YouTubeItem};
|
||||
use crate::model::{Comment, Paginator, PlaylistVideo, YouTubeItem};
|
||||
use crate::param::ContinuationEndpoint;
|
||||
use crate::serializer::MapResult;
|
||||
use crate::util::TryRemove;
|
||||
|
||||
|
|
@ -80,65 +81,6 @@ impl<T: TryFrom<YouTubeItem>> MapResponse<Paginator<T>> for response::Continuati
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! paginator {
|
||||
($entity_type:ty, $cont_function:path) => {
|
||||
impl Paginator<$entity_type> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match &self.ctoken {
|
||||
Some(ctoken) => Some($cont_function(query, ctoken).await?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool, Error> {
|
||||
match self.next(query).await {
|
||||
Ok(Some(paginator)) => {
|
||||
let mut items = paginator.items;
|
||||
self.items.append(&mut items);
|
||||
self.ctoken = paginator.ctoken;
|
||||
Ok(true)
|
||||
}
|
||||
Ok(None) => Ok(false),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn extend_pages(
|
||||
&mut self,
|
||||
query: RustyPipeQuery,
|
||||
n_pages: usize,
|
||||
) -> Result<(), Error> {
|
||||
for _ in 0..n_pages {
|
||||
match self.extend(query.clone()).await {
|
||||
Ok(false) => break,
|
||||
Err(e) => return Err(e),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn extend_limit(
|
||||
&mut self,
|
||||
query: RustyPipeQuery,
|
||||
n_items: usize,
|
||||
) -> Result<(), Error> {
|
||||
while self.items.len() < n_items {
|
||||
match self.extend(query.clone()).await {
|
||||
Ok(false) => break,
|
||||
Err(e) => return Err(e),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
paginator!(Comment, RustyPipeQuery::video_comments);
|
||||
paginator!(PlaylistVideo, RustyPipeQuery::playlist_continuation);
|
||||
|
||||
impl<T: TryFrom<YouTubeItem>> Paginator<T> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match &self.ctoken {
|
||||
|
|
@ -195,6 +137,80 @@ impl<T: TryFrom<YouTubeItem>> Paginator<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Paginator<Comment> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match &self.ctoken {
|
||||
Some(ctoken) => Some(
|
||||
query
|
||||
.video_comments(ctoken, self.visitor_data.as_deref())
|
||||
.await?,
|
||||
),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Paginator<PlaylistVideo> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match &self.ctoken {
|
||||
Some(ctoken) => Some(query.playlist_continuation(ctoken).await?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! paginator {
|
||||
($entity_type:ty) => {
|
||||
impl Paginator<$entity_type> {
|
||||
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool, Error> {
|
||||
match self.next(query).await {
|
||||
Ok(Some(paginator)) => {
|
||||
let mut items = paginator.items;
|
||||
self.items.append(&mut items);
|
||||
self.ctoken = paginator.ctoken;
|
||||
Ok(true)
|
||||
}
|
||||
Ok(None) => Ok(false),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn extend_pages(
|
||||
&mut self,
|
||||
query: RustyPipeQuery,
|
||||
n_pages: usize,
|
||||
) -> Result<(), Error> {
|
||||
for _ in 0..n_pages {
|
||||
match self.extend(query.clone()).await {
|
||||
Ok(false) => break,
|
||||
Err(e) => return Err(e),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn extend_limit(
|
||||
&mut self,
|
||||
query: RustyPipeQuery,
|
||||
n_items: usize,
|
||||
) -> Result<(), Error> {
|
||||
while self.items.len() < n_items {
|
||||
match self.extend(query.clone()).await {
|
||||
Ok(false) => break,
|
||||
Err(e) => return Err(e),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
paginator!(Comment);
|
||||
paginator!(PlaylistVideo);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{fs::File, io::BufReader, path::Path};
|
||||
|
|
|
|||
Reference in a new issue