summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock34
-rw-r--r--Cargo.toml1
-rw-r--r--src/db.rs20
-rw-r--r--src/exporter.rs36
-rw-r--r--src/lastfm/mod.rs4
-rw-r--r--src/main.rs14
6 files changed, 103 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 79a42c6..ecc1766 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -337,6 +337,7 @@ version = "0.1.0"
dependencies = [
"directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rusqlite 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -575,6 +576,18 @@ dependencies = [
]
[[package]]
+name = "pbr"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "percent-encoding"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -666,6 +679,14 @@ version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "redox_termios"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "remove_dir_all"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -879,6 +900,16 @@ dependencies = [
]
[[package]]
+name = "termion"
+version = "1.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "time"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1236,6 +1267,7 @@ dependencies = [
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5"
"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c"
+"checksum pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "deb73390ab68d81992bd994d145f697451bb0b54fd39738e72eef32458ad6907"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum phf 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)" = "cec29da322b242f4c3098852c77a0ca261c9c01b806cae85a5572a1eb94db9a6"
"checksum phf_codegen 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)" = "7d187f00cd98d5afbcd8898f6cf181743a449162aeb329dcd2f3849009e605ad"
@@ -1248,6 +1280,7 @@ dependencies = [
"checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372"
"checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
+"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
"checksum reqwest 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)" = "00a5870d8edc74fc6e1eb58edbd2815d2243e1a2255d6bf9c82a7a875901b5db"
"checksum rusqlite 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "39bae767eb27866f5c0be918635ae54af705bc09db11be2c43a3c6b361cf3462"
@@ -1273,6 +1306,7 @@ dependencies = [
"checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "55c1195ef8513f3273d55ff59fe5da6940287a0d7a98331254397f464833675b"
+"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
"checksum tokio 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "6e93c78d23cc61aa245a8acd2c4a79c4d7fa7fb5c3ca90d5737029f043a84895"
"checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f"
diff --git a/Cargo.toml b/Cargo.toml
index 4b24c57..68d7b9e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,6 +6,7 @@ authors = ["Jesse Luehrs <doy@tozt.net>"]
[dependencies]
directories = "1.0"
failure = "0.1"
+pbr = "1.0"
reqwest = "0.9"
rusqlite = "0.15"
serde = "1.0"
diff --git a/src/db.rs b/src/db.rs
index 450202f..b291e86 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,4 +1,5 @@
use error::Result;
+use lastfm;
const SCHEMA: &'static str = "
CREATE TABLE `tracks` (
@@ -56,4 +57,23 @@ impl DB {
}
})?)
}
+
+ pub fn insert_tracks<F: FnMut(lastfm::Track)>(
+ &self,
+ tracks: impl Iterator<Item=lastfm::Track>,
+ mut f: F
+ ) -> Result<()> {
+ let mut sth = self.conn.prepare("INSERT INTO tracks VALUES (?, ?, ?, ?)")?;
+ for track in tracks {
+ sth.execute(
+ &[
+ &track.artist as &rusqlite::types::ToSql,
+ &track.album,
+ &track.name,
+ &track.timestamp,
+ ]).map(|_| ())?;
+ f(track);
+ }
+ Ok(())
+ }
}
diff --git a/src/exporter.rs b/src/exporter.rs
new file mode 100644
index 0000000..89ad32c
--- /dev/null
+++ b/src/exporter.rs
@@ -0,0 +1,36 @@
+use db;
+use lastfm;
+
+use error::Result;
+
+pub struct Exporter<'d, 'l> {
+ db: &'d db::DB,
+ lastfm: &'l lastfm::LastFMClient,
+}
+
+impl<'d, 'l> Exporter<'d, 'l> {
+ pub fn new(
+ db: &'d db::DB,
+ lastfm: &'l lastfm::LastFMClient
+ ) -> Exporter<'d, 'l> {
+ Exporter {
+ db,
+ lastfm,
+ }
+ }
+
+ pub fn tracks_to_sync(&self) -> Result<u64> {
+ let ts = self.db.most_recent_timestamp()?;
+ Ok(self.lastfm.track_count(ts.map(|x| x + 1))?)
+ }
+
+ pub fn sync<F: FnMut(lastfm::Track)>(&self, track_cb: F) -> Result<()> {
+ let ts = self.db.most_recent_timestamp()?;
+ self.db.insert_tracks(
+ self.lastfm.tracks(ts.map(|x| x + 1)),
+ track_cb
+ )?;
+
+ Ok(())
+ }
+}
diff --git a/src/lastfm/mod.rs b/src/lastfm/mod.rs
index ea23ee5..a4ec8eb 100644
--- a/src/lastfm/mod.rs
+++ b/src/lastfm/mod.rs
@@ -15,8 +15,8 @@ pub struct LastFMClient {
pub struct Track {
pub artist: String,
- pub name: String,
pub album: String,
+ pub name: String,
pub timestamp: i64,
}
@@ -66,8 +66,8 @@ impl<'a> Tracks<'a> {
.map(|t| {
Ok(Track {
artist: t.artist.text.clone(),
- name: t.name.clone(),
album: t.album.text.clone(),
+ name: t.name.clone(),
timestamp: t.date.uts.parse()?,
})
})
diff --git a/src/main.rs b/src/main.rs
index 0df7810..ac8e5f8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
extern crate directories;
extern crate failure;
+extern crate pbr;
extern crate reqwest;
extern crate rusqlite;
extern crate serde;
@@ -8,6 +9,7 @@ extern crate serde_json;
extern crate serde_derive;
mod error;
+mod exporter;
mod lastfm;
mod paths;
mod db;
@@ -23,10 +25,14 @@ fn main() {
let db = db::DB::new(&paths::dbpath())
.expect("failed to create db");
let lastfm = lastfm::LastFMClient::new(api_key, username);
+ let exporter = exporter::Exporter::new(&db, &lastfm);
- println!("{}", lastfm.track_count().unwrap());
+ let to_fetch = exporter.tracks_to_sync().unwrap();
+ println!("need to download {} tracks", to_fetch);
+ let mut bar = pbr::ProgressBar::new(to_fetch);
- for track in lastfm.tracks().take(10) {
- println!("{}", track.name);
- }
+ exporter.sync(|_| { bar.inc(); })
+ .expect("failed to update db");
+
+ bar.finish_print("done");
}