hammond-downloader: Use glob Insead of hardcoded file extensions.

This commit is contained in:
Jordan Petridis 2017-12-31 03:59:48 +02:00
parent 785a5f80c6
commit a3cd76dbf8
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 33 additions and 28 deletions

15
Cargo.lock generated
View File

@ -410,7 +410,7 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -519,6 +519,11 @@ dependencies = [
"pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "gobject-sys" name = "gobject-sys"
version = "0.5.0" version = "0.5.0"
@ -602,6 +607,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"diesel 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"hammond-data 0.1.0", "hammond-data 0.1.0",
"hyper 0.11.10 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.11.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1010,7 +1016,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.7.0" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1188,7 +1194,7 @@ dependencies = [
"coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1795,6 +1801,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a303bbf7a5e75ab3b627117ff10e495d1b9e97e1d68966285ac2b1f6270091bc" "checksum gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a303bbf7a5e75ab3b627117ff10e495d1b9e97e1d68966285ac2b1f6270091bc"
"checksum glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "450247060df7d52fdad31e1d66f30d967e925c9d1d26a0ae050cfe33dcd00d08" "checksum glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "450247060df7d52fdad31e1d66f30d967e925c9d1d26a0ae050cfe33dcd00d08"
"checksum glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8" "checksum glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2" "checksum gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2"
"checksum gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0847c507e52c1feaede13ef56fb4847742438602655449d5f1f782e8633f146f" "checksum gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0847c507e52c1feaede13ef56fb4847742438602655449d5f1f782e8633f146f"
"checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f" "checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f"
@ -1839,7 +1846,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" "checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"
"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" "checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"
"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" "checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070"
"checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
"checksum open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c281318d992e4432cfa799969467003d05921582a7489a8325e37f8a450d5113" "checksum open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c281318d992e4432cfa799969467003d05921582a7489a8325e37f8a450d5113"
"checksum openssl 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "169a4b9160baf9b9b1ab975418c673686638995ba921683a7f1e01470dcb8854" "checksum openssl 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "169a4b9160baf9b9b1ab975418c673686638995ba921683a7f1e01470dcb8854"
"checksum openssl-sys 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "2200ffec628e3f14c39fc0131a301db214f1a7d584e36507ee8700b0c7fb7a46" "checksum openssl-sys 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "2200ffec628e3f14c39fc0131a301db214f1a7d584e36507ee8700b0c7fb7a46"

View File

@ -11,6 +11,7 @@ log = "0.3.8"
mime_guess = "1.8.3" mime_guess = "1.8.3"
reqwest = "0.8.2" reqwest = "0.8.2"
tempdir = "0.3.5" tempdir = "0.3.5"
glob = "0.2.11"
[dependencies.diesel] [dependencies.diesel]
features = ["sqlite"] features = ["sqlite"]

View File

@ -2,6 +2,7 @@ use reqwest;
use hyper::header::*; use hyper::header::*;
use tempdir::TempDir; use tempdir::TempDir;
use mime_guess; use mime_guess;
use glob::glob;
use std::fs::{rename, DirBuilder, File}; use std::fs::{rename, DirBuilder, File};
use std::io::{BufWriter, Read, Write}; use std::io::{BufWriter, Read, Write};
@ -43,7 +44,8 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result<String> {
info!("Extension: {}", ext); info!("Extension: {}", ext);
// Construct a temp file to save desired content. // Construct a temp file to save desired content.
let tempdir = TempDir::new_in(dir, "")?; // It has to be a `new_in` instead of new cause rename can't move cross filesystems.
let tempdir = TempDir::new_in(dir, "temp_download")?;
let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),); let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),);
@ -151,21 +153,13 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option<String> {
pd.title().to_owned() pd.title().to_owned()
); );
// Hacky way // Weird glob magic.
// TODO: make it so it returns the first cover.* file encountered. if let Ok(mut foo) = glob(&format!("{}/cover.*", download_fold)) {
// Use glob instead // For some reason there is no .first() method so nth(0) is used
let png = format!("{}/cover.png", download_fold); let path = foo.nth(0).and_then(|x| x.ok());
let jpg = format!("{}/cover.jpg", download_fold); if let Some(p) = path {
let jpe = format!("{}/cover.jpe", download_fold); return Some(p.to_str()?.into());
let jpeg = format!("{}/cover.jpeg", download_fold); }
if Path::new(&png).exists() {
return Some(png);
} else if Path::new(&jpe).exists() {
return Some(jpe);
} else if Path::new(&jpg).exists() {
return Some(jpg);
} else if Path::new(&jpeg).exists() {
return Some(jpeg);
}; };
DirBuilder::new() DirBuilder::new()
@ -173,14 +167,16 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option<String> {
.create(&download_fold) .create(&download_fold)
.unwrap(); .unwrap();
let dlpath = download_into(&download_fold, "cover", &url); match download_into(&download_fold, "cover", &url) {
if let Ok(path) = dlpath { Ok(path) => {
info!("Cached img into: {}", &path); info!("Cached img into: {}", &path);
Some(path) Some(path)
} else { }
error!("Failed to get feed image."); Err(err) => {
error!("Error: {}", dlpath.unwrap_err()); error!("Failed to get feed image.");
None error!("Error: {}", err);
None
}
} }
} }

View File

@ -3,6 +3,7 @@
extern crate diesel; extern crate diesel;
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
extern crate glob;
extern crate hammond_data; extern crate hammond_data;
extern crate hyper; extern crate hyper;
#[macro_use] #[macro_use]