From 3f4a6ec11e55a1d5634ba24e2dfd32d9e1d70d65 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 6 Nov 2018 23:26:02 -0500 Subject: make db syncing work --- src/exporter.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/exporter.rs (limited to 'src/exporter.rs') 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 { + let ts = self.db.most_recent_timestamp()?; + Ok(self.lastfm.track_count(ts.map(|x| x + 1))?) + } + + pub fn sync(&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(()) + } +} -- cgit v1.2.3-54-g00ecf