Skip to content

Commit

Permalink
Input: Add separate YouTube title and channel to Metadata (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgotf authored and FelixMcFelix committed Jun 14, 2021
1 parent dabcf90 commit edcd39a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/twilight/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async fn play(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
"Playing **{:?}** by **{:?}**",
input
.metadata
.title
.track
.as_ref()
.unwrap_or(&"<UNKNOWN>".to_string()),
input
Expand Down
4 changes: 2 additions & 2 deletions src/input/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub(crate) struct Origin {

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

Self {
title,
track,
artist,

channels,
Expand Down
36 changes: 24 additions & 12 deletions src/input/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::time::Duration;
/// [`Input`]: crate::input::Input
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Metadata {
/// The title of this stream.
pub title: Option<String>,
/// The track of this stream.
pub track: Option<String>,
/// The main artist of this stream.
pub artist: Option<String>,
/// The date of creation of this stream.
Expand All @@ -18,6 +18,8 @@ pub struct Metadata {
///
/// Any number `>= 2` is treated as stereo.
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.
///
/// This occurs as an artefact of coder delay.
Expand All @@ -28,6 +30,8 @@ pub struct Metadata {
pub sample_rate: Option<u32>,
/// The source url of this stream.
pub source_url: Option<String>,
/// The YouTube title of this stream.
pub title: Option<String>,
/// The thumbnail url of this stream.
pub thumbnail: Option<String>,
}
Expand All @@ -52,7 +56,7 @@ impl Metadata {

let tags = format.and_then(|m| m.get("tags"));

let title = tags
let track = tags
.and_then(|m| m.get("title"))
.and_then(Value::as_str)
.map(str::to_string);
Expand Down Expand Up @@ -88,7 +92,7 @@ impl Metadata {
.map(|v| v as u32);

Self {
title,
track,
artist,
date,

Expand All @@ -110,12 +114,6 @@ impl Metadata {
.and_then(Value::as_str)
.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
.and_then(|m| m.get("artist"))
.and_then(Value::as_str)
Expand All @@ -138,6 +136,11 @@ impl Metadata {
.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
.and_then(|m| m.get("duration"))
.and_then(Value::as_f64)
Expand All @@ -148,20 +151,27 @@ impl Metadata {
.and_then(Value::as_str)
.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
.and_then(|m| m.get("thumbnail"))
.and_then(Value::as_str)
.map(str::to_string);

Self {
title,
track,
artist,
date,

channels: Some(2),
channel,
duration,
sample_rate: Some(SAMPLE_RATE_RAW as u32),
source_url,
title,
thumbnail,

..Default::default()
Expand All @@ -171,15 +181,17 @@ impl Metadata {
/// Move all fields from a `Metadata` object into a new one.
pub fn take(&mut self) -> Self {
Self {
title: self.title.take(),
track: self.track.take(),
artist: self.artist.take(),
date: self.date.take(),

channels: self.channels.take(),
channel: self.channel.take(),
start_time: self.start_time.take(),
duration: self.duration.take(),
sample_rate: self.sample_rate.take(),
source_url: self.source_url.take(),
title: self.title.take(),
thumbnail: self.thumbnail.take(),
}
}
Expand Down

0 comments on commit edcd39a

Please sign in to comment.