1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use reqwest::header;
use serde::Deserialize;
use tracing::{debug, error, trace};

#[derive(Deserialize, Clone)]
pub struct Plex {
    /// URL to the Plex server
    pub url: String,
    /// API token for the Plex server
    pub token: String,
    /// Whether to refresh metadata of the file (default: false)
    #[serde(default)]
    pub refresh: bool,
    /// Whether to analyze the file (default: false)
    #[serde(default)]
    pub analyze: bool,
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Media {
    #[serde(rename = "Part")]
    pub part: Vec<Part>,
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Part {
    pub id: i64,
    pub key: String,
    pub duration: Option<i64>,
    pub file: String,
    pub size: i64,
    pub audio_profile: Option<String>,
    pub container: Option<String>,
    pub video_profile: Option<String>,
    pub has_thumbnail: Option<String>,
    pub has64bit_offsets: Option<bool>,
    pub optimized_for_streaming: Option<bool>,
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
    pub key: String,
    #[serde(rename = "Media")]
    pub media: Option<Vec<Media>>,
}

#[doc(hidden)]
#[derive(Deserialize, Clone, Debug)]
struct Location {
    path: String,
}

#[doc(hidden)]
#[derive(Deserialize, Clone, Debug)]
struct Library {
    key: String,
    #[serde(rename = "Location")]
    location: Vec<Location>,
}

#[doc(hidden)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
struct MediaContainer {
    directory: Option<Vec<Library>>,
    metadata: Option<Vec<Metadata>>,
}

#[doc(hidden)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
struct LibraryResponse {
    media_container: MediaContainer,
}

impl Plex {
    fn get_client(&self) -> anyhow::Result<reqwest::Client> {
        let mut headers = header::HeaderMap::new();

        headers.insert("X-Plex-Token", self.token.parse().unwrap());
        headers.insert("Accept", "application/json".parse().unwrap());

        reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .default_headers(headers)
            .build()
            .map_err(Into::into)
    }

    async fn libraries(&self) -> anyhow::Result<Vec<Library>> {
        let client = self.get_client()?;
        let url = url::Url::parse(&self.url)?
            .join("/library/sections")?
            .to_string();

        let res = client.get(&url).send().await?;
        let libraries: LibraryResponse = res.json().await?;

        Ok(libraries.media_container.directory.unwrap())
    }

    fn in_library(
        &self,
        libraries: &Vec<Library>,
        ev: &ScanEvent,
    ) -> anyhow::Result<Option<Library>> {
        for library in libraries {
            for location in &library.location {
                if ev.file_path.starts_with(&location.path) {
                    return Ok(Some(library.clone()));
                }
            }
        }

        Ok(None)
    }

    async fn get_item(&self, library: &Library, path: &str) -> anyhow::Result<Option<Metadata>> {
        let client = self.get_client()?;
        let url = url::Url::parse(&self.url)?
            .join(&format!("/library/sections/{}/all", library.key))?
            .to_string();

        let res = client.get(&url).send().await?;
        let lib: LibraryResponse = res.json().await?;

        for item in lib.media_container.metadata.unwrap_or_default() {
            if item
                .media
                .clone()
                .unwrap_or_default()
                .iter()
                .any(|media| media.part.iter().any(|part| part.file == path))
            {
                return Ok(Some(item));
            }
        }

        Ok(None)
    }

    // async fn refresh_library(&self, library: &str) -> anyhow::Result<()> {
    //     let client = self.get_client()?;
    //     let mut url =
    //         url::Url::parse(&self.url)?.join(&format!("/library/sections/{}/refresh", library))?;

    //     url.query_pairs_mut().append_pair("force", "1");

    //     let res = client.get(url.to_string()).send().await?;

    //     if res.status().is_success() {
    //         Ok(())
    //     } else {
    //         let body = res.text().await?;
    //         Err(anyhow::anyhow!("unable to send refresh: {}", body))
    //     }
    // }

    // async fn analyze_library(&self, library: &str) -> anyhow::Result<()> {
    //     let client = self.get_client()?;
    //     let url =
    //         url::Url::parse(&self.url)?.join(&format!("/library/sections/{}/analyze", library))?;

    //     let res = client.put(url.to_string()).send().await?;

    //     if res.status().is_success() {
    //         Ok(())
    //     } else {
    //         let body = res.text().await?;
    //         Err(anyhow::anyhow!("unable to send analyze: {}", body))
    //     }
    // }

    async fn refresh_item(&self, key: &str) -> anyhow::Result<()> {
        let client = self.get_client()?;
        let url = url::Url::parse(&self.url)?.join(&format!("{}/refresh", key))?;

        let res = client.put(url.to_string()).send().await?;

        if res.status().is_success() {
            Ok(())
        } else {
            let body = res.text().await?;
            Err(anyhow::anyhow!("unable to send analyze: {}", body))
        }
    }

    async fn analyze_item(&self, key: &str) -> anyhow::Result<()> {
        let client = self.get_client()?;
        let url = url::Url::parse(&self.url)?.join(&format!("{}/analyze", key))?;

        let res = client.put(url.to_string()).send().await?;

        if res.status().is_success() {
            Ok(())
        } else {
            let body = res.text().await?;
            Err(anyhow::anyhow!("unable to send analyze: {}", body))
        }
    }

    async fn scan(&self, ev: &ScanEvent, library: &Library) -> anyhow::Result<()> {
        let client = self.get_client()?;
        let mut url = url::Url::parse(&self.url)?
            .join(&format!("/library/sections/{}/refresh", library.key))?;

        let file_dir = std::path::Path::new(&ev.file_path)
            .parent()
            .ok_or_else(|| anyhow::anyhow!("unable to get parent directory"))?
            .to_str()
            .ok_or_else(|| anyhow::anyhow!("unable to convert path to string"))?;

        url.query_pairs_mut().append_pair("path", file_dir);

        let res = client.get(url.to_string()).send().await?;

        if res.status().is_success() {
            Ok(())
        } else {
            let body = res.text().await?;
            Err(anyhow::anyhow!("unable to send scan: {}", body))
        }
    }
}

impl TargetProcess for Plex {
    async fn process<'a>(&self, evs: &[&'a ScanEvent]) -> anyhow::Result<Vec<String>> {
        let libraries = self.libraries().await?;

        let mut succeeded = Vec::new();

        for ev in evs {
            if let Some(library) = self.in_library(&libraries, ev)? {
                match self.scan(ev, &library).await {
                    Ok(_) => {
                        debug!("scanned file '{}'", ev.file_path);

                        if self.analyze || self.refresh {
                            match self.get_item(&library, &ev.file_path).await {
                                Ok(Some(item)) => {
                                    trace!("found item for file '{}'", ev.file_path);

                                    let mut success = true;

                                    if self.analyze {
                                        match self.analyze_item(&item.key).await {
                                            Ok(_) => {
                                                debug!("analyzed metadata '{}'", item.key);
                                            }
                                            Err(e) => {
                                                error!(
                                                    "failed to analyze library '{}': {}",
                                                    library.key, e
                                                );

                                                success = false;
                                            }
                                        }
                                    }

                                    if self.refresh {
                                        match self.refresh_item(&item.key).await {
                                            Ok(_) => {
                                                debug!("refreshed metadata '{}'", item.key);
                                            }
                                            Err(e) => {
                                                error!(
                                                    "failed to refresh library '{}': {}",
                                                    library.key, e
                                                );

                                                success = false;
                                            }
                                        }
                                    }

                                    if success {
                                        succeeded.push(ev.id.clone());
                                    }
                                }
                                Ok(None) => {
                                    error!("unable to find item for file: {}", ev.file_path);
                                }
                                Err(e) => {
                                    error!(
                                        "failed to get item for file '{}': {:?}",
                                        ev.file_path, e
                                    );
                                }
                            };
                        } else {
                            succeeded.push(ev.id.clone());
                        }
                    }
                    Err(e) => {
                        error!("failed to scan file '{}': {}", ev.file_path, e);
                    }
                }
            } else {
                error!("unable to find library for file: {}", ev.file_path);
            }
        }

        Ok(succeeded)
    }
}