1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![warn(missing_docs)]
extern crate winapi;
extern crate kernel32;
extern crate reqwest;
extern crate time;
extern crate json;
#[cfg(windows)]
mod windows_process;
mod connector;
pub mod status;
use std::thread::{self, JoinHandle};
use std::time::Duration;
#[cfg(windows)]
use windows_process::WindowsProcess;
use connector::{SpotifyConnector, InternalSpotifyError};
use status::{SpotifyStatus, SpotifyStatusChange};
type Result<T> = std::result::Result<T, SpotifyError>;
#[derive(Debug)]
pub enum SpotifyError {
InternalError(InternalSpotifyError),
ClientNotRunning,
WebHelperNotRunning,
}
pub struct Spotify {
connector: SpotifyConnector,
}
fn get_status(connector: &SpotifyConnector) -> Result<SpotifyStatus> {
match connector.fetch_status_json() {
Ok(result) => Ok(SpotifyStatus::from(result)),
Err(error) => Err(SpotifyError::InternalError(error)),
}
}
impl Spotify {
#[cfg(windows)]
pub fn connect() -> Result<Spotify> {
if !Spotify::spotify_webhelper_alive() {
return Err(SpotifyError::WebHelperNotRunning);
}
Spotify::new_unchecked()
}
#[cfg(not(windows))]
pub fn connect() -> Result<Spotify> {
Spotify::new_unchecked()
}
fn new_unchecked() -> Result<Spotify> {
match SpotifyConnector::connect_new() {
Ok(result) => Ok(Spotify { connector: result }),
Err(error) => Err(SpotifyError::InternalError(error)),
}
}
pub fn poll<'a, F: 'static>(self, f: F) -> JoinHandle<()>
where F: Fn(&Spotify, SpotifyStatus, SpotifyStatusChange) -> bool,
F: std::marker::Send
{
thread::spawn(move || {
let sleep_time = Duration::from_millis(250);
let mut last: Option<SpotifyStatus> = None;
let mut curr: Option<SpotifyStatus>;
let mut first = true;
loop {
curr = get_status(&self.connector).ok();
{
let last = last.clone();
if first && curr.is_some() {
let curr = curr.clone().unwrap();
if !f(&self, curr.clone(), SpotifyStatusChange::new_true()) {
break;
}
first = false;
} else if !first && curr.is_some() && last.is_some() {
let curr = curr.clone().unwrap();
let last = last.unwrap();
if !f(&self, curr.clone(), SpotifyStatusChange::from((curr, last))) {
break;
}
}
}
if curr.is_some() {
last = curr.clone();
}
thread::sleep(sleep_time);
}
})
}
pub fn status(&self) -> Result<SpotifyStatus> {
get_status(&self.connector)
}
pub fn play(&self, track: String, queue: bool) -> bool {
self.connector.request_play(track, queue)
}
pub fn pause(&self) -> bool {
self.connector.request_pause(true)
}
pub fn resume(&self) -> bool {
self.connector.request_pause(false)
}
#[cfg(windows)]
fn spotify_webhelper_alive() -> bool {
let process = "SpotifyWebHelper.exe";
WindowsProcess::find_by_name(process).is_some()
}
}