bookshelf 1.1.4

A small and modular media manager
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
pub mod config;
pub mod fsio;
pub mod module_handler;
pub mod shelf;
pub mod tui;

use clap::{load_yaml, App};
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::{
    collections::{BTreeMap, BTreeSet},
    io::BufRead,
};

use crate::tui::ui::TUI;
use config::*;
use fsio::*;
use module_handler::*;
use shelf::*;

/// Derive the directory for an item
/// # Example:
/// ```
/// let data_root: String = String::from("/tmp/data");
/// let module: &str = "myMod";
/// let code: &str = "12345";
/// let out_dir: String = get_item_dir(data_root, module, code);
///
/// assert_eq!("/tmp/data/myMod/12345", out_dir);
/// ```
fn get_item_dir(data_root: &PathBuf, module: &str, code: &str) -> PathBuf {
    let mut pb = data_root.clone();
    pb.push(module);
    pb.push(code);
    pb
}

/// Given a URL, derive the module and code then add item to shelf, optionally
/// downloading said item
fn add_by_url(
    shelf: &mut Shelf,
    module_handler: &ModuleHandler,
    url: &str,
    data_root: Option<&PathBuf>,
    verbose: bool,
) -> Result<(), ModuleError> {
    match module_handler.derive_module(url) {
        Ok(module) => match module_handler.derive_code(module, url) {
            Ok(code) => {
                match add_by_code(
                    shelf,
                    &module_handler,
                    module,
                    code.as_str(),
                    data_root,
                    verbose,
                ) {
                    Ok(()) => Ok(()),
                    Err(e) => Err(e),
                }
            }
            Err(e) => Err(e),
        },
        Err(e) => Err(e),
    }
}

/// Given a module and code, add item to shelf, optionally downloading said item
fn add_by_code(
    shelf: &mut Shelf,
    module_handler: &ModuleHandler,
    module: &str,
    code: &str,
    data_root: Option<&PathBuf>,
    verbose: bool,
) -> Result<(), ModuleError> {
    if shelf.has_item(&module, &code) {
        if verbose {
            println!("Item {}/{} already indexed", &module, &code);
        }
        Ok(())
    } else {
        // Get metadata
        match module_handler.get_metadata(&module, &code) {
            // Print item (verbose)
            Ok(metadata) => {
                if verbose {
                    println!(
                        "Adding item: {}/{}\n\tTitle: {}\n\tAuthors: {}\n\tgenres: {}",
                        &module, &code, &metadata.0, &metadata.1, metadata.2
                    );
                }
                // title
                let title = metadata.0;
                // authors
                let mut authors: BTreeSet<String> = BTreeSet::new();
                for author in metadata.1.split(",") {
                    authors.insert(author.to_string());
                }
                // genres
                let mut genres: BTreeSet<String> = BTreeSet::new();
                for genre in metadata.2.split(",") {
                    genres.insert(genre.to_string());
                }
                // Construct item
                shelf.add_item(&module, &code, title, authors, genres);
                // Download if data_root is set
                if let Some(data_root) = data_root {
                    let dest_dir: PathBuf = get_item_dir(data_root, &module, &code);
                    match module_handler.download(&module, &code, &dest_dir) {
                        Ok(()) => Ok(()),
                        Err(e) => Err(e),
                    };
                }
                Ok(())
            }
            Err(e) => Err(e),
        }
    }
}

/// Add item to index and optionally download it.
/// Set data_root to None to skip download, or set it to Some() to download
fn add_item(
    shelf: &mut Shelf,
    module_handler: &ModuleHandler,
    url: Option<&str>,
    url_file: Option<&str>,
    module: Option<&str>,
    code: Option<&str>,
    code_file: Option<&str>,
    data_root: Option<&PathBuf>,
    verbose: bool,
) -> Result<(), BTreeMap<String, ModuleError>> {
    let mut errors: BTreeMap<String, ModuleError> = BTreeMap::new();
    if let Some(url) = url {
        // shelf add|download -u
        match add_by_url(shelf, &module_handler, url, data_root, verbose) {
            Ok(()) => {
                return Ok(());
            }
            Err(e) => {
                errors.insert(url.to_string(), e);
                return Err(errors);
            }
        }
    } else if let Some(url_file) = url_file {
        // shelf add|download -U
        match File::open(url_file) {
            Ok(file) => {
                for line in BufReader::new(file).lines() {
                    match line {
                        Ok(url) => {
                            match add_by_url(
                                shelf,
                                &module_handler,
                                url.as_str(),
                                data_root,
                                verbose,
                            ) {
                                Ok(()) => {}
                                Err(e) => {
                                    errors.insert(url, e);
                                }
                            }
                        }
                        Err(_e) => {}
                    }
                }
            }
            Err(_e) => {}
        }
    } else if let Some(module) = module {
        if let Some(code) = code {
            // shelf add|download -m MODULE -c CODE
            match add_by_code(shelf, &module_handler, module, code, data_root, verbose) {
                Ok(()) => {
                    return Ok(());
                }
                Err(e) => {
                    errors.insert(format!("{} {}", module, code), e);
                    return Err(errors);
                }
            }
        } else if let Some(code_file) = code_file {
            // shelf add|download -m MODULE -C CODE_FILE
            match File::open(code_file) {
                Ok(file) => {
                    for line in BufReader::new(file).lines() {
                        match line {
                            Ok(code) => {
                                match add_by_code(
                                    shelf,
                                    &module_handler,
                                    module,
                                    code.as_str(),
                                    data_root,
                                    verbose,
                                ) {
                                    Ok(()) => {}
                                    Err(e) => {
                                        errors.insert(format!("{} {}", module, code), e);
                                    }
                                }
                            }
                            Err(_e) => {}
                        }
                    }
                }
                Err(_e) => {}
            }
        }
    }
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

/// Print item info to stdout. To be used in CLI (single command) mode.
fn cli_print_item(shelf: &Shelf, module: &str, code: &str) {
    if let Some(item) = shelf.get_item(module, code) {
        let (i_title, i_authors, i_genres) = item.export();
        println!("Title: {}", i_title);
        let mut authors = String::new();
        for author in i_authors {
            authors.push_str(author);
            authors.push_str(", ");
        }
        authors.pop();
        authors.pop();
        println!("Authors: {}", authors);
        let mut genres = String::new();
        for genre in i_genres {
            genres.push_str(genre);
            genres.push_str(", ");
        }
        genres.pop();
        genres.pop();
        println!("Authors: {}", genres);
    } else {
    }
}

fn main() {
    /***** Parse arguments and load config *****/
    let arg_file = load_yaml!("args.yaml");
    let args = App::from(arg_file).get_matches();
    let mut config = Config::default();
    {
        let config = &mut config;
        if let Some(c) = args.value_of("config") {
            let path_to_config = PathBuf::from(c);
            if Path::new(&path_to_config).exists() {
                match config.update(&path_to_config) {
                    Ok(()) => {}
                    Err(e) => println!("Error loading config file: {:?}", e),
                }
            }
        } else {
            let mut home_dir = dirs_next::home_dir();
            match &mut home_dir {
                Some(h) => {
                    h.push(".config/bookshelf/bookshelf.yaml");
                    match config.update(&h) {
                        Ok(()) => {}
                        Err(e) => println!("Error loading config file: {:?}", e),
                    }
                }
                _ => println!("Error getting home dir"),
            }
        }
    }
    // Create necessary directories
    create_dirs(&config.data_dir, &config.modules_dir);
    let verbose: bool = { args.is_present("verbose") };

    /***** Initialize shelf and handlers *****/
    // These can be unwrap'd safely because load_config guarantees the entries
    let mut shelf: Shelf = load_shelf(&config.index_file);
    let module_handler = ModuleHandler::new(&config.modules_dir);

    /***** main *****/
    match args.subcommand() {
        Some(("modules", _args)) => {
            for i in module_handler.list_modules().iter() {
                println!("{}", i);
            }
        }
        Some(("add", args)) => {
            match add_item(
                &mut shelf,
                &module_handler,
                args.value_of("url"),
                args.value_of("url_file"),
                args.value_of("module"),
                args.value_of("code"),
                args.value_of("code_file"),
                None,
                verbose,
            ) {
                Ok(()) => {
                    println!("All items added sucessfully");
                }
                Err(errors) => {
                    println!("Some items failed to be added:");
                    for (item, error) in errors {
                        println!("{}: {:?}", item, error);
                    }
                }
            }
        }

        Some(("download", args)) => {
            match add_item(
                &mut shelf,
                &module_handler,
                args.value_of("url"),
                args.value_of("url_file"),
                args.value_of("module"),
                args.value_of("code"),
                args.value_of("code_file"),
                Some(&config.data_dir),
                verbose,
            ) {
                Ok(()) => {
                    println!("All items have been downloaded sucessfully");
                }
                Err(errors) => {
                    println!("Some items failed to be downloaded:");
                    for (item, error) in errors {
                        println!("{}: {:?}", item, error);
                    }
                }
            }
        }

        Some(("search", args)) => {
            match shelf.search_item(
                args.value_of("module"),
                args.value_of("title"),
                args.value_of("authors"),
                args.value_of("genres"),
                args.value_of("blacklist"),
                args.is_present("broad_search"),
                args.is_present("favorite"),
            ) {
                Ok(result) => {
                    for (m, c) in result {
                        println!("{} {}", &m, &c);
                        if verbose {
                            cli_print_item(&shelf, &m, &c);
                        }
                    }
                }
                Err(e) => {
                    println!("Error searching items: {}", e);
                }
            }
        }

        Some(("rm", args)) => {
            match shelf.search_item(
                args.value_of("module"),
                args.value_of("title"),
                args.value_of("authors"),
                args.value_of("genres"),
                args.value_of("blacklist"),
                args.is_present("broad_search"),
                args.is_present("favorite"),
            ) {
                Ok(result) => {
                    for (m, c) in result.iter() {
                        shelf.remove_item(m, c);
                    }
                }
                Err(e) => {
                    println!("Error removing items: {}", e);
                }
            }
        }

        Some(("pull", args)) => {
            match shelf.search_item(
                args.value_of("module"),
                args.value_of("title"),
                args.value_of("authors"),
                args.value_of("genres"),
                args.value_of("blacklist"),
                args.is_present("broad_search"),
                args.is_present("favorite"),
            ) {
                Ok(result) => {
                    for (m, c) in result {
                        let dest_dir = get_item_dir(&config.data_dir, &m, &c);
                        match module_handler.download(&m[..], &c[..], &dest_dir) {
                            Ok(()) => {}
                            Err(_e) => println!("Module {} unavailable", &m),
                        }
                    }
                }
                Err(e) => {
                    println!("Error pulling items: {}", e);
                }
            }
        }

        Some(("info", args)) => {
            cli_print_item(
                &shelf,
                args.value_of("module").unwrap(),
                args.value_of("code").unwrap(),
            );
        }

        Some(("edit", args)) => {
            shelf.edit_item(
                args.value_of("module"),
                args.value_of("code"),
                args.value_of("title"),
                args.value_of("authors"),
                args.value_of("genres"),
                args.is_present("favorite"),
            );
        }

        Some(("import", args)) => {
            import_shelf(&mut shelf, &PathBuf::from(args.value_of("file").unwrap()));
        }

        Some(("export", args)) => {
            export_shelf(&shelf, &PathBuf::from(args.value_of("file").unwrap()));
        }

        None => {
            // Start TUI if no argument is given
            let mut tui = TUI::new(&config, &mut shelf, &module_handler);
            match tui.start() {
                Ok(()) => {}
                Err(e) => {
                    println!("Error: {}", e)
                }
            }
        }

        _ => {
            println!("Invalid subcommand");
        }
    }

    /***** Save and exit *****/
    save_shelf(&shelf, &config.index_file);
}