2022-05-17 11:15:22 +02:00
|
|
|
//! The Mixcloud back-end.
|
|
|
|
//!
|
|
|
|
//! It uses the Mixcloud API to retrieve the feed (user) and items (cloudcasts)).
|
|
|
|
//! See also: <https://www.mixcloud.com/developers/>
|
|
|
|
|
2022-05-26 21:20:10 +02:00
|
|
|
use cached::proc_macro::cached;
|
2022-05-17 11:15:22 +02:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use reqwest::Url;
|
|
|
|
use rocket::serde::Deserialize;
|
2022-05-26 20:37:27 +02:00
|
|
|
use youtube_dl::{YoutubeDl, YoutubeDlOutput};
|
2022-05-17 11:15:22 +02:00
|
|
|
|
2022-05-26 19:57:16 +02:00
|
|
|
use super::{Error, Result};
|
|
|
|
|
2022-05-17 11:15:22 +02:00
|
|
|
/// A Mixcloud user.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2022-05-17 11:15:22 +02:00
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub(crate) struct User {
|
|
|
|
/// The name of the user.
|
|
|
|
pub(crate) name: String,
|
|
|
|
|
|
|
|
/// The bio (description) of the user.
|
|
|
|
pub(crate) biog: String,
|
|
|
|
|
|
|
|
/// The picture URLs associated with the user.
|
|
|
|
pub(crate) pictures: Pictures,
|
|
|
|
|
|
|
|
/// The original URL of the user.
|
|
|
|
pub(crate) url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A collection of different sizes/variants of a picture.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2022-05-17 11:15:22 +02:00
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub(crate) struct Pictures {
|
|
|
|
/// The large picture of the user.
|
|
|
|
pub(crate) large: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Mixcloud cloudcasts container.
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub(crate) struct CloudcastData {
|
|
|
|
/// The contained cloudcasts.
|
|
|
|
data: Vec<Cloudcast>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A Mixcloud cloudcast.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2022-05-17 11:15:22 +02:00
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub(crate) struct Cloudcast {
|
2022-05-23 22:21:38 +02:00
|
|
|
/// The key of the cloudcast.
|
|
|
|
pub(crate) key: String,
|
|
|
|
|
2022-05-17 11:15:22 +02:00
|
|
|
/// The name of the cloudcast.
|
|
|
|
pub(crate) name: String,
|
|
|
|
|
|
|
|
/// The slug of the cloudcast (used for the enclosure).
|
|
|
|
pub(crate) slug: String,
|
|
|
|
|
|
|
|
/// The picture URLs associated with the cloudcast.
|
|
|
|
pub(crate) pictures: Pictures,
|
|
|
|
|
|
|
|
/// The tags of the cloudcast.
|
|
|
|
pub(crate) tags: Vec<Tag>,
|
|
|
|
|
|
|
|
/// The time the feed was created/started.
|
|
|
|
pub(crate) updated_time: DateTime<Utc>,
|
|
|
|
|
|
|
|
/// The original URL of the cloudcast.
|
|
|
|
pub(crate) url: String,
|
|
|
|
|
|
|
|
/// The length of the cloudcast (in seconds).
|
|
|
|
pub(crate) audio_length: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A Mixcloud cloudcast tag.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2022-05-17 11:15:22 +02:00
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub(crate) struct Tag {
|
|
|
|
/// The name of the tag.
|
|
|
|
pub(crate) name: String,
|
|
|
|
|
|
|
|
/// The URL of the tag.
|
|
|
|
pub(crate) url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The base URL for the Mixcloud API.
|
|
|
|
const API_BASE_URL: &str = "https://api.mixcloud.com";
|
|
|
|
|
2022-05-23 22:21:38 +02:00
|
|
|
/// The base URL for downloading Mixcloud files.
|
|
|
|
const FILES_BASE_URL: &str = "https://www.mixcloud.com";
|
|
|
|
|
2022-05-17 11:15:22 +02:00
|
|
|
/// The default bitrate used by
|
|
|
|
const DEFAULT_BITRATE: u32 = 64 * 1024;
|
|
|
|
|
|
|
|
/// The default file (MIME) type.
|
|
|
|
const DEFAULT_FILE_TYPE: &str = "audio/mpeg";
|
|
|
|
|
|
|
|
/// Returns the default file type used by Mixcloud.
|
|
|
|
pub(crate) fn default_file_type() -> &'static str {
|
|
|
|
DEFAULT_FILE_TYPE
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the estimated file size in bytes for a given duration.
|
|
|
|
///
|
|
|
|
/// This uses the default bitrate (see [`DEFAULT_BITRATE`]) which is in B/s.
|
|
|
|
pub(crate) fn estimated_file_size(duration: u32) -> u32 {
|
|
|
|
DEFAULT_BITRATE * duration / 8
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the user data using the Mixcloud API.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[cached(
|
|
|
|
key = "String",
|
|
|
|
convert = r#"{ username.to_owned() }"#,
|
|
|
|
time = 3600,
|
|
|
|
result = true
|
|
|
|
)]
|
2022-05-26 21:25:37 +02:00
|
|
|
pub(crate) async fn user(username: &str) -> Result<User> {
|
2022-05-26 19:57:16 +02:00
|
|
|
let mut url = Url::parse(API_BASE_URL).expect("URL can always be parsed");
|
2022-05-17 11:15:22 +02:00
|
|
|
url.set_path(username);
|
|
|
|
|
|
|
|
println!("⏬ Retrieving user {username} from {url}...");
|
2022-05-26 19:57:16 +02:00
|
|
|
let response = reqwest::get(url).await?.error_for_status()?;
|
|
|
|
let user = response.json().await?;
|
2022-05-17 11:15:22 +02:00
|
|
|
|
2022-05-26 19:57:16 +02:00
|
|
|
Ok(user)
|
2022-05-17 11:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the cloudcasts of the user using the Mixcloud API.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[cached(
|
|
|
|
key = "String",
|
|
|
|
convert = r#"{ username.to_owned() }"#,
|
|
|
|
time = 3600,
|
|
|
|
result = true
|
|
|
|
)]
|
2022-05-26 21:25:37 +02:00
|
|
|
pub(crate) async fn cloudcasts(username: &str) -> Result<Vec<Cloudcast>> {
|
2022-05-26 19:57:16 +02:00
|
|
|
let mut url = Url::parse(API_BASE_URL).expect("URL can always be parsed");
|
2022-05-17 11:15:22 +02:00
|
|
|
url.set_path(&format!("{username}/cloudcasts/"));
|
|
|
|
|
|
|
|
println!("⏬ Retrieving cloudcasts of user {username} from {url}...");
|
2022-05-26 19:57:16 +02:00
|
|
|
let response = reqwest::get(url).await?.error_for_status()?;
|
|
|
|
let cloudcasts: CloudcastData = response.json().await?;
|
2022-05-17 11:15:22 +02:00
|
|
|
|
2022-05-26 19:57:16 +02:00
|
|
|
Ok(cloudcasts.data)
|
2022-05-17 11:15:22 +02:00
|
|
|
}
|
2022-05-23 22:21:38 +02:00
|
|
|
|
|
|
|
/// Retrieves the redirect URL for the provided Mixcloud cloudcast key.
|
2022-05-26 21:20:10 +02:00
|
|
|
#[cached(
|
|
|
|
key = "String",
|
|
|
|
convert = r#"{ download_key.to_owned() }"#,
|
|
|
|
time = 3600,
|
|
|
|
result = true
|
|
|
|
)]
|
|
|
|
pub(crate) async fn redirect_url(download_key: &str) -> Result<String> {
|
2022-05-26 21:23:38 +02:00
|
|
|
let mut url = Url::parse(FILES_BASE_URL).expect("URL can always be parsed");
|
|
|
|
url.set_path(download_key);
|
2022-05-26 21:20:10 +02:00
|
|
|
|
|
|
|
println!("🌍 Determining direct URL for {download_key}...");
|
|
|
|
let output = YoutubeDl::new(url).run_async().await?;
|
2022-05-26 20:37:27 +02:00
|
|
|
|
|
|
|
if let YoutubeDlOutput::SingleVideo(yt_item) = output {
|
|
|
|
yt_item.url.ok_or(Error::NoRedirectUrlFound)
|
2022-05-23 22:21:38 +02:00
|
|
|
} else {
|
2022-05-26 20:37:27 +02:00
|
|
|
Err(Error::NoRedirectUrlFound)
|
2022-05-23 22:21:38 +02:00
|
|
|
}
|
|
|
|
}
|