Cleanup of downloader module.
This commit is contained in:
parent
3660fe0350
commit
07c1395c29
76
Cargo.lock
generated
76
Cargo.lock
generated
@ -25,11 +25,6 @@ name = "ansi_term"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "antidote"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "atk-sys"
|
||||
version = "0.4.0"
|
||||
@ -569,8 +564,10 @@ dependencies = [
|
||||
"hyper 0.11.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"reqwest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rss 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -608,24 +605,6 @@ name = "httparse"
|
||||
version = "1.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "0.10.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "0.11.6"
|
||||
@ -649,16 +628,6 @@ dependencies = [
|
||||
"unicase 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-native-tls"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"native-tls 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-tls"
|
||||
version = "0.1.2"
|
||||
@ -791,14 +760,6 @@ dependencies = [
|
||||
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.5"
|
||||
@ -1097,21 +1058,6 @@ dependencies = [
|
||||
"futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hyper-native-tls 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libflate 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.8.1"
|
||||
@ -1152,7 +1098,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"derive_builder 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quick-xml 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"reqwest 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1445,16 +1390,6 @@ dependencies = [
|
||||
"tokio-io 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "traitobject"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "typeable"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "unicase"
|
||||
version = "1.4.2"
|
||||
@ -1580,7 +1515,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a"
|
||||
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
|
||||
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
|
||||
"checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5"
|
||||
"checksum atk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c69658a4e18d5c9575f716e24559645d08a4044d6946c30c2e0025952c84d842"
|
||||
"checksum atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "21e50800ec991574876040fff8ee46b136a53e985286fbe6a3bdfe6421b78860"
|
||||
"checksum backtrace 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "99f2ce94e22b8e664d95c57fff45b98a966c2252b60691d0b7aeeccd88d70983"
|
||||
@ -1637,9 +1571,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum gtk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "854b56ce6d6b05945f7735651482835c5ac1f8582142ce67306726259a3dafb0"
|
||||
"checksum html5ever 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba3a1fd1857a714d410c191364c5d7bf8a6487c0ab5575146d37dd7eb17ef523"
|
||||
"checksum httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "af2f2dd97457e8fb1ae7c5a420db346af389926e36f43768b96f101546b04a07"
|
||||
"checksum hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)" = "368cb56b2740ebf4230520e2b90ebb0461e69034d85d1945febd9b3971426db2"
|
||||
"checksum hyper 0.11.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1b45eac8b696d59491b079bd04fcb0f3488c0f6ed62dcb36bcfea8a543e9cdc3"
|
||||
"checksum hyper-native-tls 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "72332e4a35d3059583623b50e98e491b78f8b96c5521fcb3f428167955aa56e8"
|
||||
"checksum hyper-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c81fa95203e2a6087242c38691a0210f23e9f3f8f944350bd676522132e2985"
|
||||
"checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d"
|
||||
"checksum iovec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b6e8b9c2247fcf6c6a1151f1156932be5606c9fd6f55a2d7f9fc1cb29386b2f7"
|
||||
@ -1657,7 +1589,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum markup5ever 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2cf89d3e0486c32c9d99521455ddf9a438910a1ce2bd376936086edc15dff5fc"
|
||||
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
|
||||
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
|
||||
"checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0"
|
||||
"checksum mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e2e00e17be181010a91dbfefb01660b17311059dc8c7f48b9017677721e732bd"
|
||||
"checksum mime_guess 2.0.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27a5e6679a0614e25adc14c6434ba84e41632b765a6d9cb2031a0cca682699ae"
|
||||
"checksum mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0e8411968194c7b139e9105bc4ae7db0bae232af087147e72f0616ebf5fdb9cb"
|
||||
@ -1691,7 +1622,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"
|
||||
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
|
||||
"checksum relay 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f301bafeb60867c85170031bdb2fcf24c8041f33aee09e7b116a58d4e9f781c5"
|
||||
"checksum reqwest 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1d56dbe269dbe19d716b76ec8c3efce8ef84e974f5b7e5527463e8c0507d4e17"
|
||||
"checksum reqwest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f73a8482e3b2b20ef5c07168b27048fc3778a012ce9b11a021556a450a01e9b5"
|
||||
"checksum rfc822_sanitizer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "680e8305c1e0cdf836dc4bec5424e045f278c975a3cac36d1ca01c4695f9d815"
|
||||
"checksum rss 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "70294b2be2d620fed3939032067684c53b8ccae18e8ca0b8410447f0f07228c5"
|
||||
@ -1729,8 +1659,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fbb47ae81353c63c487030659494b295f6cb6576242f907f203473b191b0389"
|
||||
"checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162"
|
||||
"checksum tokio-tls 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d88e411cac1c87e405e4090be004493c5d8072a370661033b1a64ea205ec2e13"
|
||||
"checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
||||
"checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"
|
||||
"checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
|
||||
"checksum unicase 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2e01da42520092d0cd2d6ac3ae69eb21a22ad43ff195676b86f8c37f487d6b80"
|
||||
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
|
||||
|
||||
@ -1,24 +1,30 @@
|
||||
[package]
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
name = "hammond-data"
|
||||
version = "0.1.0"
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
rfc822_sanitizer = "0.3"
|
||||
chrono = "0.4"
|
||||
# diesel wants dotenv with a feature disabled and breaks rss
|
||||
dotenv = "0.10"
|
||||
error-chain = "0.11"
|
||||
lazy_static = "0.2"
|
||||
log = "0.3"
|
||||
rayon = "0.8"
|
||||
reqwest = "0.8"
|
||||
error-chain = "0.11"
|
||||
log = "0.3"
|
||||
diesel = { version = "0.16", features = ["sqlite"] }
|
||||
diesel_codegen = { version = "0.16", features = ["sqlite"] }
|
||||
xdg = "2.1"
|
||||
lazy_static = "0.2"
|
||||
rfc822_sanitizer = "0.3"
|
||||
rss = "1.1"
|
||||
chrono = "0.4"
|
||||
# overwrite diesel dependancy that disables a feature rss depends upon
|
||||
dotenv = "*"
|
||||
xdg = "2.1"
|
||||
|
||||
[dependencies.diesel]
|
||||
features = ["sqlite"]
|
||||
version = "0.16"
|
||||
|
||||
[dependencies.diesel_codegen]
|
||||
features = ["sqlite"]
|
||||
version = "0.16"
|
||||
|
||||
[dev-dependencies]
|
||||
tempdir = "0.3"
|
||||
rand = "0.3"
|
||||
tempdir = "0.3"
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
use reqwest;
|
||||
use diesel::SaveChangesDsl;
|
||||
use diesel::result::QueryResult;
|
||||
use reqwest::header::{ETag, LastModified};
|
||||
|
||||
use schema::{episode, podcast, source};
|
||||
@ -105,10 +106,9 @@ impl Episode {
|
||||
self.played = value;
|
||||
}
|
||||
|
||||
pub fn save(&self, db: &Database) -> Result<()> {
|
||||
pub fn save(&self, db: &Database) -> QueryResult<Episode> {
|
||||
let tempdb = db.lock().unwrap();
|
||||
self.save_changes::<Episode>(&*tempdb)?;
|
||||
Ok(())
|
||||
self.save_changes::<Episode>(&*tempdb)
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,10 +163,9 @@ impl Podcast {
|
||||
self.image_uri = value.map(|x| x.to_string());
|
||||
}
|
||||
|
||||
pub fn save(&self, db: &Database) -> Result<()> {
|
||||
pub fn save(&self, db: &Database) -> QueryResult<Podcast> {
|
||||
let tempdb = db.lock().unwrap();
|
||||
self.save_changes::<Podcast>(&*tempdb)?;
|
||||
Ok(())
|
||||
self.save_changes::<Podcast>(&*tempdb)
|
||||
}
|
||||
}
|
||||
|
||||
@ -227,10 +226,9 @@ impl<'a> Source {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save(&self, db: &Database) -> Result<()> {
|
||||
pub fn save(&self, db: &Database) -> QueryResult<Source> {
|
||||
let tempdb = db.lock().unwrap();
|
||||
self.save_changes::<Source>(&*tempdb)?;
|
||||
Ok(())
|
||||
self.save_changes::<Source>(&*tempdb)
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,3 +275,16 @@ pub struct NewPodcast {
|
||||
pub image_uri: Option<String>,
|
||||
pub source_id: i32,
|
||||
}
|
||||
|
||||
impl NewPodcast {
|
||||
pub fn into_podcast(self) -> Podcast {
|
||||
Podcast {
|
||||
id: 0,
|
||||
title: self.title,
|
||||
link: self.link,
|
||||
description: self.description,
|
||||
image_uri: self.image_uri,
|
||||
source_id: self.source_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,23 @@
|
||||
[package]
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
name = "hammond-downloader"
|
||||
version = "0.1.0"
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
hammond-data = {path = "../hammond-data"}
|
||||
dotenv = "*"
|
||||
error-chain = "0.11"
|
||||
hyper = "0.11"
|
||||
log = "0.3"
|
||||
mime = "0.3"
|
||||
rand = "0.3"
|
||||
reqwest = "0.8"
|
||||
hyper = "0.11"
|
||||
diesel = { version = "0.16", features = ["sqlite"] }
|
||||
rss = { version = "1.1", features = ["from_url"]}
|
||||
dotenv = "*"
|
||||
rss = "1.1"
|
||||
tempdir = "0.3"
|
||||
|
||||
[dependencies.diesel]
|
||||
features = ["sqlite"]
|
||||
version = "0.16"
|
||||
|
||||
[dependencies.hammond-data]
|
||||
path = "../hammond-data"
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
use reqwest;
|
||||
use hyper::header::*;
|
||||
use tempdir::TempDir;
|
||||
use rand;
|
||||
use rand::Rng;
|
||||
// use mime::Mime;
|
||||
|
||||
use std::fs::{rename, DirBuilder, File};
|
||||
@ -9,7 +12,7 @@ use std::path::Path;
|
||||
|
||||
use errors::*;
|
||||
use hammond_data::index_feed::Database;
|
||||
use hammond_data::models::Episode;
|
||||
use hammond_data::models::{Episode, Podcast};
|
||||
use hammond_data::{DL_DIR, HAMMOND_CACHE};
|
||||
|
||||
// Adapted from https://github.com/mattgathu/rget .
|
||||
@ -18,7 +21,7 @@ use hammond_data::{DL_DIR, HAMMOND_CACHE};
|
||||
// Would much rather use a crate,
|
||||
// or bindings for a lib like youtube-dl(python),
|
||||
// But cant seem to find one.
|
||||
pub fn download_to(dir: &str, filename: &str, url: &str) -> Result<String> {
|
||||
pub fn download_to(dir: &str, file_title: &str, url: &str) -> Result<String> {
|
||||
info!("GET request to: {}", url);
|
||||
let client = reqwest::Client::builder().referer(false).build()?;
|
||||
let mut resp = client.get(url).send()?;
|
||||
@ -32,26 +35,39 @@ pub fn download_to(dir: &str, filename: &str, url: &str) -> Result<String> {
|
||||
ct_len.map(|x| info!("File Lenght: {}", x));
|
||||
ct_type.map(|x| info!("Content Type: {}", x));
|
||||
|
||||
let target = format!("{}/{}", dir, filename);
|
||||
// let target = format!("{}{}",dir, filename, ext);
|
||||
return save_io(&target, &mut resp, ct_len);
|
||||
// FIXME: Unreliable and hacky way to extract the file extension from the url.
|
||||
// https://gitlab.gnome.org/alatiera/Hammond/issues/5
|
||||
let ext = url.split('.').last().unwrap();
|
||||
// Construct the download path.
|
||||
// TODO: Check if its a valid path
|
||||
let filename = format!("{}.{}", file_title, ext);
|
||||
|
||||
return save_io(dir, &filename, &mut resp, ct_len);
|
||||
}
|
||||
// Ok(String::from(""))
|
||||
panic!("foo");
|
||||
}
|
||||
|
||||
fn save_io(
|
||||
target: &str,
|
||||
target_dir: &str,
|
||||
filename: &str,
|
||||
resp: &mut reqwest::Response,
|
||||
content_lenght: Option<u64>,
|
||||
) -> Result<String> {
|
||||
info!("Downloading into: {}", target);
|
||||
info!("Downloading into: {}", target_dir);
|
||||
let chunk_size = match content_lenght {
|
||||
Some(x) => x as usize / 99,
|
||||
None => 1024 as usize, // default chunk size
|
||||
};
|
||||
|
||||
let out_file = format!("{}.part", target);
|
||||
let tempdir = TempDir::new(target_dir)?;
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let out_file = format!(
|
||||
"{}/{}.part",
|
||||
tempdir.path().to_str().unwrap(),
|
||||
rng.gen::<usize>()
|
||||
);
|
||||
let mut writer = BufWriter::new(File::create(&out_file)?);
|
||||
|
||||
loop {
|
||||
@ -64,9 +80,11 @@ fn save_io(
|
||||
break;
|
||||
}
|
||||
}
|
||||
rename(out_file, target)?;
|
||||
info!("Downloading of {} completed succesfully.", target);
|
||||
Ok(target.to_string())
|
||||
|
||||
let target = format!("{}/{}", target_dir, filename);
|
||||
rename(out_file, &target)?;
|
||||
info!("Downloading of {} completed succesfully.", &target);
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
pub fn get_download_folder(pd_title: &str) -> Result<String> {
|
||||
@ -90,21 +108,11 @@ pub fn get_episode(connection: &Database, ep: &mut Episode, download_folder: &st
|
||||
ep.save(connection)?;
|
||||
};
|
||||
|
||||
// FIXME: Unreliable and hacky way to extract the file extension from the url.
|
||||
// https://gitlab.gnome.org/alatiera/Hammond/issues/5
|
||||
let ext = ep.uri().split('.').last().unwrap().to_owned();
|
||||
let res = download_to(download_folder, ep.title().unwrap(), ep.uri());
|
||||
|
||||
// Construct the download path.
|
||||
// TODO: Check if its a valid path
|
||||
let file_name = format!("/{}.{}", ep.title().unwrap().to_owned(), ext);
|
||||
|
||||
let uri = ep.uri().to_owned();
|
||||
let res = download_to(download_folder, &file_name, uri.as_str());
|
||||
|
||||
if res.is_ok() {
|
||||
if let Ok(path) = res {
|
||||
// If download succedes set episode local_uri to dlpath.
|
||||
let dlpath = res.unwrap();
|
||||
ep.set_local_uri(Some(&dlpath));
|
||||
ep.set_local_uri(Some(&path));
|
||||
ep.save(connection)?;
|
||||
Ok(())
|
||||
} else {
|
||||
@ -113,40 +121,46 @@ pub fn get_episode(connection: &Database, ep: &mut Episode, download_folder: &st
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn cache_image(pd: &Podcast) -> Option<String> {
|
||||
// TODO: Right unit test
|
||||
// TODO: Refactor
|
||||
pub fn cache_image(title: &str, image_uri: Option<&str>) -> Option<String> {
|
||||
if let Some(url) = image_uri {
|
||||
pub fn cache_image(pd: &Podcast) -> Option<String> {
|
||||
if pd.image_uri().is_some() {
|
||||
let url = pd.image_uri().unwrap().to_owned();
|
||||
if url == "" {
|
||||
return None;
|
||||
}
|
||||
|
||||
// FIXME: https://gitlab.gnome.org/alatiera/Hammond/issues/5
|
||||
let ext = url.split('.').last().unwrap();
|
||||
let download_fold = format!(
|
||||
"{}{}",
|
||||
HAMMOND_CACHE.to_str().unwrap(),
|
||||
pd.title().to_owned()
|
||||
);
|
||||
|
||||
// Hacky way
|
||||
// TODO: make it so it returns the first cover.* file encountered.
|
||||
let png = format!("{}/cover.png", download_fold);
|
||||
let jpg = format!("{}/cover.jpg", download_fold);
|
||||
let jpeg = format!("{}/cover.jpeg", download_fold);
|
||||
if Path::new(&png).exists() {
|
||||
return Some(png);
|
||||
} else if Path::new(&jpg).exists() {
|
||||
return Some(jpg);
|
||||
} else if Path::new(&jpeg).exists() {
|
||||
return Some(jpeg);
|
||||
};
|
||||
|
||||
let download_fold = format!("{}{}", HAMMOND_CACHE.to_str().unwrap(), title);
|
||||
DirBuilder::new()
|
||||
.recursive(true)
|
||||
.create(&download_fold)
|
||||
.unwrap();
|
||||
let file_name = format!("cover.{}", ext);
|
||||
|
||||
// This will need rework once the #5 is completed.
|
||||
let dlpath = format!("{}/{}", download_fold, file_name);
|
||||
|
||||
if Path::new(&dlpath).exists() {
|
||||
return Some(dlpath);
|
||||
}
|
||||
|
||||
if let Err(err) = download_to(&download_fold, &file_name, url) {
|
||||
let dlpath = download_to(&download_fold, "cover", &url);
|
||||
if let Ok(path) = dlpath {
|
||||
info!("Cached img into: {}", &path);
|
||||
return Some(path);
|
||||
} else {
|
||||
error!("Failed to get feed image.");
|
||||
error!("Error: {}", err);
|
||||
error!("Error: {}", dlpath.unwrap_err());
|
||||
return None;
|
||||
};
|
||||
|
||||
info!("Cached img into: {}", dlpath);
|
||||
return Some(dlpath);
|
||||
}
|
||||
None
|
||||
}
|
||||
@ -155,6 +169,7 @@ pub fn cache_image(title: &str, image_uri: Option<&str>) -> Option<String> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use hammond_data::{DL_DIR, HAMMOND_CACHE};
|
||||
use hammond_data::models::NewPodcast;
|
||||
|
||||
#[test]
|
||||
fn test_get_dl_folder() {
|
||||
@ -164,13 +179,20 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_cache_image() {
|
||||
let img_path =
|
||||
cache_image("New Rustacean", Some("http://newrustacean.com/podcast.png")).unwrap();
|
||||
let pd = NewPodcast {
|
||||
title: "New Rustacean".to_string(),
|
||||
description: "".to_string(),
|
||||
link: "".to_string(),
|
||||
image_uri: Some("http://newrustacean.com/podcast.png".to_string()),
|
||||
source_id: 0,
|
||||
};
|
||||
let pd = pd.into_podcast();
|
||||
let img_path = cache_image(&pd);
|
||||
let foo_ = format!(
|
||||
"{}{}/cover.png",
|
||||
HAMMOND_CACHE.to_str().unwrap(),
|
||||
"New Rustacean"
|
||||
);
|
||||
assert_eq!(img_path, foo_);
|
||||
assert_eq!(img_path, Some(foo_));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,11 @@
|
||||
use diesel::result;
|
||||
use hammond_data;
|
||||
use reqwest;
|
||||
use rss;
|
||||
use std::io;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
ReqError(reqwest::Error);
|
||||
IoError(io::Error);
|
||||
RSSError(rss::Error);
|
||||
DieselResultError(result::Error);
|
||||
HamDBError(hammond_data::errors::Error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,8 +8,10 @@ extern crate hyper;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate mime;
|
||||
extern crate rand;
|
||||
extern crate reqwest;
|
||||
extern crate rss;
|
||||
// extern crate rss;
|
||||
extern crate tempdir;
|
||||
|
||||
pub mod downloader;
|
||||
pub mod errors;
|
||||
|
||||
@ -1,22 +1,27 @@
|
||||
[package]
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
build = "build.rs"
|
||||
name = "hammond-gtk"
|
||||
version = "0.1.0"
|
||||
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
workspace = "../"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
gtk = { version = "0.2", features = ["v3_22"]}
|
||||
gdk = "0.6"
|
||||
glib = "0.3"
|
||||
gio = "0.2"
|
||||
gdk-pixbuf = "0.2"
|
||||
|
||||
loggerv = "0.4"
|
||||
log = "0.3"
|
||||
open = "1.2"
|
||||
dissolve = "0.2"
|
||||
gdk = "0.6"
|
||||
gdk-pixbuf = "0.2"
|
||||
gio = "0.2"
|
||||
glib = "0.3"
|
||||
log = "0.3"
|
||||
loggerv = "0.4"
|
||||
open = "1.2"
|
||||
rayon = "0.8"
|
||||
|
||||
hammond-data = {path = "../hammond-data"}
|
||||
hammond-downloader = {path = "../hammond-downloader"}
|
||||
[dependencies.gtk]
|
||||
features = ["v3_22"]
|
||||
version = "0.2"
|
||||
|
||||
[dependencies.hammond-data]
|
||||
path = "../hammond-data"
|
||||
|
||||
[dependencies.hammond-downloader]
|
||||
path = "../hammond-downloader"
|
||||
|
||||
@ -46,7 +46,7 @@ fn create_flowbox_child(db: &Database, pd: &Podcast) -> gtk::FlowBoxChild {
|
||||
|
||||
pd_title.set_text(pd.title());
|
||||
|
||||
let cover = get_pixbuf_from_path(pd.title(), pd.image_uri());
|
||||
let cover = get_pixbuf_from_path(pd);
|
||||
if let Some(img) = cover {
|
||||
pd_cover.set_from_pixbuf(&img);
|
||||
};
|
||||
|
||||
@ -40,7 +40,7 @@ pub fn podcast_widget(db: &Database, stack: >k::Stack, pd: &Podcast) -> gtk::B
|
||||
buff.set_text(pd.description());
|
||||
}
|
||||
|
||||
let img = get_pixbuf_from_path(pd.title(), pd.image_uri());
|
||||
let img = get_pixbuf_from_path(pd);
|
||||
if let Some(i) = img {
|
||||
cover.set_from_pixbuf(&i);
|
||||
}
|
||||
@ -101,8 +101,8 @@ fn show_played_button(db: &Database, pd: &Podcast, played_button: >k::Button)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pixbuf_from_path(pd_title: &str, img_path: Option<&str>) -> Option<Pixbuf> {
|
||||
let img_path = downloader::cache_image(pd_title, img_path);
|
||||
pub fn get_pixbuf_from_path(pd: &Podcast) -> Option<Pixbuf> {
|
||||
let img_path = downloader::cache_image(pd);
|
||||
if let Some(i) = img_path {
|
||||
Pixbuf::new_from_file_at_scale(&i, 256, 256, true).ok()
|
||||
} else {
|
||||
@ -130,12 +130,21 @@ pub fn update_podcast_widget(db: &Database, stack: >k::Stack, pd: &Podcast) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use hammond_data::models::NewPodcast;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_pixbuf_from_path() {
|
||||
let pxbuf =
|
||||
get_pixbuf_from_path("New Rustacean", Some("http://newrustacean.com/podcast.png"));
|
||||
let pd = NewPodcast {
|
||||
title: "New Rustacean".to_string(),
|
||||
description: "".to_string(),
|
||||
link: "".to_string(),
|
||||
image_uri: Some("http://newrustacean.com/podcast.png".to_string()),
|
||||
source_id: 0,
|
||||
};
|
||||
let pd = pd.into_podcast();
|
||||
|
||||
let pxbuf = get_pixbuf_from_path(&pd);
|
||||
assert!(pxbuf.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user