Skip to content

Commit

Permalink
feat: get media URL programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickvd committed Nov 22, 2024
1 parent 6655ee8 commit 2fc4420
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
- [main] Improved command-line argument descriptions and examples
- [main] Made command-line parsing dependency (`clap`) optional and binary-only
- [player] Optimized track skipping using `HashSet` for better performance
- [track] Get the media URL programmatically instead of hardcoding it

### Deprecated

Expand Down
12 changes: 7 additions & 5 deletions src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,13 @@ impl Gateway {
}

/// The user's account name.
pub fn user_name(&self) -> &str {
self.user_data
.as_ref()
.map(|data| data.user.name.as_str())
.unwrap_or_default()
pub fn user_name(&self) -> Option<&str> {
self.user_data.as_ref().map(|data| data.user.name.as_str())
}

// The URL to use for media requests.
pub fn media_url(&self) -> Option<&str> {
self.user_data.as_ref().map(|data| data.media_url.as_str())
}

/// Converts a list of tracks from the Deezer API to a [`Queue`].
Expand Down
18 changes: 17 additions & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ pub struct Player {
/// The audio output stream. Although not used directly, this field must be retained to keep
/// the sink alive.
_stream: rodio::OutputStream,

/// The URL to use for media requests.
media_url: String,
}

/// The default target volume to normalize to in dB LUFS.
pub const DEFAULT_GAIN_TARGET_DB: i8 = -15;

/// The default media URL to use for media requests.
const DEFAULT_MEDIA_URL: &str = "https://media.deezer.com";

impl Player {
/// Creates a new `Player` with the given `Config`.
///
Expand Down Expand Up @@ -119,6 +125,7 @@ impl Player {
audio_quality: AudioQuality::default(),
client,
license_token: String::new(),
media_url: DEFAULT_MEDIA_URL.to_string(),
bf_secret,
repeat_mode: RepeatMode::default(),
shuffle: false,
Expand Down Expand Up @@ -342,7 +349,12 @@ impl Player {
let download = tokio::time::timeout(Duration::from_secs(1), async {
// Start downloading the track.
let medium = track
.get_medium(&self.client, self.audio_quality, self.license_token.clone())
.get_medium(
&self.client,
&self.media_url,
self.audio_quality,
self.license_token.clone(),
)
.await?;

// Return `None` on success to indicate that the track is not yet appended
Expand Down Expand Up @@ -739,4 +751,8 @@ impl Player {
pub fn gain_target_db(&self) -> i8 {
self.gain_target_db
}

pub fn set_media_url(&mut self, url: &str) {
self.media_url = url.to_string();
}
}
2 changes: 1 addition & 1 deletion src/protocol/gateway/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct UserData {
#[serde(rename = "__DZR_GATEKEEPS__")]
pub gatekeeps: Gatekeeps,
#[serde(rename = "URL_MEDIA")]
media_url: String,
pub media_url: String,
#[serde(rename = "GAIN")]
pub gain: Gain,
}
Expand Down
9 changes: 8 additions & 1 deletion src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl Client {
if let Some(license_token) = self.gateway.license_token() {
self.player.set_license_token(license_token);
}

if let Some(media_url) = self.gateway.media_url() {
self.player.set_media_url(media_url);
}
}

/// TODO
Expand Down Expand Up @@ -437,7 +441,10 @@ impl Client {
command
.env("EVENT", "connected")
.env("USER_ID", shell_escape(&self.user_id().to_string()))
.env("USER_NAME", shell_escape(self.gateway.user_name()));
.env(
"USER_NAME",
shell_escape(self.gateway.user_name().unwrap_or_default()),
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ impl Track {
Self::BF_CBC_STRIPE_MP3_MISC,
];

const MEDIA_GET_URL: &'static str = "https://media.deezer.com/v1/get_url";
/// The endpoint for obtaining media sources.
const MEDIA_ENDPOINT: &'static str = "/v1/get_url";

/// Get a HTTP media source for the track.
///
Expand All @@ -215,6 +216,7 @@ impl Track {
pub async fn get_medium(
&self,
client: &http::Client,
media_url: &str,
quality: AudioQuality,
license_token: impl Into<String>,
) -> Result<Medium> {
Expand Down Expand Up @@ -246,7 +248,7 @@ impl Track {

// Do not use `client.unlimited` but instead apply rate limiting.
// This is to prevent hammering the Deezer API in case of deserialize errors.
let get_url = Self::MEDIA_GET_URL.parse::<reqwest::Url>()?;
let get_url = format!("{media_url}{}", Self::MEDIA_ENDPOINT).parse::<reqwest::Url>()?;
let body = serde_json::to_string(&request)?;
let request = client.post(get_url, body);
let response = client.execute(request).await?;
Expand Down

0 comments on commit 2fc4420

Please sign in to comment.