Input: Add separate YouTube title and channel to Metadata (#75)

This commit is contained in:
Vilgot Fredenberg
2021-05-28 00:28:56 +02:00
committed by Kyle Simpson
parent e47861c009
commit 00c8bc915a
3 changed files with 27 additions and 15 deletions

View File

@@ -190,7 +190,7 @@ async fn play(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
"Playing **{:?}** by **{:?}**", "Playing **{:?}** by **{:?}**",
input input
.metadata .metadata
.title .track
.as_ref() .as_ref()
.unwrap_or(&"<UNKNOWN>".to_string()), .unwrap_or(&"<UNKNOWN>".to_string()),
input input

View File

@@ -118,7 +118,7 @@ pub(crate) struct Origin {
impl From<DcaMetadata> for Metadata { impl From<DcaMetadata> for Metadata {
fn from(mut d: DcaMetadata) -> Self { fn from(mut d: DcaMetadata) -> Self {
let (title, artist) = d let (track, artist) = d
.info .info
.take() .take()
.map(|mut m| (m.title.take(), m.artist.take())) .map(|mut m| (m.title.take(), m.artist.take()))
@@ -128,7 +128,7 @@ impl From<DcaMetadata> for Metadata {
let sample_rate = Some(d.opus.sample_rate); let sample_rate = Some(d.opus.sample_rate);
Self { Self {
title, track,
artist, artist,
channels, channels,

View File

@@ -7,8 +7,8 @@ use std::time::Duration;
/// [`Input`]: crate::input::Input /// [`Input`]: crate::input::Input
#[derive(Clone, Debug, Default, Eq, PartialEq)] #[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Metadata { pub struct Metadata {
/// The title of this stream. /// The track of this stream.
pub title: Option<String>, pub track: Option<String>,
/// The main artist of this stream. /// The main artist of this stream.
pub artist: Option<String>, pub artist: Option<String>,
/// The date of creation of this stream. /// The date of creation of this stream.
@@ -18,6 +18,8 @@ pub struct Metadata {
/// ///
/// Any number `>= 2` is treated as stereo. /// Any number `>= 2` is treated as stereo.
pub channels: Option<u8>, pub channels: Option<u8>,
/// The YouTube channel of this stream.
pub channel: Option<String>,
/// The time at which the first true sample is played back. /// The time at which the first true sample is played back.
/// ///
/// This occurs as an artefact of coder delay. /// This occurs as an artefact of coder delay.
@@ -28,6 +30,8 @@ pub struct Metadata {
pub sample_rate: Option<u32>, pub sample_rate: Option<u32>,
/// The source url of this stream. /// The source url of this stream.
pub source_url: Option<String>, pub source_url: Option<String>,
/// The YouTube title of this stream.
pub title: Option<String>,
/// The thumbnail url of this stream. /// The thumbnail url of this stream.
pub thumbnail: Option<String>, pub thumbnail: Option<String>,
} }
@@ -52,7 +56,7 @@ impl Metadata {
let tags = format.and_then(|m| m.get("tags")); let tags = format.and_then(|m| m.get("tags"));
let title = tags let track = tags
.and_then(|m| m.get("title")) .and_then(|m| m.get("title"))
.and_then(Value::as_str) .and_then(Value::as_str)
.map(str::to_string); .map(str::to_string);
@@ -88,7 +92,7 @@ impl Metadata {
.map(|v| v as u32); .map(|v| v as u32);
Self { Self {
title, track,
artist, artist,
date, date,
@@ -110,12 +114,6 @@ impl Metadata {
.and_then(Value::as_str) .and_then(Value::as_str)
.map(str::to_string); .map(str::to_string);
let title = track.or_else(|| {
obj.and_then(|m| m.get("title"))
.and_then(Value::as_str)
.map(str::to_string)
});
let true_artist = obj let true_artist = obj
.and_then(|m| m.get("artist")) .and_then(|m| m.get("artist"))
.and_then(Value::as_str) .and_then(Value::as_str)
@@ -138,6 +136,11 @@ impl Metadata {
.map(str::to_string) .map(str::to_string)
}); });
let channel = obj
.and_then(|m| m.get("channel"))
.and_then(Value::as_str)
.map(str::to_string);
let duration = obj let duration = obj
.and_then(|m| m.get("duration")) .and_then(|m| m.get("duration"))
.and_then(Value::as_f64) .and_then(Value::as_f64)
@@ -148,20 +151,27 @@ impl Metadata {
.and_then(Value::as_str) .and_then(Value::as_str)
.map(str::to_string); .map(str::to_string);
let title = obj
.and_then(|m| m.get("title"))
.and_then(Value::as_str)
.map(str::to_string);
let thumbnail = obj let thumbnail = obj
.and_then(|m| m.get("thumbnail")) .and_then(|m| m.get("thumbnail"))
.and_then(Value::as_str) .and_then(Value::as_str)
.map(str::to_string); .map(str::to_string);
Self { Self {
title, track,
artist, artist,
date, date,
channels: Some(2), channels: Some(2),
channel,
duration, duration,
sample_rate: Some(SAMPLE_RATE_RAW as u32), sample_rate: Some(SAMPLE_RATE_RAW as u32),
source_url, source_url,
title,
thumbnail, thumbnail,
..Default::default() ..Default::default()
@@ -171,15 +181,17 @@ impl Metadata {
/// Move all fields from a `Metadata` object into a new one. /// Move all fields from a `Metadata` object into a new one.
pub fn take(&mut self) -> Self { pub fn take(&mut self) -> Self {
Self { Self {
title: self.title.take(), track: self.track.take(),
artist: self.artist.take(), artist: self.artist.take(),
date: self.date.take(), date: self.date.take(),
channels: self.channels.take(), channels: self.channels.take(),
channel: self.channel.take(),
start_time: self.start_time.take(), start_time: self.start_time.take(),
duration: self.duration.take(), duration: self.duration.take(),
sample_rate: self.sample_rate.take(), sample_rate: self.sample_rate.take(),
source_url: self.source_url.take(), source_url: self.source_url.take(),
title: self.title.take(),
thumbnail: self.thumbnail.take(), thumbnail: self.thumbnail.take(),
} }
} }