-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathemby.rs
101 lines (97 loc) · 3.84 KB
/
emby.rs
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
use anyhow::{bail, Result};
use dependent_models::{ImportCompletedItem, ImportResult};
use enum_models::{MediaLot, MediaSource};
use media_models::{ImportOrExportMetadataItem, ImportOrExportMetadataItemSeen};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use sea_orm::DatabaseConnection;
use serde::{Deserialize, Serialize};
use crate::utils::get_show_by_episode_identifier;
mod models {
use super::*;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct EmbyWebhookPlaybackInfoPayload {
pub position_ticks: Option<Decimal>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct EmbyWebhookItemProviderIdsPayload {
pub tmdb: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct EmbyWebhookItemPayload {
pub run_time_ticks: Option<Decimal>,
#[serde(rename = "Type")]
pub item_type: String,
pub provider_ids: EmbyWebhookItemProviderIdsPayload,
#[serde(rename = "ParentIndexNumber")]
pub season_number: Option<i32>,
#[serde(rename = "IndexNumber")]
pub episode_number: Option<i32>,
#[serde(rename = "Name")]
pub episode_name: Option<String>,
pub series_name: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct EmbyWebhookPayload {
pub event: Option<String>,
pub item: EmbyWebhookItemPayload,
pub series: Option<EmbyWebhookItemPayload>,
pub playback_info: EmbyWebhookPlaybackInfoPayload,
}
}
pub async fn sink_progress(payload: String, db: &DatabaseConnection) -> Result<ImportResult> {
let payload: models::EmbyWebhookPayload = serde_json::from_str(&payload)?;
let runtime = payload
.item
.run_time_ticks
.ok_or_else(|| anyhow::anyhow!("No run time associated with this media"))?;
let position = payload
.playback_info
.position_ticks
.ok_or_else(|| anyhow::anyhow!("No position associated with this media"))?;
let (identifier, lot) =
match payload.item.item_type.as_str() {
"Movie" => {
let id = payload
.item
.provider_ids
.tmdb
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No TMDb ID associated with this media"))?;
(id.clone(), MediaLot::Movie)
}
"Episode" => {
let series_name =
payload.item.series_name.as_ref().ok_or_else(|| {
anyhow::anyhow!("No series name associated with this media")
})?;
let episode_name =
payload.item.episode_name.as_ref().ok_or_else(|| {
anyhow::anyhow!("No episode name associated with this media")
})?;
let db_show = get_show_by_episode_identifier(db, series_name, episode_name).await?;
(db_show.identifier, MediaLot::Show)
}
_ => bail!("Only movies and shows supported"),
};
Ok(ImportResult {
completed: vec![ImportCompletedItem::Metadata(ImportOrExportMetadataItem {
lot,
identifier,
source: MediaSource::Tmdb,
seen_history: vec![ImportOrExportMetadataItemSeen {
provider_watched_on: Some("Emby".to_string()),
progress: Some(position / runtime * dec!(100)),
show_season_number: payload.item.season_number,
show_episode_number: payload.item.episode_number,
..Default::default()
}],
..Default::default()
})],
..Default::default()
})
}