Skip to content

Commit

Permalink
fix: screw it max 250
Browse files Browse the repository at this point in the history
  • Loading branch information
lostb1t committed Aug 13, 2023
1 parent 838ffdb commit b1a4e05
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 133 deletions.
98 changes: 49 additions & 49 deletions src/plex_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,55 +161,55 @@ impl PlexClient {
let mut c = self
.get_collection_children(id, Some(offset), Some(limit))
.await?;

if !config.include_watched {
let original_size = c.media_container.size.unwrap();
//let children =
c.media_container.children_mut().retain(|x| !x.is_watched());
let children_lenght = c.media_container.children_mut().len() as i32;
let total_size = c.media_container.total_size.unwrap();

// dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// if original_size != children_lenght {
// dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// }
// take into account watched, reload if we are under the requested limit
if (children_lenght < original_limit
&& total_size > offset + limit
&& offset < total_size)
|| (children_lenght < original_limit
&& total_size > offset + original_size)
{
let mut children = c.media_container.children();
// dbg!("recursive");
// dbg!("checking", original_size, children_lenght, offset, total_size, limit);
let new_limit = match limit {
x if x < 25 => 25 + x,
x if x > 25 => x * 2,
_ => 25 + limit,
};
let mut r = self
.load_collection_children_recursive(
id,
offset + children_lenght,
new_limit,
// limit + 10,
original_limit,
)
.await.unwrap();
children.append(r.media_container.children_mut());
c.media_container.set_children(children);
// return self
// .load_collection_children_recursive(
// id,
// offset,
// new_limit,
// // limit + 10,
// original_limit,
// )
// .await;
}
}
c.media_container.children_mut().retain(|x| !x.is_watched());
// if !config.include_watched {
// let original_size = c.media_container.size.unwrap();
// //let children =
// c.media_container.children_mut().retain(|x| !x.is_watched());
// let children_lenght = c.media_container.children_mut().len() as i32;
// let total_size = c.media_container.total_size.unwrap();

// // dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// // if original_size != children_lenght {
// // dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// // }
// // take into account watched, reload if we are under the requested limit
// if (children_lenght < original_limit
// && total_size > offset + limit
// && offset < total_size)
// || (children_lenght < original_limit
// && total_size > offset + original_size)
// {
// let mut children = c.media_container.children();
// // dbg!("recursive");
// // dbg!("checking", original_size, children_lenght, offset, total_size, limit);
// let new_limit = match limit {
// x if x < 25 => 25 + x,
// x if x > 25 => x * 2,
// _ => 25 + limit,
// };
// let mut r = self
// .load_collection_children_recursive(
// id,
// offset + children_lenght,
// new_limit,
// // limit + 10,
// original_limit,
// )
// .await.unwrap();
// children.append(r.media_container.children_mut());
// c.media_container.set_children(children);
// // return self
// // .load_collection_children_recursive(
// // id,
// // offset,
// // new_limit,
// // // limit + 10,
// // original_limit,
// // )
// // .await;
// }
// }
// dbg!(c.media_container.children_mut().len());
// dbg!(limit);
c.media_container
Expand Down
12 changes: 10 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,11 @@ pub async fn get_collections_children(
let content_type = get_content_type_from_headers(req.headers_mut());

// We dont listen to pagination. We have a hard max of 250 per collection
let mut limit: i32 = params.container_size.unwrap_or(50);
let mut offset: i32 = params.container_start.unwrap_or(0);
let mut limit: i32 = 250;
let mut offset: i32 = 0;

// let mut limit: i32 = params.container_size.unwrap_or(50);
// let mut offset: i32 = params.container_start.unwrap_or(0);
// if params.container_start.is_some() {
// offset = params.container_start;
// }
Expand Down Expand Up @@ -380,6 +383,11 @@ pub async fn get_collections_children(
.with_transform(UserStateTransform)
.apply_to(&mut container)
.await;

// harcodded limit
if container.media_container.total_size.unwrap() > limit {
container.media_container.total_size = Some(250);
}

res.render(container); // TODO: FIx XML
Ok(())
Expand Down
119 changes: 37 additions & 82 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use futures_util::{
stream::{FuturesOrdered, FuturesUnordered},
StreamExt,
};
use std::collections::HashMap;
use itertools::Itertools;
use lazy_static::__Deref;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::task::JoinSet;
use tokio::time::Instant;
Expand Down Expand Up @@ -405,7 +405,8 @@ impl Transform for LibraryMixTransform {
let mut children: Vec<MetaData> = vec![];
let collection_ids_len = self.collection_ids.clone().len() as i32;
let mut total_size = 0;
let mut collections_total_sizes: HashMap<u32, i32> = HashMap::with_capacity(collection_ids_len as usize);
let mut collections_total_sizes: HashMap<u32, i32> =
HashMap::with_capacity(collection_ids_len as usize);
let collection_offset =
(self.offset as f32 / collection_ids_len as f32).ceil() as i32;
let collection_limit =
Expand All @@ -417,13 +418,13 @@ impl Transform for LibraryMixTransform {
// Which results in a loop
if !config.include_watched {
let mut collection = plex_client
.clone()
.get_cached(
plex_client.get_collection(id as i32),
format!("get_collection:{}", id),
)
.await
.unwrap();
.clone()
.get_cached(
plex_client.get_collection(id as i32),
format!("get_collection:{}", id),
)
.await
.unwrap();

let collection_metadata =
collection.media_container.children_mut()[0].clone();
Expand All @@ -449,7 +450,10 @@ impl Transform for LibraryMixTransform {
)
.await
.unwrap();
collections_total_sizes.insert(id, library_items.media_container.total_size.unwrap());
collections_total_sizes.insert(
id,
library_items.media_container.total_size.unwrap(),
);
total_size += library_items.media_container.total_size.unwrap();
} else {
let c = plex_client
Expand All @@ -460,97 +464,47 @@ impl Transform for LibraryMixTransform {
Some(0),
Some(0),
),
format!(
"get_collection_children:{}:{}:{}",
id, 0, 0
),
format!("get_collection_children:{}:{}:{}", id, 0, 0),
)
.await
.unwrap();
collections_total_sizes.insert(id, c.media_container.total_size.unwrap());
collections_total_sizes
.insert(id, c.media_container.total_size.unwrap());
total_size += c.media_container.total_size.unwrap();
}
}

for id in self.collection_ids.clone() {
// let percentage = {
// let mut b = 0;
// for s in collections_total_sizes.clone().into_iter() {
// b += s.1;
// }
// let size = collections_total_sizes.get(&id);
// // b / size
// // dbg!(&b);
// // dbg!(&size);
// let x = ((size.unwrap() * 100 / b * 100) / 100) as f32;
// // let x = (100 -((size.unwrap() * 100 / b * 100) / 100) as f32;
// x / 100.00
// };
// let mut c = plex_client
// .get_collection_children(
// id,
// Some(self.offset.clone()),
// Some(self.limit.clone()),
// )
// .await
// .unwrap();

let mut c = plex_client
.clone()
.get_cached(
plex_client.load_collection_children_recursive(
plex_client.get_collection_children(
id,
collection_offset,
collection_limit,
collection_limit,
// (self.offset as f32 * percentage).ceil() as i32,
// (self.limit as f32 * percentage).ceil() as i32,
// (self.limit as f32 * percentage).ceil() as i32,
Some(self.offset.clone()),
Some(self.limit.clone()),
),
format!(
"load_collection_children_recursive:{}:{}:{}",
"get_collection_children:{}:{}:{}",
id, self.offset, self.limit
),
)
.await
.unwrap();
// dbg!(c.media_container.children())
// dbg!(c)
// if !config.include_watched {
// // We need this for the total count. In theory it shouldnt have mattered.
// // But IOS doesnt listen to changes in total size. Picks it from the first request.
// // Which results in a loop
// let mut collection = plex_client
// .clone()
// .get_cached(
// plex_client.get_collection(id as i32),
// format!("collection:{}", id),
// )
// .await
// .unwrap();

// let collection_metadata =
// collection.media_container.children_mut()[0].clone();
// let library_items = plex_client
// .clone()
// .get_cached(
// plex_client.get_collection_total_size(
// collection
// .media_container
// .library_section_id
// .unwrap() as i32,
// collection_metadata.index.unwrap() as i32,
// collection_metadata.subtype.unwrap(),
// ),
// format!(
// "library_items:{}:{}",
// collection
// .media_container
// .library_section_id
// .unwrap() as i32,
// id
// ),
// )
// .await
// .unwrap();
// total_size += library_items.media_container.total_size.unwrap();
// } else {
// total_size += c.media_container.total_size.unwrap();
// }
// let local_c = c.media_container.children().truncate();
// children.truncate(self.limit as usize);

if !config.include_watched {
c.media_container.children_mut().retain(|x| !x.is_watched());
}

// total_size += c.media_container.total_size.unwrap();
match children.is_empty() {
false => {
children = children
Expand All @@ -562,11 +516,12 @@ impl Transform for LibraryMixTransform {
}
}
// children.truncate(self.limit as usize);

// dbg!("DONE");
// dbg!("------");
item.total_size = Some(total_size);
// always metadata library
// item.size = Some(children.len() as i32);
item.metadata = children;
}
}
Expand Down

0 comments on commit b1a4e05

Please sign in to comment.