Compare commits
No commits in common. "86a348f210297680fa0005286c395c2e082d8f94" and "722f964f774e7542b9382980e2c227af0e13f64b" have entirely different histories.
86a348f210
...
722f964f77
5 changed files with 59 additions and 180 deletions
|
@ -3,6 +3,7 @@ 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,4 +1,3 @@
|
||||||
pub mod pagination;
|
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod playlist;
|
pub mod playlist;
|
||||||
pub mod video_details;
|
pub mod video_details;
|
||||||
|
|
|
@ -1,137 +0,0 @@
|
||||||
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,6 +237,50 @@ 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,59 +1162,31 @@ 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 next_recommendations = details.recommended.next(rp.query()).await.unwrap().unwrap();
|
let rec = rp
|
||||||
dbg!(&next_recommendations);
|
.query()
|
||||||
|
.video_recommendations(&details.recommended.ctoken.unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert!(
|
dbg!(&rec);
|
||||||
next_recommendations.items.len() > 10,
|
|
||||||
"expected > 10 next recommendations, got {}",
|
|
||||||
next_recommendations.items.len()
|
|
||||||
);
|
|
||||||
assert!(!next_recommendations.is_exhausted());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[test_log::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
|
||||||
let top_comments = details
|
.query()
|
||||||
.top_comments
|
.video_comments(&details.top_comments.ctoken.unwrap())
|
||||||
.next(rp.query())
|
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
|
||||||
.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();
|
dbg!(&rec);
|
||||||
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