use crate::client::Client;
use crate::error::Result;
use crate::models::{Dashboard, StationResult, StationTracksResult, Status};
use crate::params::Params;
use crate::types::{Diversity, MoodEnergy, RotorFeedback, StationLanguage, StationType};
#[derive(Debug, Clone, Default)]
pub struct RotorFeedbackReport {
pub track_id: Option<i64>,
pub batch_id: Option<String>,
pub from: Option<String>,
pub total_played_seconds: Option<f64>,
pub timestamp: Option<f64>,
}
impl Client {
pub async fn rotor_account_status(&self) -> Result<Status> {
self.get("/rotor/account/status", Params::new()).await
}
pub async fn rotor_dashboard(&self) -> Result<Dashboard> {
self.get("/rotor/stations/dashboard", Params::new()).await
}
pub async fn rotor_stations(&self) -> Result<Vec<StationResult>> {
let params = Params::new().add("language", self.language());
self.get("/rotor/stations/list", params).await
}
pub async fn rotor_station_info(&self, station: &str) -> Result<Vec<StationResult>> {
self.get(&format!("/rotor/station/{station}/info"), Params::new())
.await
}
pub async fn rotor_station_tracks(
&self,
station: &str,
queue: Option<i64>,
) -> Result<StationTracksResult> {
let params = match queue {
Some(queue) => Params::new().add("queue", queue),
None => Params::new().add("settings2", true),
};
self.get(&format!("/rotor/station/{station}/tracks"), params)
.await
}
pub async fn rotor_feedback(
&self,
station: &str,
event: RotorFeedback,
report: &RotorFeedbackReport,
) -> Result<bool> {
let path = match &report.batch_id {
Some(batch_id) => format!("/rotor/station/{station}/feedback?batch-id={batch_id}"),
None => format!("/rotor/station/{station}/feedback"),
};
let timestamp = report.timestamp.unwrap_or_else(|| {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0.0, |d| d.as_secs_f64())
});
let form = Params::new()
.add("type", event)
.add("timestamp", timestamp)
.maybe("trackId", report.track_id)
.maybe("from", report.from.as_deref())
.maybe("totalPlayedSeconds", report.total_played_seconds);
let value = self.post_form_value(&path, form).await?;
Ok(Client::is_ok_response(&value))
}
pub async fn rotor_radio_started(&self, station: &str, from: &str) -> Result<bool> {
let report = RotorFeedbackReport {
from: Some(from.to_owned()),
..Default::default()
};
self.rotor_feedback(station, RotorFeedback::RadioStarted, &report)
.await
}
pub async fn rotor_track_started(&self, station: &str, track_id: i64) -> Result<bool> {
let report = RotorFeedbackReport {
track_id: Some(track_id),
..Default::default()
};
self.rotor_feedback(station, RotorFeedback::TrackStarted, &report)
.await
}
pub async fn rotor_track_finished(
&self,
station: &str,
track_id: i64,
total_played_seconds: f64,
) -> Result<bool> {
let report = RotorFeedbackReport {
track_id: Some(track_id),
total_played_seconds: Some(total_played_seconds),
..Default::default()
};
self.rotor_feedback(station, RotorFeedback::TrackFinished, &report)
.await
}
pub async fn rotor_skip(
&self,
station: &str,
track_id: i64,
total_played_seconds: f64,
) -> Result<bool> {
let report = RotorFeedbackReport {
track_id: Some(track_id),
total_played_seconds: Some(total_played_seconds),
..Default::default()
};
self.rotor_feedback(station, RotorFeedback::Skip, &report)
.await
}
pub async fn rotor_set_station_settings(
&self,
station: &str,
mood_energy: MoodEnergy,
diversity: Diversity,
language: StationLanguage,
station_type: StationType,
) -> Result<bool> {
let form = Params::new()
.add("moodEnergy", mood_energy)
.add("diversity", diversity)
.add("type", station_type)
.add("language", language);
let value = self
.post_form_value(&format!("/rotor/station/{station}/settings3"), form)
.await?;
Ok(Client::is_ok_response(&value))
}
}