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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#![doc(
html_logo_url = "https://raw.githubusercontent.com/gstavrinos/send-to-kindle/master/media/send-to-kindle256.png",
html_favicon_url = "https://raw.githubusercontent.com/gstavrinos/send-to-kindle/master/media/send-to-kindle128.png"
)]
use thirtyfour::prelude::{WebDriverResult, By};
use thirtyfour::{FirefoxCapabilities, WebDriver};
use thirtyfour::common::capabilities::firefox::FirefoxPreferences;
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long)]
pub username: String,
#[arg(short, long)]
pub password: String,
#[arg(short, long, default_value_t = String::from(""))]
pub file: String,
#[arg(short, long, default_value_t = String::from(""))]
pub directory: String,
#[arg(short, long, default_value_t = String::from(""))]
pub extension: String,
#[arg(long, default_value_t = 60)]
pub file_timeout: usize,
#[arg(long, default_value_t = true)]
pub geckodriver_daemon: bool,
#[arg(long, default_value_t = false)]
pub debugging_mode: bool,
#[arg(long, default_value_t = String::from("https://www.amazon.com/sendtokindle"))]
pub amazon_url: String,
}
impl Default for Args {
fn default() -> Args {
Args {
username: String::from(""),
password: String::from(""),
file: String::from(""),
directory: String::from(""),
extension: String::from(""),
file_timeout: 60,
geckodriver_daemon: true,
debugging_mode: false,
amazon_url: String::from("https://www.amazon.com/sendtokindle"),
}
}
}
impl Args {
pub fn new(u: &str, p: &str) -> Args {
let mut args = Args::default();
args.username = String::from(u);
args.password = String::from(p);
return args;
}
}
pub async fn send_files_to_kindle(username: &str, password: &str, files: Vec<String>, file_timeout: usize, url: &str, daemon: bool, debugging_mode: bool) -> WebDriverResult<()> {
let mut gd_daemon = std::process::Command::new("echo").stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).spawn()?;
if daemon {
gd_daemon = std::process::Command::new("geckodriver").stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).spawn()?;
}
let user_agent = "Linux";
let mut prefs = FirefoxPreferences::new();
prefs.set_user_agent(user_agent.to_string())?;
let mut caps = FirefoxCapabilities::new();
caps.set_preferences(prefs)?;
if !debugging_mode {
caps.set_headless()?;
}
let driver = WebDriver::new("http://localhost:4444", caps).await?;
driver.goto(url).await?;
println!("Reached {}", url);
let signin_button = driver.find(By::Id("s2k-dnd-sign-in-button")).await?;
signin_button.click().await?;
println!("Found sign in button");
let email_input = driver.find(By::Css("input[type='email']")).await?;
email_input.send_keys(username).await?;
println!("Found email input and sent user email");
let continue_button = driver.find(By::Css("input[type='submit'][id='continue']")).await?;
continue_button.click().await?;
println!("Found and clicked continue button");
let password_input = driver.find(By::Css("input[type='password'][id='ap_password']")).await?;
password_input.send_keys(password).await?;
println!("Found password input and sent user password");
let sis_button = driver.find(By::Css("input[type='submit'][id='signInSubmit']")).await?;
sis_button.click().await?;
println!("Found and clicked sign in button");
driver.execute(r#"
let elem = document.getElementById("s2k-home-wrapper");
var input = document.createElement("input");
input.id = "hacky-file-input";
input.type = "file";
input.multiple = true;
elem.appendChild(input);
"#, Vec::new()).await?;
let file_input = driver.find(By::Id("hacky-file-input")).await?;
for f in files.clone() {
file_input.send_keys(f).await?;
}
driver.execute(r#"
let elem = document.getElementById("s2k-home-wrapper");
function CustomDataTransfer() {
var f = document.getElementById("hacky-file-input").files;
this.dropEffect = 'all';
this.effectAllowed = 'all';
this.items = [];
this.types = ['Files'];
this.files = f;
};
var customDropEvent = new DragEvent('drop');
Object.defineProperty(customDropEvent, 'dataTransfer', {
value: new CustomDataTransfer()
});
var button_input = document.createElement("button");
button_input.id = "hacky-button-file-input";
button_input.addEventListener('click', function(e) {
e.preventDefault();
// the fake event will be called on the button click
document.getElementById("s2k-dnd-area").dispatchEvent(customDropEvent);
});
elem.appendChild(button_input); // put it into the DOM
"#, Vec::new()
).await?;
let dnd_area = driver.find(By::Id("s2k-dnd-area")).await?;
if !dnd_area.is_displayed().await? {
println!("Something's off with the dnd area...");
}
else {
println!("Found dnd area, we are ready to send some files!");
}
let file_input_button = driver.find(By::Id("hacky-button-file-input")).await?;
file_input_button.click().await?;
driver.find(By::Css(".s2k-r2s-file-item")).await?;
println!("Found at least one item in the dnd area");
let add_to_library_label = driver.find(By::Id("s2k-r2s-add2lib")).await?;
let add_to_library_checkbox = add_to_library_label.find(By::Css("input[type='checkbox']")).await?;
if add_to_library_checkbox.prop("checked").await?.unwrap() != "true" {
add_to_library_label.click().await?;
}
println!("Found the 'Add to your library' checkbox, and ensured that it is checked");
if !debugging_mode {
let start = std::time::Instant::now();
let send_button = driver.find(By::Id("s2k-r2s-send-button")).await?;
send_button.click().await?;
println!("Found and clicked the send button");
let mut uploading = true;
println!("Waiting for files to upload...");
while uploading {
uploading = !dnd_area.is_displayed().await?;
if start.elapsed().as_secs() as usize > files.clone().len() * file_timeout {
println!("Waited for more than {} seconds per file and still appears that not everything was done. Giving up. (If your connection is slow, try increasing the --file-timeout argument)", file_timeout);
break;
}
std::thread::sleep(std::time::Duration::from_secs(3));
print!(".");
}
if !uploading {
println!("\nEverything uploaded successfully! :)");
}
}
else {
println!("DEBUGGING MODE: Press enter to close the browser window...");
let mut _s = String::new();
std::io::stdin().read_line(&mut _s)?;
}
driver.close_window().await?;
if daemon {
gd_daemon.kill()?;
}
Ok(())
}
pub async fn send_to_kindle(username: &str, password: &str, f: &str, ext: &str, file_timeout: usize, url: &str, daemon: bool, debugging_mode: bool) -> WebDriverResult<()> {
let mut files = Vec::<String>::new();
let source = std::path::Path::new(f);
if source.is_file() {
if ext == "" || ext == source.extension().unwrap_or(std::ffi::OsStr::new("")) {
files.push(String::from(f));
}
}
else if source.is_dir() {
for file in std::fs::read_dir(f).unwrap() {
let file_path = file.unwrap().path();
if file_path.ends_with(ext) {
files.push(file_path.display().to_string());
}
}
}
send_files_to_kindle(username, password, files, file_timeout, url, daemon, debugging_mode).await
}