Compare commits
2 commits
722f964f77
...
86a348f210
Author | SHA1 | Date | |
---|---|---|---|
86a348f210 | |||
6066813cba |
5 changed files with 182 additions and 61 deletions
|
@ -3,7 +3,6 @@ repos:
|
||||||
rev: v4.3.0
|
rev: v4.3.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: check-added-large-files
|
|
||||||
- id: check-json
|
- id: check-json
|
||||||
|
|
||||||
- repo: https://github.com/cathiele/pre-commit-rust
|
- repo: https://github.com/cathiele/pre-commit-rust
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod pagination;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod playlist;
|
pub mod playlist;
|
||||||
pub mod video_details;
|
pub mod video_details;
|
||||||
|
|
137
src/client/pagination.rs
Normal file
137
src/client/pagination.rs
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use crate::model::{Comment, Paginator, PlaylistVideo, RecommendedVideo};
|
||||||
|
|
||||||
|
use super::RustyPipeQuery;
|
||||||
|
|
||||||
|
impl Paginator<PlaylistVideo> {
|
||||||
|
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>> {
|
||||||
|
Ok(match &self.ctoken {
|
||||||
|
Some(ctoken) => Some(query.playlist_continuation(ctoken).await?),
|
||||||
|
None => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool> {
|
||||||
|
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<()> {
|
||||||
|
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<()> {
|
||||||
|
while self.items.len() < n_items {
|
||||||
|
match self.extend(query.clone()).await {
|
||||||
|
Ok(false) => break,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Paginator<RecommendedVideo> {
|
||||||
|
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>> {
|
||||||
|
Ok(match &self.ctoken {
|
||||||
|
Some(ctoken) => Some(query.video_recommendations(ctoken).await?),
|
||||||
|
None => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool> {
|
||||||
|
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<()> {
|
||||||
|
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<()> {
|
||||||
|
while self.items.len() < n_items {
|
||||||
|
match self.extend(query.clone()).await {
|
||||||
|
Ok(false) => break,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Paginator<Comment> {
|
||||||
|
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>> {
|
||||||
|
Ok(match &self.ctoken {
|
||||||
|
Some(ctoken) => Some(query.video_comments(ctoken).await?),
|
||||||
|
None => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool> {
|
||||||
|
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<()> {
|
||||||
|
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<()> {
|
||||||
|
while self.items.len() < n_items {
|
||||||
|
match self.extend(query.clone()).await {
|
||||||
|
Ok(false) => break,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -237,50 +237,6 @@ fn map_playlist_items(
|
||||||
(videos, ctoken)
|
(videos, ctoken)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Paginator<PlaylistVideo> {
|
|
||||||
pub async fn next(&self, query: RustyPipeQuery) -> Result<Option<Self>> {
|
|
||||||
Ok(match &self.ctoken {
|
|
||||||
Some(ctoken) => Some(query.playlist_continuation(ctoken).await?),
|
|
||||||
None => None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn extend(&mut self, query: RustyPipeQuery) -> Result<bool> {
|
|
||||||
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<()> {
|
|
||||||
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<()> {
|
|
||||||
while self.items.len() < n_items {
|
|
||||||
match self.extend(query.clone()).await {
|
|
||||||
Ok(false) => break,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs::File, io::BufReader, path::Path};
|
use std::{fs::File, io::BufReader, path::Path};
|
||||||
|
|
|
@ -1162,31 +1162,59 @@ mod tests {
|
||||||
assert!(details.recommended.items.is_empty());
|
assert!(details.recommended.items.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[tokio::test]
|
||||||
#[test_log::test(tokio::test)]
|
|
||||||
async fn get_video_recommendations() {
|
async fn get_video_recommendations() {
|
||||||
let rp = RustyPipe::builder().strict().build();
|
let rp = RustyPipe::builder().strict().build();
|
||||||
let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap();
|
let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap();
|
||||||
let rec = rp
|
let next_recommendations = details.recommended.next(rp.query()).await.unwrap().unwrap();
|
||||||
.query()
|
dbg!(&next_recommendations);
|
||||||
.video_recommendations(&details.recommended.ctoken.unwrap())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
dbg!(&rec);
|
assert!(
|
||||||
|
next_recommendations.items.len() > 10,
|
||||||
|
"expected > 10 next recommendations, got {}",
|
||||||
|
next_recommendations.items.len()
|
||||||
|
);
|
||||||
|
assert!(!next_recommendations.is_exhausted());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_log::test(tokio::test)]
|
#[tokio::test]
|
||||||
async fn get_video_comments() {
|
async fn get_video_comments() {
|
||||||
let rp = RustyPipe::builder().strict().build();
|
let rp = RustyPipe::builder().strict().build();
|
||||||
let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap();
|
let details = rp.query().video_details("ZeerrnuLi5E").await.unwrap();
|
||||||
let rec = rp
|
|
||||||
.query()
|
|
||||||
.video_comments(&details.top_comments.ctoken.unwrap())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
dbg!(&rec);
|
let top_comments = details
|
||||||
|
.top_comments
|
||||||
|
.next(rp.query())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
top_comments.items.len() > 10,
|
||||||
|
"expected > 10 next comments, got {}",
|
||||||
|
top_comments.items.len()
|
||||||
|
);
|
||||||
|
assert!(!top_comments.is_exhausted());
|
||||||
|
|
||||||
|
let n_comments = top_comments.count.unwrap();
|
||||||
|
assert!(
|
||||||
|
n_comments > 700000,
|
||||||
|
"expected > 700k comments, got {}",
|
||||||
|
n_comments
|
||||||
|
);
|
||||||
|
// Comment count should be exact after fetching first page
|
||||||
|
assert!(n_comments % 1000 != 0);
|
||||||
|
|
||||||
|
let latest_comments = details
|
||||||
|
.latest_comments
|
||||||
|
.next(rp.query())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
latest_comments.items.len() > 10,
|
||||||
|
"expected > 10 next comments, got {}",
|
||||||
|
latest_comments.items.len()
|
||||||
|
);
|
||||||
|
assert!(!latest_comments.is_exhausted());
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue