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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
mod config;
pub mod download;
pub mod humble_api;
mod key_match;
pub mod util;
use anyhow::{anyhow, Context};
use clap::value_parser;
use clap::{Arg, Command};
use config::set_config;
use key_match::KeyMatch;
use std::fs;
use std::path;
use tabled::{object::Columns, Alignment, Modify, Style};
pub use config::{get_config, Config};
use humble_api::{ApiError, HumbleApi};
pub fn run() -> Result<(), anyhow::Error> {
let list_subcommand = Command::new("list")
.about("List all your purchased bundles")
.arg(
Arg::new("id-only")
.long("id-only")
.help("Print bundle IDs only")
.long_help(
"Print bundle IDs only. This can be used to chain commands together for automation.",
),
).arg(
Arg::new("claimed")
.long("claimed")
.takes_value(true)
.possible_values(["all", "yes", "no"])
.default_value("all")
.value_parser(value_parser!(String))
.help("Show claimed or unclaimed bundles only. This is mostly useful if you want to know which games you have not claimed yet.")
);
let auth_subcommand = Command::new("auth")
.about("Set the authentication session key")
.long_about(
"Set the session key used for authentication with Humble Bundle API. \
See online documentation on how to find the session key from your web browser.",
)
.arg(
Arg::new("SESSION-KEY")
.required(true)
.takes_value(true)
.help("Session key that's copied from your web browser"),
);
let details_subcommand = Command::new("details")
.about("Print details of a certain bundle")
.arg(
Arg::new("BUNDLE-KEY")
.required(true)
.takes_value(true)
.help("The key for the bundle which must be shown")
.long_help(
"The key for the bundle which must be shown. It can be partially entered.",
),
);
let download_subcommand = Command::new("download")
.about("Selectively download items from a bundle")
.arg(
Arg::new("BUNDLE-KEY")
.required(true)
.help("The key for the bundle which must be downloaded")
.long_help(
"The key for the bundle which must be downloaded. It can be partially entered."
)
)
.arg(
Arg::new("item-numbers")
.short('i')
.long("item-numbers")
.takes_value(true)
.help("Download only specified items")
.long_help(
"Download only specified items. This is a comman-separated list of item numbers to download. \
Item numbers begin from 1 and can be a single number or a range.\n\
Some examples:\n\n\
'--item-numbers 1,3,5' will download items 1, 3, and 5.\n\
'--item number 5-10' will download items 5 to 10 (inclusive)\n\n\
When specifying ranges, either the beginning or the end of the range can be omitted.\n\
For example, '--item-numbers 10-' will download items 10 to the end.
"
)
)
.arg(
Arg::new("format")
.short('f')
.long("format")
.takes_value(true)
.multiple_occurrences(true)
.help("Filter downloaded items by their format")
.long_help(
"Filter downloaded files by their format. Formats are case-insensitive and \
this filter can be used several times to specify multiple formats.\n\n\
For example: --filter-by-format epub --filter-by-format mobi"
)
)
.arg(
Arg::new("max-size")
.short('s')
.long("max-size")
.takes_value(true)
.help("Filter downloaded items by their maximum size")
.long_help(
"Filter downloaded items by their maximum size. This will skip any sub-item in a bundle \
that exceeds this limit. \
You can use the traditional size units such as KB or MiB. Make sure there is no space \
between the number and the unit. For example 14MB or 4GiB.\n\n\
Note: The size limit works on a sub-item level, and not per file. \
For example, if you specify a limit of 10 MB and a sub-item has two 6 MB books in it, \
this sub-items will not be downloaded, because its total size exceeds the 10 MB limit (12 MB in total)."
)
);
let sub_commands = vec![
auth_subcommand,
list_subcommand,
details_subcommand,
download_subcommand,
];
let crate_name = clap::crate_name!();
let matches = clap::Command::new(crate_name)
.about("The missing Humble Bundle CLI")
.version(clap::crate_version!())
.after_help("Note: `humble-cli -h` prints a short and concise overview while `humble-cli --help` gives all details.")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommands(sub_commands)
.get_matches();
return match matches.subcommand() {
Some(("auth", sub_matches)) => auth(sub_matches),
Some(("details", sub_matches)) => show_bundle_details(sub_matches),
Some(("download", sub_matches)) => download_bundle(sub_matches),
Some(("list", sub_matches)) => list_bundles(sub_matches),
_ => Ok(()),
};
}
fn auth(matches: &clap::ArgMatches) -> Result<(), anyhow::Error> {
let session_key = matches.value_of("SESSION-KEY").unwrap();
set_config(Config {
session_key: session_key.to_owned(),
})
}
fn handle_http_errors<T>(input: Result<T, ApiError>) -> Result<T, anyhow::Error> {
match input {
Ok(val) => Ok(val),
Err(ApiError::NetworkError(e)) if e.is_status() => {
return match e.status().unwrap() {
reqwest::StatusCode::UNAUTHORIZED => Err(anyhow!(
"Unauthorized request (401). Is the session key correct?"
)),
reqwest::StatusCode::NOT_FOUND => Err(anyhow!(
"Bundle not found (404). Is the bundle key correct?"
)),
s => Err(anyhow!("failed with status: {}", s)),
}
}
Err(e) => return Err(anyhow!("failed: {}", e)),
}
}
fn list_bundles(matches: &clap::ArgMatches) -> Result<(), anyhow::Error> {
let id_only = matches.is_present("id-only");
let claimed_filter = matches.get_one::<String>("claimed").unwrap();
let config = get_config()?;
let api = HumbleApi::new(&config.session_key);
if id_only && claimed_filter == "all" {
let ids = handle_http_errors(api.list_bundle_keys())?;
for id in ids {
println!("{}", id);
}
return Ok(());
}
let mut bundles = handle_http_errors(api.list_bundles())?;
if claimed_filter != "all" {
let claimed = claimed_filter == "yes";
bundles = bundles
.into_iter()
.filter(|b| b.claimed == claimed)
.collect();
}
if id_only {
for b in bundles {
println!("{}", b.gamekey);
}
return Ok(());
}
println!("{} bundle(s) found.", bundles.len());
if bundles.len() == 0 {
return Ok(());
}
let mut builder =
tabled::builder::Builder::default().set_columns(["Key", "Name", "Size", "Claimed"]);
for p in bundles {
builder = builder.add_record([
p.gamekey.as_str(),
p.details.human_name.as_str(),
util::humanize_bytes(p.total_size()).as_str(),
if p.claimed { "Yes" } else { "No" },
]);
}
let table = builder
.build()
.with(Style::psql())
.with(Modify::new(Columns::single(1)).with(Alignment::left()))
.with(Modify::new(Columns::single(2)).with(Alignment::right()));
println!("{table}");
Ok(())
}
fn find_key(all_keys: Vec<String>, key_to_find: &str) -> Option<String> {
let key_match = KeyMatch::new(all_keys, key_to_find);
let keys = key_match.get_matches();
match keys.len() {
1 => Some(keys[0].clone()),
0 => {
eprintln!("No bundle matches '{}'", key_to_find);
None
}
_ => {
eprintln!("More than one bundle matches '{}':", key_to_find);
for key in keys {
eprintln!("{}", key);
}
None
}
}
}
fn show_bundle_details(matches: &clap::ArgMatches) -> Result<(), anyhow::Error> {
let config = get_config()?;
let bundle_key = matches.value_of("BUNDLE-KEY").unwrap();
let api = crate::HumbleApi::new(&config.session_key);
let bundle_key = match find_key(handle_http_errors(api.list_bundle_keys())?, bundle_key) {
Some(key) => key,
None => return Ok(()),
};
let bundle = handle_http_errors(api.read_bundle(&bundle_key))?;
println!();
println!("{}", bundle.details.human_name);
println!("Purchased: {}", bundle.created.format("%v %I:%M %p"));
println!("Total size: {}", util::humanize_bytes(bundle.total_size()));
println!();
let mut builder = tabled::builder::Builder::default();
builder = builder.set_columns(["#", "Sub-item", "Format", "Total Size"]);
for (idx, entry) in bundle.products.iter().enumerate() {
builder = builder.add_record([
&(idx + 1).to_string(),
&entry.human_name,
&entry.formats(),
&util::humanize_bytes(entry.total_size()),
]);
}
let table = builder
.build()
.with(Style::psql())
.with(Modify::new(Columns::single(0)).with(Alignment::right()))
.with(Modify::new(Columns::single(1)).with(Alignment::left()))
.with(Modify::new(Columns::single(2)).with(Alignment::left()))
.with(Modify::new(Columns::single(3)).with(Alignment::right()));
println!("{table}");
Ok(())
}
fn download_bundle(matches: &clap::ArgMatches) -> Result<(), anyhow::Error> {
let config = get_config()?;
let bundle_key = matches.value_of("BUNDLE-KEY").unwrap();
let formats = if let Some(values) = matches.values_of("format") {
values.map(|f| f.to_lowercase()).collect::<Vec<_>>()
} else {
vec![]
};
let max_size: u64 = if let Some(byte_str) = matches.value_of("max-size") {
util::byte_string_to_number(byte_str)
.context(format!("failed to parse the specified size: {}", byte_str))?
} else {
0
};
let api = crate::HumbleApi::new(&config.session_key);
let bundle_key = match find_key(handle_http_errors(api.list_bundle_keys())?, bundle_key) {
Some(key) => key,
None => return Ok(()),
};
let bundle = handle_http_errors(api.read_bundle(&bundle_key))?;
let item_numbers = if let Some(value) = matches.value_of("item-numbers") {
let ranges = value.split(',').collect::<Vec<_>>();
util::union_usize_ranges(&ranges, bundle.products.len())?
} else {
vec![]
};
let products = bundle
.products
.iter()
.enumerate()
.filter(|&(i, _)| item_numbers.is_empty() || item_numbers.contains(&(i + 1)))
.map(|(_, p)| p)
.filter(|p| max_size == 0 || p.total_size() < max_size)
.filter(|p| {
formats.is_empty() || util::str_vectors_intersect(&p.formats_as_vec(), &formats)
})
.collect::<Vec<_>>();
if products.is_empty() {
println!("Nothing to download");
return Ok(());
}
let dir_name = util::replace_invalid_chars_in_filename(&bundle.details.human_name);
let bundle_dir = create_dir(&dir_name)?;
let client = reqwest::Client::new();
for product in products {
if max_size > 0 && product.total_size() > max_size {
continue;
}
println!();
println!("{}", product.human_name);
let dir_name = util::replace_invalid_chars_in_filename(&product.human_name);
let entry_dir = bundle_dir.join(dir_name);
if !entry_dir.exists() {
fs::create_dir(&entry_dir)?;
}
for product_download in product.downloads.iter() {
for dl_info in product_download.items.iter() {
if !formats.is_empty() && !formats.contains(&dl_info.format.to_lowercase()) {
println!("Skipping '{}'", dl_info.format);
continue;
}
let filename = util::extract_filename_from_url(&dl_info.url.web).context(
format!("Cannot get file name from URL '{}'", &dl_info.url.web),
)?;
let download_path = entry_dir.join(&filename);
let f = download::download_file(
&client,
&dl_info.url.web,
download_path.to_str().unwrap(),
&filename,
);
util::run_future(f)?;
}
}
}
Ok(())
}
fn create_dir(dir: &str) -> Result<path::PathBuf, std::io::Error> {
let dir = path::Path::new(dir).to_owned();
if !dir.exists() {
fs::create_dir(&dir)?;
}
Ok(dir)
}