-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
240 lines (223 loc) · 7.67 KB
/
main.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//////////
// DOCS // https://docs.rs/rspotify/latest/rspotify
//////////
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
/////////////
// Imports //
/////////////
// Extern imports
use dotenv;
use rspotify::{
model::{AdditionalType, Country, Market},
prelude::*,
};
use std::{env, process::exit, thread, time::Duration};
// Self made files
mod auth;
mod functions;
mod spotifyd;
use auth::*;
use functions::*;
use spotifyd::*;
/////////////
// Program //
/////////////
// Real entry point
async fn real_main() -> Result<String, String> {
// Check for internet connection
if !online() {
return Err(String::from("Failed to connect to the internet"));
}
// Get config variables
dotenv::from_filename(".env").ok();
// Check spotifyd
match init_spotifyd() {
Ok(_) => (),
Err(e) => match e.as_str() {
"Failed parsing spotifyd settings" => {
return Err(String::from("Failed parsing spotifyd settings"))
}
"Failed to make spotifyd config file" => {
return Err(String::from("Failed to make spotifyd config file"))
}
"Failed to start spotifyd" => return Err(String::from("Failed to start spotifyd")),
_ => return Err(String::from("Unexpected exit_code")),
},
};
// First authorization and checks if everything works
let client = match auth_client().await {
Ok(client) => client,
Err(e) => match e.as_str() {
"Authorization failed" => return Err(String::from("Authorization failed")),
"Failed parsing spotify client" => {
return Err(String::from("Failed parsing spotify client"))
}
"Failed parsing spotify api" => return Err(String::from("Failed parsing spotify api")),
_ => return Err(String::from("Unexpected exit_code")),
},
};
// Getting data of current user
let mut user_country = Country::Netherlands;
#[allow(unused_assignments)]
let mut user_market = Market::Country(user_country.clone());
let content_types = [AdditionalType::Track, AdditionalType::Episode];
match online() {
true => {
match client.me().await {
Ok(me) => {
user_country = me.country.unwrap();
user_market = Market::Country(user_country.clone());
}
// Check client prefix is correct in .env
Err(_) => return Err(String::from("Failed parsing spotify client")),
}
}
false => return Err(String::from("Failed to connect to the internet")),
}
// Get playlist to play
let playlist = match get_playlist(&client).await {
Ok(playlist) => playlist,
Err(e) => match e.as_str() {
"Failed parsing playing settings" => {
return Err(String::from("Failed parsing playing settings"))
}
"Failed to connect to the internet" => {
return Err(String::from("Failed to connect to the internet"))
}
_ => return Err(String::from("Unexpected exit_code")),
},
};
// Check playing settings
match parse_playing_settings() {
Ok(_) => (),
Err(_) => return Err(String::from("Failed parsing playing settings")),
}
let mut device_id = None;
for device in client.device().await.unwrap() {
if device.name == env::var("SPOTIFYD_DEVICE_NAME").unwrap() {
device_id = device.id;
break;
}
}
let mut played = false;
let mut can_i_play_counter = 0;
let skip_wait_time = env::var("WAIT_TILL_SKIP").unwrap().parse::<u64>().unwrap();
let check_wait_time = env::var("TIME_BETWEEN_CHECKS")
.unwrap()
.parse::<u64>()
.unwrap();
let mut tracks = get_tracks(&client, &playlist.id, &user_market)
.await
.unwrap();
loop {
played = false;
match is_playing(&client).await {
Ok(bool) => match bool {
true => can_i_play_counter += 1,
false => {
can_i_play_counter = 0;
break; // DEBUG
}
},
Err(_) => (),
}
thread::sleep(Duration::from_secs(check_wait_time));
while can_i_play_counter
>= env::var("CHECKS_BEFORE_PLAYING")
.unwrap()
.parse::<i32>()
.unwrap()
{
if !played {
match client
.transfer_playback(&device_id.as_ref().unwrap(), Some(false))
.await
{
Ok(_) => played = true,
Err(_) => continue,
}
}
if tracks.len() == 0 {
tracks = get_tracks(&client, &playlist.id, &user_market)
.await
.unwrap();
}
let current_track = tracks.pop();
let track_id = current_track.unwrap().track.unwrap().id();
client.add_item_to_queue(&track_id.unwrap(), Some(device_id.unwrap().as_str()));
// TODO play track somehow in spotifyd by putting it in the queue before any api interaction check is_playing()
}
}
// End of program
match stop_spotifyd() {
Ok(_) => (),
Err(e) => match e.as_str() {
"Failed getting pid of spotifyd" => {
return Err(String::from("Failed getting pid of spotifyd"))
}
"Failed stopping spotifyd" => return Err(String::from("Failed stopping spotifyd")),
_ => return Err(String::from("Unexpected exit_code")),
},
}
Ok(String::from("Program finished successfully"))
}
// Entry point
#[tokio::main]
async fn main() {
dotenv::from_filename(".env").ok();
// Run application and match on exit codes
exit(match real_main().await.unwrap().as_str() {
"Program finished successfully" => {
println!("Program finished successfully");
0
}
"Authorization failed" => {
println!("Authorization failed; please try again");
1
}
"Failed parsing spotify client" => {
println!("Failed parsing spotify client. Please check your .env file");
1
}
"Failed to connect to the internet" => {
println!("Failed to connect to the internet, please check your connection");
1
}
"Failed parsing playing settings" => {
println!("Failed parsing playing settings. Please check your .env file");
1
}
"Failed parsing spotify api" => {
println!("Failed parsing spotify api, Please check your .env file");
1
}
"Failed parsing spotifyd settings" => {
println!("Failed parsing spotifyd settings, Please check your .env file");
1
}
"Failed to make spotifyd config file" => {
println!("Failed to make spotifyd config file, please try again");
1
}
"Failed to start spotifyd" => {
println!(
"Failed to start spotifyd, make sure spotifyd is installed and added to your PATH"
);
1
}
"Failed getting pid of spotifyd" => {
println!("Failed getting pid of spotifyd, make sure pgrep is installed");
1
}
"Failed stopping spotifyd" => {
println!("Failed stopping spotifyd, make sure kill is installed");
1
}
_ => {
println!("Unexpected exit_code");
-1
}
});
}