From 9ec6dabb484bf3405629f8f9c2065491a6d47041 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Thu, 14 Jan 2021 02:49:41 +0100 Subject: [PATCH] Add several example programs --- examples/delete_listen.rs | 18 ++++++ examples/stats_sitewide_artists.rs | 8 +++ examples/submit_listens.rs | 90 ++++++++++++++++++++++++++++++ examples/user_activity.rs | 13 +++++ examples/user_artist_map.rs | 10 ++++ examples/user_listens.rs | 22 ++++++++ examples/user_stats.rs | 27 +++++++++ examples/validate_token.rs | 10 ++++ src/bin/validate.rs | 7 --- 9 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 examples/delete_listen.rs create mode 100644 examples/stats_sitewide_artists.rs create mode 100644 examples/submit_listens.rs create mode 100644 examples/user_activity.rs create mode 100644 examples/user_artist_map.rs create mode 100644 examples/user_listens.rs create mode 100644 examples/user_stats.rs create mode 100644 examples/validate_token.rs delete mode 100644 src/bin/validate.rs diff --git a/examples/delete_listen.rs b/examples/delete_listen.rs new file mode 100644 index 0000000..db8feff --- /dev/null +++ b/examples/delete_listen.rs @@ -0,0 +1,18 @@ +use listenbrainz::Client; +use listenbrainz::models::request::DeleteListen; + +fn main() { + let mut args = std::env::args().skip(1); + let token = args.next().expect("No token provided"); + let listened_at = args.next().expect("No listened_at provided"); + let recording_msid = args.next().expect("No recording_msid provided"); + + let mut client = Client::new(); + + let delete = DeleteListen { + listened_at: listened_at.parse().unwrap(), + recording_msid: &recording_msid, + }; + let result = client.delete_listen(&token, delete); + println!("{:#?}", result); +} diff --git a/examples/stats_sitewide_artists.rs b/examples/stats_sitewide_artists.rs new file mode 100644 index 0000000..d58bbd3 --- /dev/null +++ b/examples/stats_sitewide_artists.rs @@ -0,0 +1,8 @@ +use listenbrainz::Client; + +fn main() { + let mut client = Client::new(); + + let result = client.stats_sitewide_artists(None, None, Some("year")); + println!("{:#?}", result); +} diff --git a/examples/submit_listens.rs b/examples/submit_listens.rs new file mode 100644 index 0000000..4c71e1a --- /dev/null +++ b/examples/submit_listens.rs @@ -0,0 +1,90 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +use listenbrainz::Client; +use listenbrainz::models::request::{SubmitListens, ListenType, Payload, TrackMetadata}; + +fn now() -> i64 { + SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64 +} + +fn main() { + let token = std::env::args().nth(1).expect("No token provided"); + + let mut client = Client::new(); + + // Submit single + + let listen = Payload { + listened_at: Some(now()), + track_metadata: TrackMetadata { + artist_name: "Rick Astley", + track_name: "Never Gonna Give You Up", + release_name: None, + additional_info: None, + }, + }; + let submission = SubmitListens { + listen_type: ListenType::Single, + payload: &[listen], + }; + + let result = client.submit_listens(&token, submission); + println!("{:#?}", result); + + // Submit playing now + + let now_playing = Payload { + listened_at: None, + track_metadata: TrackMetadata { + artist_name: "Rick Astley", + track_name: "Never Gonna Give You Up", + release_name: None, + additional_info: None, + }, + }; + let submission = SubmitListens { + listen_type: ListenType::PlayingNow, + payload: &[now_playing], + }; + + let result = client.submit_listens(&token, submission); + println!("{:#?}", result); + + // Submit imports + + let first = Payload { + listened_at: Some(now() - 60*5), + track_metadata: TrackMetadata { + artist_name: "Lil Tecca", + track_name: "All Star (with Lil Tjay)", + release_name: None, + additional_info: None, + }, + }; + let second = Payload { + listened_at: Some(now() - 60*15), + track_metadata: TrackMetadata { + artist_name: "Queens of the Stone Age", + track_name: "None One Knows", + release_name: Some("Songs For The Deaf"), + additional_info: None, + }, + }; + let third = Payload { + listened_at: Some(now() - 60*10), + track_metadata: TrackMetadata { + artist_name: "Alkaline Trio", + track_name: "Fall Victim", + release_name: Some("Crimson"), + additional_info: None, + } + }; + + let submission = SubmitListens { + listen_type: ListenType::Import, + payload: &[first, second, third], + }; + + let result = client.submit_listens(&token, submission); + println!("{:#?}", result); +} diff --git a/examples/user_activity.rs b/examples/user_activity.rs new file mode 100644 index 0000000..24e8995 --- /dev/null +++ b/examples/user_activity.rs @@ -0,0 +1,13 @@ +use listenbrainz::Client; + +fn main() { + let user_name = std::env::args().nth(1).expect("No username provided"); + + let mut client = Client::new(); + + let result = client.stats_user_listening_activity(&user_name, None); + println!("{:#?}", result); + + let result = client.stats_user_daily_activity(&user_name, None); + println!("{:#?}", result); +} diff --git a/examples/user_artist_map.rs b/examples/user_artist_map.rs new file mode 100644 index 0000000..a6ed0fb --- /dev/null +++ b/examples/user_artist_map.rs @@ -0,0 +1,10 @@ +use listenbrainz::Client; + +fn main() { + let user_name = std::env::args().nth(1).expect("No username provided"); + + let mut client = Client::new(); + + let result = client.stats_user_artist_map(&user_name, None, None); + println!("{:#?}", result); +} diff --git a/examples/user_listens.rs b/examples/user_listens.rs new file mode 100644 index 0000000..d33696c --- /dev/null +++ b/examples/user_listens.rs @@ -0,0 +1,22 @@ +use listenbrainz::Client; + +fn main() { + let user_name = std::env::args().nth(1).expect("No username provided"); + + let mut client = Client::new(); + + let result = client.user_listen_count(&user_name); + println!("Listen count: {:#?}", result); + + let result = client.user_playing_now(&user_name); + println!("Playing now: {:#?}", result); + + let result = client.user_listens( + &user_name, + None, + None, + Some(5), + None, + ); + println!("Recent listens: {:#?}", result); +} diff --git a/examples/user_stats.rs b/examples/user_stats.rs new file mode 100644 index 0000000..ec7b128 --- /dev/null +++ b/examples/user_stats.rs @@ -0,0 +1,27 @@ +use listenbrainz::Client; + +fn main() { + let mut args = std::env::args().skip(1); + let subject = args.next().expect("No subject provided"); + let user_name = args.next().expect("No username provided"); + + let mut client = Client::new(); + + match subject.as_str() { + "recordings" => { + let result = client.stats_user_recordings(&user_name, None, None, None); + println!("{:#?}", result); + } + "releases" => { + let result = client.stats_user_releases(&user_name, None, None, None); + println!("{:#?}", result); + } + "artists" => { + let result = client.stats_user_artists(&user_name, None, None, None); + println!("{:#?}", result); + } + _ => { + eprintln!(r"Subject must be one of recordings, releases, artists"); + } + } +} diff --git a/examples/validate_token.rs b/examples/validate_token.rs new file mode 100644 index 0000000..753de58 --- /dev/null +++ b/examples/validate_token.rs @@ -0,0 +1,10 @@ +use listenbrainz::Client; + +fn main() { + let token = std::env::args().nth(1).expect("No token provided"); + + let mut client = Client::new(); + + let result = client.validate_token(&token); + println!("{:#?}", result); +} diff --git a/src/bin/validate.rs b/src/bin/validate.rs deleted file mode 100644 index 98df66a..0000000 --- a/src/bin/validate.rs +++ /dev/null @@ -1,7 +0,0 @@ -use listenbrainz::Client; - -fn main() { - let token = std::env::args().nth(1).unwrap(); - let mut client = Client::new(); - println!("{:#?}", client.validate_token(&token)); -}