refactor: update trends response model
This commit is contained in:
parent
6a10909377
commit
db6b1ab8a7
9 changed files with 936 additions and 1044 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use crate::error::{Error, ExtractionError};
|
||||
use crate::model::{
|
||||
Comment, ContinuationEndpoint, Paginator, PlaylistVideo, RecommendedVideo, SearchVideo,
|
||||
YouTubeItem,
|
||||
Comment, ContinuationEndpoint, Paginator, PlaylistVideo, RecommendedVideo, YouTubeItem,
|
||||
};
|
||||
use crate::serializer::MapResult;
|
||||
use crate::util::TryRemove;
|
||||
|
|
@ -13,8 +12,10 @@ impl RustyPipeQuery {
|
|||
self,
|
||||
ctoken: &str,
|
||||
endpoint: ContinuationEndpoint,
|
||||
visitor_data: Option<&str>,
|
||||
) -> Result<Paginator<T>, Error> {
|
||||
let context = self.get_context(ClientType::Desktop, true).await;
|
||||
let mut context = self.get_context(ClientType::Desktop, true).await;
|
||||
context.client.visitor_data = visitor_data.map(str::to_owned);
|
||||
let request_body = QContinuation {
|
||||
context,
|
||||
continuation: ctoken,
|
||||
|
|
@ -140,64 +141,14 @@ paginator!(Comment, RustyPipeQuery::video_comments);
|
|||
paginator!(PlaylistVideo, RustyPipeQuery::playlist_continuation);
|
||||
paginator!(RecommendedVideo, RustyPipeQuery::video_recommendations);
|
||||
|
||||
impl Paginator<SearchVideo> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match (&self.ctoken, &self.visitor_data) {
|
||||
(Some(ctoken), Some(visitor_data)) => {
|
||||
Some(query.startpage_continuation(ctoken, visitor_data).await?)
|
||||
}
|
||||
_ => 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(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TryFrom<YouTubeItem>> Paginator<T> {
|
||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>, Error> {
|
||||
Ok(match &self.ctoken {
|
||||
Some(ctoken) => Some(query.continuation(ctoken, self.endpoint).await?),
|
||||
Some(ctoken) => Some(
|
||||
query
|
||||
.continuation(ctoken, self.endpoint, self.visitor_data.as_deref())
|
||||
.await?,
|
||||
),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
|
@ -261,6 +212,7 @@ mod tests {
|
|||
|
||||
#[rstest]
|
||||
#[case("search", "search/cont")]
|
||||
#[case("startpage", "trends/startpage_cont")]
|
||||
fn map_continuation_items(#[case] name: &str, #[case] path: &str) {
|
||||
let filename = format!("testfiles/{}.json", path);
|
||||
let json_path = Path::new(&filename);
|
||||
|
|
|
|||
Reference in a new issue