|
8 | 8 | use super::{ContentProvider, DiscoveryItem, ProviderError, TokenResponse}; |
9 | 9 | use async_trait::async_trait; |
10 | 10 | use reqwest::Client; |
11 | | -use serde::Deserialize; |
| 11 | +use serde::{Deserialize, Serialize}; |
12 | 12 | use tracing::debug; |
13 | 13 |
|
14 | 14 | const TRAKT_API_URL: &str = "https://api.trakt.tv"; |
@@ -148,6 +148,60 @@ fn movie_to_discovery(movie: &TraktMovie) -> Option<DiscoveryItem> { |
148 | 148 | }) |
149 | 149 | } |
150 | 150 |
|
| 151 | +// ========== Rating and History API Types ========== |
| 152 | + |
| 153 | +/// Request body for adding items to history (marking as watched) |
| 154 | +#[derive(Debug, Clone, Serialize)] |
| 155 | +struct HistoryRequest { |
| 156 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 157 | + movies: Option<Vec<HistoryMovie>>, |
| 158 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 159 | + episodes: Option<Vec<HistoryEpisode>>, |
| 160 | +} |
| 161 | + |
| 162 | +#[derive(Debug, Clone, Serialize)] |
| 163 | +struct HistoryMovie { |
| 164 | + title: String, |
| 165 | + year: u16, |
| 166 | + ids: HistoryIds, |
| 167 | +} |
| 168 | + |
| 169 | +// TODO: Add support for marking TV episodes as watched |
| 170 | +#[derive(Debug, Clone, Serialize)] |
| 171 | +struct HistoryEpisode { |
| 172 | + ids: HistoryIds, |
| 173 | +} |
| 174 | + |
| 175 | +#[derive(Debug, Clone, Serialize)] |
| 176 | +struct HistoryIds { |
| 177 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 178 | + tmdb: Option<u64>, |
| 179 | +} |
| 180 | + |
| 181 | +/// Request body for rating items |
| 182 | +#[derive(Debug, Clone, Serialize)] |
| 183 | +struct RatingRequest { |
| 184 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 185 | + movies: Option<Vec<RatingMovie>>, |
| 186 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 187 | + episodes: Option<Vec<RatingEpisode>>, |
| 188 | +} |
| 189 | + |
| 190 | +#[derive(Debug, Clone, Serialize)] |
| 191 | +struct RatingMovie { |
| 192 | + title: String, |
| 193 | + year: u16, |
| 194 | + ids: HistoryIds, |
| 195 | + rating: u32, // 1-10 |
| 196 | +} |
| 197 | + |
| 198 | +// TODO: Add support for rating TV episodes |
| 199 | +#[derive(Debug, Clone, Serialize)] |
| 200 | +struct RatingEpisode { |
| 201 | + ids: HistoryIds, |
| 202 | + rating: u32, // 1-10 |
| 203 | +} |
| 204 | + |
151 | 205 | // ========== TraktProvider ========== |
152 | 206 |
|
153 | 207 | /// Trakt.tv content provider |
@@ -207,6 +261,106 @@ impl TraktProvider { |
207 | 261 | req |
208 | 262 | } |
209 | 263 |
|
| 264 | + // ========== Public API methods ========== |
| 265 | + |
| 266 | + /// Mark a movie as watched in Trakt history |
| 267 | + pub async fn mark_movie_watched( |
| 268 | + &self, |
| 269 | + title: String, |
| 270 | + year: u16, |
| 271 | + tmdb_id: Option<u64>, |
| 272 | + ) -> Result<(), ProviderError> { |
| 273 | + if self.access_token.is_none() { |
| 274 | + return Err(ProviderError::NotAuthenticated); |
| 275 | + } |
| 276 | + |
| 277 | + let request = HistoryRequest { |
| 278 | + movies: Some(vec![HistoryMovie { |
| 279 | + title, |
| 280 | + year, |
| 281 | + ids: HistoryIds { tmdb: tmdb_id }, |
| 282 | + }]), |
| 283 | + episodes: None, |
| 284 | + }; |
| 285 | + |
| 286 | + debug!("marking movie as watched: {:?}", request); |
| 287 | + |
| 288 | + let response = self |
| 289 | + .request(reqwest::Method::POST, "/sync/history") |
| 290 | + .json(&request) |
| 291 | + .send() |
| 292 | + .await?; |
| 293 | + |
| 294 | + let status = response.status(); |
| 295 | + if !status.is_success() { |
| 296 | + let error_text = response.text().await.unwrap_or_default(); |
| 297 | + let error_msg = match status.as_u16() { |
| 298 | + 401 => "Authentication failed - please re-authenticate with Trakt".to_string(), |
| 299 | + 422 => format!("Validation error - invalid data: {}", error_text), |
| 300 | + 429 => "Rate limit exceeded - please try again later".to_string(), |
| 301 | + 500..=599 => "Trakt server error - please try again later".to_string(), |
| 302 | + _ => format!("Failed to mark as watched (status {}): {}", status, error_text), |
| 303 | + }; |
| 304 | + return Err(ProviderError::InvalidResponse(error_msg)); |
| 305 | + } |
| 306 | + |
| 307 | + debug!("Successfully marked movie as watched on Trakt"); |
| 308 | + Ok(()) |
| 309 | + } |
| 310 | + |
| 311 | + /// Rate a movie on Trakt (1-10) |
| 312 | + pub async fn rate_movie( |
| 313 | + &self, |
| 314 | + title: String, |
| 315 | + year: u16, |
| 316 | + tmdb_id: Option<u64>, |
| 317 | + rating: u32, |
| 318 | + ) -> Result<(), ProviderError> { |
| 319 | + if self.access_token.is_none() { |
| 320 | + return Err(ProviderError::NotAuthenticated); |
| 321 | + } |
| 322 | + |
| 323 | + if !(1..=10).contains(&rating) { |
| 324 | + return Err(ProviderError::InvalidResponse( |
| 325 | + "Rating must be between 1 and 10".to_string(), |
| 326 | + )); |
| 327 | + } |
| 328 | + |
| 329 | + let request = RatingRequest { |
| 330 | + movies: Some(vec![RatingMovie { |
| 331 | + title, |
| 332 | + year, |
| 333 | + ids: HistoryIds { tmdb: tmdb_id }, |
| 334 | + rating, |
| 335 | + }]), |
| 336 | + episodes: None, |
| 337 | + }; |
| 338 | + |
| 339 | + debug!("rating movie: {:?}", request); |
| 340 | + |
| 341 | + let response = self |
| 342 | + .request(reqwest::Method::POST, "/sync/ratings") |
| 343 | + .json(&request) |
| 344 | + .send() |
| 345 | + .await?; |
| 346 | + |
| 347 | + let status = response.status(); |
| 348 | + if !status.is_success() { |
| 349 | + let error_text = response.text().await.unwrap_or_default(); |
| 350 | + let error_msg = match status.as_u16() { |
| 351 | + 401 => "Authentication failed - please re-authenticate with Trakt".to_string(), |
| 352 | + 422 => format!("Validation error - invalid rating data: {}", error_text), |
| 353 | + 429 => "Rate limit exceeded - please try again later".to_string(), |
| 354 | + 500..=599 => "Trakt server error - please try again later".to_string(), |
| 355 | + _ => format!("Failed to rate (status {}): {}", status, error_text), |
| 356 | + }; |
| 357 | + return Err(ProviderError::InvalidResponse(error_msg)); |
| 358 | + } |
| 359 | + |
| 360 | + debug!("Successfully rated movie on Trakt"); |
| 361 | + Ok(()) |
| 362 | + } |
| 363 | + |
210 | 364 | // ========== Internal API methods ========== |
211 | 365 |
|
212 | 366 | async fn fetch_watchlist(&self) -> Result<Vec<WatchlistItem>, ProviderError> { |
|
0 commit comments