summaryrefslogtreecommitdiffstats
path: root/src/cmd/sync.rs
blob: 7dfb55997e70e85a6104e45e904ecf1137e31923 (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
use cli;
use db;
use lastfm;
use paths;

use failure;

pub fn run(opts: &cli::Options) -> failure::Fallible<()> {
    let db = db::DB::new(&paths::dbpath())?;
    let lastfm = lastfm::LastFMClient::new(
        opts.api_key.as_ref().unwrap(),
        opts.username.as_ref().unwrap()
    );

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

    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(())
}