From fdc828e87e672b84b801dd9f34d33a330d050043 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 25 Jun 2016 17:32:34 +0300 Subject: [PATCH] First release --- .gitignore | 1 + Cargo.lock | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 15 ++++ src/main.rs | 82 +++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bc6ac59 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,221 @@ +[root] +name = "anmusic" +version = "0.1.0" +dependencies = [ + "aimpremote 0.1.0 (git+https://bitbucket.org/annimon/rust-aimpremote)", + "hyper 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-ini 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aimpremote" +version = "0.1.0" +source = "git+https://bitbucket.org/annimon/rust-aimpremote#dcbfc568d4c6551603ed4028eca742968eb57d57" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cookie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hpack" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "httparse" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "hyper" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "idna" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "language-tags" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "matches" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "mime" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_cpus" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-ini" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rustc_version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "solicit" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "traitobject" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "typeable" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicase" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-bidi" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "url" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "user32-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..70394e7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "anmusic" +version = "0.1.0" +authors = ["aNNiMON "] + +[dependencies] +url = "1.0" +rust-ini = "*" + +[dependencies.hyper] +version = "0.9" +default-features = false + +[dependencies.aimpremote] +git = "https://bitbucket.org/annimon/rust-aimpremote" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..22e4e33 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,82 @@ +extern crate aimpremote; +extern crate ini; +extern crate hyper; +extern crate url; + +use std::io::Read; +use std::time::Duration; +use std::thread; + +use aimpremote::TrackInfo; +use ini::Ini; +use hyper::Client; +use hyper::header::{ContentType, Headers}; +use url::form_urlencoded; + +fn send_track_info(login: String, token: String, track: TrackInfo) -> String { + let url = "http://annimon.com/json/nowplay.php"; + let client = Client::new(); + let query = vec![ + ("act", "set".to_string()), + ("login", login), + ("token", token), + ("artist", track.artist), + ("title", track.title), + ("genre", track.genre) + ]; + let body = form_urlencoded::Serializer::new(String::new()) + .extend_pairs(query.into_iter()) + .finish(); + let mut headers = Headers::new(); + headers.set(ContentType::form_url_encoded()); + let mut response = client.post(url) + .headers(headers) + .body(&body) + .send() + .unwrap(); + let mut response_body = String::new(); + response.read_to_string(&mut response_body).unwrap(); + response_body +} + +fn read_config() -> Result<(String, String, u32), String> { + let config = match Ini::load_from_file("config.properties") { + Ok(config) => config, + Err(_) => { + return Err("Unable to open config.properties file".to_owned()); + } + }; + let section = config.general_section(); + + let login = section.get("login"); + let token = section.get("token"); + let time = section.get("update_time").map(|t| t.parse::()); + match (login, token, time) { + (Some(l), Some(t), Some(Ok(time))) => Ok((l.to_string(), t.to_string(), time)), + (Some(login), Some(token), _) => Ok((login.to_string(), token.to_string(), 2)), + (None, _, _) => Err("Unable to find login".to_owned()), + (_, None, _) => Err("Unable to find token".to_owned()) + } +} + +fn main() { + let (login, token, time) = match read_config() { + Ok((login, token, time)) => (login, token, time), + Err(e) => { + println!("Error: {}", e); + return + } + }; + let update_duration = Duration::from_secs(60 * time as u64); + loop { + if let Some(track) = aimpremote::aimp_track_info() { + println!("{} - {}", track.artist, track.title); + let result = send_track_info(login.clone(), token.clone(), track); + if result.starts_with("{\"error") { + println!("{}", result); + break; + }; + } + thread::sleep(update_duration); + } +}