main.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #![feature(result_expect)]
  2. #![allow(deprecated)]
  3. extern crate getopts;
  4. extern crate librespot;
  5. extern crate rpassword;
  6. use std::clone::Clone;
  7. use std::fs::File;
  8. use std::io::{stdout, Read, Write};
  9. use std::path::Path;
  10. use std::thread;
  11. use std::path::PathBuf;
  12. use getopts::Options;
  13. use rpassword::read_password;
  14. use librespot::session::{Config, Session};
  15. use librespot::util::version::version_string;
  16. use librespot::player::Player;
  17. use librespot::spirc::SpircManager;
  18. fn usage(program: &str, opts: &Options) -> String {
  19. let brief = format!("Usage: {} [options]", program);
  20. format!("{}", opts.usage(&brief))
  21. }
  22. fn main() {
  23. let args: Vec<String> = std::env::args().collect();
  24. let program = args[0].clone();
  25. let mut opts = Options::new();
  26. opts.reqopt("a", "appkey", "Path to a spotify appkey", "APPKEY");
  27. opts.reqopt("u", "username", "Username to sign in with", "USERNAME");
  28. opts.optopt("p", "password", "Password (optional)", "PASSWORD");
  29. opts.reqopt("c", "cache", "Path to a directory where files will be cached.", "CACHE");
  30. opts.reqopt("n", "name", "Device name", "NAME");
  31. let matches = match opts.parse(&args[1..]) {
  32. Ok(m) => { m },
  33. Err(f) => {
  34. print!("Error: {}\n{}", f.to_string(), usage(&*program, &opts));
  35. return;
  36. }
  37. };
  38. let mut appkey_file = File::open(
  39. Path::new(&*matches.opt_str("a").unwrap())
  40. ).expect("Could not open app key.");
  41. let username = matches.opt_str("u").unwrap();
  42. let cache_location = matches.opt_str("c").unwrap();
  43. let name = matches.opt_str("n").unwrap();
  44. let password = matches.opt_str("p").unwrap_or_else(|| {
  45. print!("Password: ");
  46. stdout().flush().unwrap();
  47. read_password().unwrap()
  48. });
  49. let mut appkey = Vec::new();
  50. appkey_file.read_to_end(&mut appkey).unwrap();
  51. let config = Config {
  52. application_key: appkey,
  53. user_agent: version_string(),
  54. device_id: name.clone(),
  55. cache_location: PathBuf::from(cache_location)
  56. };
  57. let session = Session::new(config);
  58. session.login(username.clone(), password);
  59. session.poll();
  60. let _session = session.clone();
  61. thread::spawn(move || {
  62. loop {
  63. _session.poll();
  64. }
  65. });
  66. let player = Player::new(&session);
  67. let mut spirc_manager = SpircManager::new(&session, player, username, name);
  68. spirc_manager.run();
  69. }