plex_api/myplex/
discover.rs1use crate::{
2 library::{metadata_items, Item},
3 media_container::server::{MediaProvider, MediaProviderWrapper},
4 url::MYPLEX_DISCOVER_API_BASE_URL,
5 Error, HttpClient, HttpClientBuilder, Result,
6};
7
8#[derive(Debug, Clone)]
9pub struct Discover {
10 pub provider: MediaProvider,
11 client: HttpClient,
12}
13
14impl Discover {
15 pub async fn new<C: Into<HttpClient>>(client: C) -> Result<Self> {
16 let client = HttpClientBuilder::from(client.into())
17 .set_api_url(MYPLEX_DISCOVER_API_BASE_URL)
18 .build()?;
19
20 let w: MediaProviderWrapper = client.get("/").json().await?;
21 Ok(Self {
22 provider: w.media_provider,
23 client,
24 })
25 }
26
27 #[tracing::instrument(level = "debug", skip(self))]
29 pub async fn item_by_id(&self, rating_key: &str) -> Result<Item> {
30 let path = format!("/library/metadata/{rating_key}?includeConcerts=1&includeExtras=1&includePreferences=1&includeReviews=1&includeOnDeck=1&includeChapters=1&includeStations=1&includeExternalMedia=1&asyncAugmentMetadata=1&asyncCheckFiles=1&asyncRefreshAnalysis=1&asyncRefreshLocalMediaAgent=1");
31
32 match metadata_items(&self.client, &path).await {
33 Ok(items) => items.into_iter().next().ok_or(Error::ItemNotFound),
34 Err(Error::UnexpectedApiResponse {
35 status_code,
36 content,
37 }) => {
38 if status_code == 404 {
40 Err(Error::ItemNotFound)
41 } else {
42 Err(Error::UnexpectedApiResponse {
43 status_code,
44 content,
45 })
46 }
47 }
48 Err(err) => Err(err),
49 }
50 }
51}