2021-01-19 23:16:53 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::raw::request::{ListenType, Payload, SubmitListens, TrackMetadata};
|
2021-01-20 00:13:13 +00:00
|
|
|
use crate::raw::Client;
|
2021-01-19 23:16:53 +00:00
|
|
|
|
|
|
|
/// Contains a ListenBrainz token and the associated username
|
|
|
|
/// for authentication purposes.
|
2021-10-19 22:56:42 +01:00
|
|
|
#[derive(Debug)]
|
2021-01-19 23:16:53 +00:00
|
|
|
struct Auth {
|
|
|
|
token: String,
|
|
|
|
user: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An ergonomic ListenBrainz client.
|
|
|
|
///
|
|
|
|
/// As opposed to [`Client`](crate::raw::Client), this aims to be a convenient and high-level
|
|
|
|
/// wrapper of the ListenBrainz API.
|
2021-10-19 22:56:42 +01:00
|
|
|
#[derive(Debug)]
|
2021-01-19 23:16:53 +00:00
|
|
|
pub struct ListenBrainz {
|
|
|
|
client: Client,
|
|
|
|
auth: Option<Auth>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ListenBrainz {
|
2021-01-19 23:57:03 +00:00
|
|
|
/// Construct a new ListenBrainz client that is not authenticated.
|
2021-01-19 23:16:53 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
client: Client::new(),
|
|
|
|
auth: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:19:27 +01:00
|
|
|
/// Construct a new ListenBrainz client with a custom API URL that is not authenticated.
|
|
|
|
pub fn new_with_url(url: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
client: Client::new_with_url(url),
|
|
|
|
auth: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 23:16:53 +00:00
|
|
|
/// Check if this client is authenticated.
|
|
|
|
pub fn is_authenticated(&self) -> bool {
|
|
|
|
self.auth.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the token if authenticated or [`None`] if not.
|
|
|
|
pub fn authenticated_token(&self) -> Option<&str> {
|
|
|
|
self.auth.as_ref().map(|auth| auth.token.as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the user if authenticated or [`None`] if not.
|
|
|
|
pub fn authenticated_user(&self) -> Option<&str> {
|
|
|
|
self.auth.as_ref().map(|auth| auth.user.as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Authenticate this client with the given token.
|
2021-01-20 00:11:39 +00:00
|
|
|
/// If the token is valid, authenticates the client.
|
2021-01-19 23:57:03 +00:00
|
|
|
/// In case the client was already authenticated, the old information
|
|
|
|
/// is discarded and the new token will be used.
|
2021-01-19 23:16:53 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2021-01-19 23:52:10 +00:00
|
|
|
/// If the token was invalid, returns [`Error::InvalidToken`].
|
2021-01-19 23:16:53 +00:00
|
|
|
/// If there was an error while validating the token, that error is returned.
|
|
|
|
/// See the Errors section of [`Client`] for more info on what errors might occur.
|
2021-01-19 23:52:10 +00:00
|
|
|
pub fn authenticate(&mut self, token: &str) -> Result<(), Error> {
|
2021-01-19 23:16:53 +00:00
|
|
|
let result = self.client.validate_token(token)?;
|
|
|
|
if result.valid && result.user_name.is_some() {
|
|
|
|
self.auth.replace(Auth {
|
|
|
|
token: token.to_string(),
|
|
|
|
user: result.user_name.unwrap(),
|
|
|
|
});
|
2021-01-19 23:52:10 +00:00
|
|
|
return Ok(());
|
2021-01-19 23:16:53 +00:00
|
|
|
}
|
2021-01-19 23:52:10 +00:00
|
|
|
Err(Error::InvalidToken)
|
2021-01-19 23:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper method to submit a listen (either "single" or "playing now").
|
|
|
|
fn submit_listen(
|
|
|
|
&self,
|
|
|
|
listen_type: ListenType,
|
|
|
|
timestamp: Option<i64>,
|
|
|
|
artist: &str,
|
|
|
|
track: &str,
|
|
|
|
release: &str,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
if !self.is_authenticated() {
|
|
|
|
return Err(Error::NotAuthenticated);
|
|
|
|
}
|
|
|
|
|
|
|
|
let payload = Payload {
|
|
|
|
listened_at: timestamp,
|
|
|
|
track_metadata: TrackMetadata {
|
|
|
|
artist_name: artist,
|
|
|
|
track_name: track,
|
|
|
|
release_name: Some(release),
|
|
|
|
additional_info: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let token = self.authenticated_token().unwrap();
|
|
|
|
self.client.submit_listens(
|
|
|
|
token,
|
|
|
|
SubmitListens {
|
|
|
|
listen_type,
|
|
|
|
payload: &[payload],
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Submit a listened track with the current time as the listen time.
|
|
|
|
/// This requires authentication.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If not authenticated, returns [`Error::NotAuthenticated`].
|
|
|
|
/// Otherwise, see the Errors section of [`Client`] for more info on
|
|
|
|
/// what errors might occur.
|
|
|
|
pub fn listen(&self, artist: &str, track: &str, release: &str) -> Result<(), Error> {
|
|
|
|
let now = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.submit_listen(ListenType::Single, Some(now), artist, track, release)
|
|
|
|
}
|
|
|
|
|
2021-01-30 01:31:57 +00:00
|
|
|
/// Submit a currently playing track. This requires authentication.
|
2021-01-19 23:26:27 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If not authenticated, returns [`Error::NotAuthenticated`].
|
|
|
|
/// Otherwise, see the Errors section of [`Client`] for more info on
|
|
|
|
/// what errors might occur.
|
2021-01-19 23:16:53 +00:00
|
|
|
pub fn playing_now(&self, artist: &str, track: &str, release: &str) -> Result<(), Error> {
|
|
|
|
self.submit_listen(ListenType::PlayingNow, None, artist, track, release)
|
|
|
|
}
|
|
|
|
}
|