fix: play stations through the rotor session API so feedback is accepted
The old /rotor/station/{id}/feedback endpoint answered every event,
radioStarted included, with "condition is not met". Stations now run
on /rotor/session: a session starts with seeds (My Wave settings travel
as settingMoodEnergy/Diversity/Language seeds), batches continue it with
recent track ids, and feedback posts JSON events with millisecond ISO
timestamps and trackId:albumId to /rotor/session/{id}/feedback.
- ym-api posts JSON itself (yamuse keeps its JSON POST private), with the
same client header; YmClient's Debug no longer prints anything
- Player keeps the session id, sends radioStarted with the first batch,
and starts a new session when settings change
This commit is contained in:
1 parent
03aa180840
commit
99f795b500
7 files changed
+257
-94
No files matched your search
+89
-16
@@ -146,9 +146,7 @@ impl WaveSettings {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_yamuse(
|
||||
self,
|
||||
) -> (yamuse::MoodEnergy, yamuse::Diversity, yamuse::StationLanguage) {
|
||||
pub(crate) fn to_yamuse(self) -> (yamuse::MoodEnergy, yamuse::Diversity, yamuse::StationLanguage) {
|
||||
(self.mood.to_yamuse(), self.character.to_yamuse(), self.language.to_yamuse())
|
||||
}
|
||||
}
|
||||
@@ -181,19 +179,77 @@ impl Station {
|
||||
}
|
||||
}
|
||||
|
||||
/// One batch of station tracks; the batch id goes back with feedback.
|
||||
/// One batch of station tracks. Feedback names the session and the batch.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct WaveBatch {
|
||||
pub session_id: Option<String>,
|
||||
pub tracks: Vec<Track>,
|
||||
pub batch_id: Option<String>,
|
||||
}
|
||||
|
||||
impl WaveBatch {
|
||||
pub(crate) fn from_yamuse(r: y::StationTracksResult) -> Self {
|
||||
Self {
|
||||
tracks: r.sequence.into_iter().filter_map(|s| s.track.and_then(Track::from_yamuse)).collect(),
|
||||
batch_id: r.batch_id,
|
||||
/// From a `/rotor/session/...` result: `{radioSessionId, batchId, sequence: [{track}]}`.
|
||||
pub(crate) fn from_session(v: &serde_json::Value) -> Self {
|
||||
let tracks = v
|
||||
.get("sequence")
|
||||
.and_then(|s| s.as_array())
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|item| item.get("track"))
|
||||
.filter_map(|t| serde_json::from_value::<y::Track>(t.clone()).ok())
|
||||
.filter_map(Track::from_yamuse)
|
||||
.collect();
|
||||
let text = |k: &str| v.get(k).and_then(|x| x.as_str()).map(str::to_owned);
|
||||
Self { session_id: text("radioSessionId"), tracks, batch_id: text("batchId") }
|
||||
}
|
||||
}
|
||||
|
||||
impl WaveSettings {
|
||||
/// Settings as session seeds (`settingMoodEnergy:calm`…); defaults add nothing.
|
||||
pub(crate) fn seeds(self) -> Vec<String> {
|
||||
let mut seeds = Vec::new();
|
||||
let mood = match self.mood {
|
||||
Mood::Any => None,
|
||||
Mood::Energetic => Some("active"),
|
||||
Mood::Cheerful => Some("fun"),
|
||||
Mood::Calm => Some("calm"),
|
||||
Mood::Sad => Some("sad"),
|
||||
};
|
||||
let diversity = match self.character {
|
||||
Character::Default => None,
|
||||
Character::Favourite => Some("favorite"),
|
||||
Character::Unfamiliar => Some("discover"),
|
||||
Character::Popular => Some("popular"),
|
||||
};
|
||||
let language = match self.language {
|
||||
Language::Any => None,
|
||||
Language::Russian => Some("russian"),
|
||||
Language::Foreign => Some("not-russian"),
|
||||
};
|
||||
seeds.extend(mood.map(|m| format!("settingMoodEnergy:{m}")));
|
||||
seeds.extend(diversity.map(|d| format!("settingDiversity:{d}")));
|
||||
seeds.extend(language.map(|l| format!("settingLanguage:{l}")));
|
||||
seeds
|
||||
}
|
||||
}
|
||||
|
||||
impl WaveEvent {
|
||||
/// The session feedback body's `event` object.
|
||||
pub(crate) fn to_json(&self, now: &str) -> serde_json::Value {
|
||||
let (kind, track, played) = match self {
|
||||
WaveEvent::Started => ("radioStarted", None, None),
|
||||
WaveEvent::TrackStarted { track_id } => ("trackStarted", Some(track_id), None),
|
||||
WaveEvent::TrackFinished { track_id, played } => ("trackFinished", Some(track_id), Some(played)),
|
||||
WaveEvent::Skipped { track_id, played } => ("skip", Some(track_id), Some(played)),
|
||||
};
|
||||
let mut event = serde_json::json!({ "type": kind, "timestamp": now });
|
||||
if let Some(track) = track {
|
||||
event["trackId"] = track.clone().into();
|
||||
}
|
||||
if let Some(played) = played {
|
||||
event["totalPlayedSeconds"] = played.as_secs_f64().into();
|
||||
}
|
||||
event
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,6 +318,31 @@ mod tests {
|
||||
assert_eq!(WaveSettings::from_api(&y::StationSettings::default()), WaveSettings::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_become_seeds() {
|
||||
assert!(WaveSettings::default().seeds().is_empty());
|
||||
let s = WaveSettings { mood: Mood::Calm, character: Character::Unfamiliar, language: Language::Russian };
|
||||
assert_eq!(s.seeds(), ["settingMoodEnergy:calm", "settingDiversity:discover", "settingLanguage:russian"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn feedback_events_serialise_like_the_official_clients() {
|
||||
let skip = WaveEvent::Skipped { track_id: "1:2".into(), played: Duration::from_millis(1500) };
|
||||
assert_eq!(
|
||||
skip.to_json("T"),
|
||||
serde_json::json!({"type": "skip", "timestamp": "T", "trackId": "1:2", "totalPlayedSeconds": 1.5})
|
||||
);
|
||||
assert_eq!(WaveEvent::Started.to_json("T"), serde_json::json!({"type": "radioStarted", "timestamp": "T"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_batch_parses() {
|
||||
let v = serde_json::json!({"radioSessionId": "s1", "batchId": "b1",
|
||||
"sequence": [{"type": "track", "track": {"id": "7", "title": "A"}}, {"type": "ad"}]});
|
||||
let b = WaveBatch::from_session(&v);
|
||||
assert_eq!((b.session_id.as_deref(), b.batch_id.as_deref(), b.tracks.len()), (Some("s1"), Some("b1"), 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn station_and_batch_convert() {
|
||||
let r: y::StationResult = serde_json::from_str(
|
||||
@@ -273,14 +354,6 @@ mod tests {
|
||||
assert_eq!(s.id, "genre:rock");
|
||||
assert_eq!(s.color.as_deref(), Some("#ff4444"));
|
||||
assert_eq!(s.icon_url(200).as_deref(), Some("https://avatars.yandex.net/x/200x200"));
|
||||
|
||||
let b: y::StationTracksResult = serde_json::from_str(
|
||||
r#"{"batchId":"b1","sequence":[{"type":"track","track":{"id":5,"title":"A"}},{"type":"ad"}]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let batch = WaveBatch::from_yamuse(b);
|
||||
assert_eq!(batch.tracks.len(), 1);
|
||||
assert_eq!(batch.batch_id.as_deref(), Some("b1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user