summaryrefslogtreecommitdiffstats
path: root/src/cmd/sync.rs
blob: 99e39f7d593f6b95574afc9ea23de94b273e2cdb (plain) (blame)
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
use db;
use lastfm;
use util;

use clap;

pub struct Command {
    username: String,
}

pub fn subcommand<'a, 'b>() -> clap::App<'a, 'b> {
    clap::SubCommand::with_name("sync")
        .about("Updates the local copy of track data from last.fm")
        .arg(
            clap::Arg::with_name("username")
                .required(true)
                .help("last.fm username to fetch tracks for")
        )
}

impl Command {
    pub fn new<'a>(matches: &clap::ArgMatches<'a>) -> Command {
        Command {
            username: matches.value_of("username").unwrap().to_string(),
        }
    }
}

impl super::Command for Command {
    fn run(&self) -> failure::Fallible<()> {
        let db = db::DB::new(&util::db_path()?)?;
        let lastfm = lastfm::LastFMClient::new(&self.username)?;

        let from = db.most_recent_timestamp()?.map(|x| x + 1);
        let to_fetch = lastfm.track_count(from)?;

        if to_fetch > 0 {
            let bar = indicatif::ProgressBar::new(to_fetch);
            bar.set_style(
                indicatif::ProgressStyle::default_bar()
                    .progress_chars("=> ")
                    .template("Downloading {pos}/{len} tracks...\n{percent:>3}% [{wide_bar}] {eta:5}")
            );

            db.insert_tracks(bar.wrap_iter(lastfm.tracks(from)))?;

            bar.finish_with_message("done");
        }

        Ok(())
    }
}