Cleanup of downloader module.

This commit is contained in:
Jordan Petridis 2017-11-09 17:47:38 +02:00
parent 3660fe0350
commit 07c1395c29
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
10 changed files with 159 additions and 173 deletions

76
Cargo.lock generated
View File

@ -25,11 +25,6 @@ name = "ansi_term"
version = "0.9.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "atk-sys" name = "atk-sys"
version = "0.4.0" version = "0.4.0"
@ -569,8 +564,10 @@ dependencies = [
"hyper 0.11.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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]] [[package]]
@ -608,24 +605,6 @@ name = "httparse"
version = "1.2.3" version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "hyper" name = "hyper"
version = "0.11.6" version = "0.11.6"
@ -649,16 +628,6 @@ dependencies = [
"unicase 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.1.2" version = "0.1.2"
@ -791,14 +760,6 @@ dependencies = [
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "mime" name = "mime"
version = "0.3.5" version = "0.3.5"
@ -1097,21 +1058,6 @@ 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)",
] ]
[[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]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.8.1" version = "0.8.1"
@ -1152,7 +1098,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"derive_builder 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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]] [[package]]
@ -1445,16 +1390,6 @@ dependencies = [
"tokio-io 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "unicase" name = "unicase"
version = "1.4.2" 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 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 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 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 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 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" "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 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 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 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 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 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 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" "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 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 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 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 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 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" "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 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 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 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 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 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" "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-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-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 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 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 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" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"

View File

@ -1,24 +1,30 @@
[package] [package]
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
name = "hammond-data" name = "hammond-data"
version = "0.1.0" version = "0.1.0"
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
workspace = "../" workspace = "../"
[dependencies] [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" rayon = "0.8"
reqwest = "0.8" reqwest = "0.8"
error-chain = "0.11" rfc822_sanitizer = "0.3"
log = "0.3"
diesel = { version = "0.16", features = ["sqlite"] }
diesel_codegen = { version = "0.16", features = ["sqlite"] }
xdg = "2.1"
lazy_static = "0.2"
rss = "1.1" rss = "1.1"
chrono = "0.4" xdg = "2.1"
# overwrite diesel dependancy that disables a feature rss depends upon
dotenv = "*" [dependencies.diesel]
features = ["sqlite"]
version = "0.16"
[dependencies.diesel_codegen]
features = ["sqlite"]
version = "0.16"
[dev-dependencies] [dev-dependencies]
tempdir = "0.3"
rand = "0.3" rand = "0.3"
tempdir = "0.3"

View File

@ -1,5 +1,6 @@
use reqwest; use reqwest;
use diesel::SaveChangesDsl; use diesel::SaveChangesDsl;
use diesel::result::QueryResult;
use reqwest::header::{ETag, LastModified}; use reqwest::header::{ETag, LastModified};
use schema::{episode, podcast, source}; use schema::{episode, podcast, source};
@ -105,10 +106,9 @@ impl Episode {
self.played = value; self.played = value;
} }
pub fn save(&self, db: &Database) -> Result<()> { pub fn save(&self, db: &Database) -> QueryResult<Episode> {
let tempdb = db.lock().unwrap(); let tempdb = db.lock().unwrap();
self.save_changes::<Episode>(&*tempdb)?; self.save_changes::<Episode>(&*tempdb)
Ok(())
} }
} }
@ -163,10 +163,9 @@ impl Podcast {
self.image_uri = value.map(|x| x.to_string()); 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(); let tempdb = db.lock().unwrap();
self.save_changes::<Podcast>(&*tempdb)?; self.save_changes::<Podcast>(&*tempdb)
Ok(())
} }
} }
@ -227,10 +226,9 @@ impl<'a> Source {
Ok(()) Ok(())
} }
pub fn save(&self, db: &Database) -> Result<()> { pub fn save(&self, db: &Database) -> QueryResult<Source> {
let tempdb = db.lock().unwrap(); let tempdb = db.lock().unwrap();
self.save_changes::<Source>(&*tempdb)?; self.save_changes::<Source>(&*tempdb)
Ok(())
} }
} }
@ -277,3 +275,16 @@ pub struct NewPodcast {
pub image_uri: Option<String>, pub image_uri: Option<String>,
pub source_id: i32, 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,
}
}
}

View File

@ -1,16 +1,23 @@
[package] [package]
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
name = "hammond-downloader" name = "hammond-downloader"
version = "0.1.0" version = "0.1.0"
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
workspace = "../" workspace = "../"
[dependencies] [dependencies]
hammond-data = {path = "../hammond-data"} dotenv = "*"
error-chain = "0.11" error-chain = "0.11"
hyper = "0.11"
log = "0.3" log = "0.3"
mime = "0.3" mime = "0.3"
rand = "0.3"
reqwest = "0.8" reqwest = "0.8"
hyper = "0.11" rss = "1.1"
diesel = { version = "0.16", features = ["sqlite"] } tempdir = "0.3"
rss = { version = "1.1", features = ["from_url"]}
dotenv = "*" [dependencies.diesel]
features = ["sqlite"]
version = "0.16"
[dependencies.hammond-data]
path = "../hammond-data"

View File

@ -1,5 +1,8 @@
use reqwest; use reqwest;
use hyper::header::*; use hyper::header::*;
use tempdir::TempDir;
use rand;
use rand::Rng;
// use mime::Mime; // use mime::Mime;
use std::fs::{rename, DirBuilder, File}; use std::fs::{rename, DirBuilder, File};
@ -9,7 +12,7 @@ use std::path::Path;
use errors::*; use errors::*;
use hammond_data::index_feed::Database; use hammond_data::index_feed::Database;
use hammond_data::models::Episode; use hammond_data::models::{Episode, Podcast};
use hammond_data::{DL_DIR, HAMMOND_CACHE}; use hammond_data::{DL_DIR, HAMMOND_CACHE};
// Adapted from https://github.com/mattgathu/rget . // Adapted from https://github.com/mattgathu/rget .
@ -18,7 +21,7 @@ use hammond_data::{DL_DIR, HAMMOND_CACHE};
// Would much rather use a crate, // Would much rather use a crate,
// or bindings for a lib like youtube-dl(python), // or bindings for a lib like youtube-dl(python),
// But cant seem to find one. // 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); info!("GET request to: {}", url);
let client = reqwest::Client::builder().referer(false).build()?; let client = reqwest::Client::builder().referer(false).build()?;
let mut resp = client.get(url).send()?; 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_len.map(|x| info!("File Lenght: {}", x));
ct_type.map(|x| info!("Content Type: {}", x)); ct_type.map(|x| info!("Content Type: {}", x));
let target = format!("{}/{}", dir, filename); // FIXME: Unreliable and hacky way to extract the file extension from the url.
// let target = format!("{}{}",dir, filename, ext); // https://gitlab.gnome.org/alatiera/Hammond/issues/5
return save_io(&target, &mut resp, ct_len); 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("")) // Ok(String::from(""))
panic!("foo"); panic!("foo");
} }
fn save_io( fn save_io(
target: &str, target_dir: &str,
filename: &str,
resp: &mut reqwest::Response, resp: &mut reqwest::Response,
content_lenght: Option<u64>, content_lenght: Option<u64>,
) -> Result<String> { ) -> Result<String> {
info!("Downloading into: {}", target); info!("Downloading into: {}", target_dir);
let chunk_size = match content_lenght { let chunk_size = match content_lenght {
Some(x) => x as usize / 99, Some(x) => x as usize / 99,
None => 1024 as usize, // default chunk size 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)?); let mut writer = BufWriter::new(File::create(&out_file)?);
loop { loop {
@ -64,9 +80,11 @@ fn save_io(
break; break;
} }
} }
rename(out_file, target)?;
info!("Downloading of {} completed succesfully.", target); let target = format!("{}/{}", target_dir, filename);
Ok(target.to_string()) rename(out_file, &target)?;
info!("Downloading of {} completed succesfully.", &target);
Ok(target)
} }
pub fn get_download_folder(pd_title: &str) -> Result<String> { 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)?; ep.save(connection)?;
}; };
// FIXME: Unreliable and hacky way to extract the file extension from the url. let res = download_to(download_folder, ep.title().unwrap(), ep.uri());
// https://gitlab.gnome.org/alatiera/Hammond/issues/5
let ext = ep.uri().split('.').last().unwrap().to_owned();
// Construct the download path. if let Ok(path) = res {
// 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 download succedes set episode local_uri to dlpath. // If download succedes set episode local_uri to dlpath.
let dlpath = res.unwrap(); ep.set_local_uri(Some(&path));
ep.set_local_uri(Some(&dlpath));
ep.save(connection)?; ep.save(connection)?;
Ok(()) Ok(())
} else { } 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> { pub fn cache_image(pd: &Podcast) -> Option<String> {
// TODO: Right unit test if pd.image_uri().is_some() {
// TODO: Refactor let url = pd.image_uri().unwrap().to_owned();
pub fn cache_image(title: &str, image_uri: Option<&str>) -> Option<String> {
if let Some(url) = image_uri {
if url == "" { if url == "" {
return None; return None;
} }
// FIXME: https://gitlab.gnome.org/alatiera/Hammond/issues/5 let download_fold = format!(
let ext = url.split('.').last().unwrap(); "{}{}",
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() DirBuilder::new()
.recursive(true) .recursive(true)
.create(&download_fold) .create(&download_fold)
.unwrap(); .unwrap();
let file_name = format!("cover.{}", ext);
// This will need rework once the #5 is completed. let dlpath = download_to(&download_fold, "cover", &url);
let dlpath = format!("{}/{}", download_fold, file_name); if let Ok(path) = dlpath {
info!("Cached img into: {}", &path);
if Path::new(&dlpath).exists() { return Some(path);
return Some(dlpath); } else {
}
if let Err(err) = download_to(&download_fold, &file_name, url) {
error!("Failed to get feed image."); error!("Failed to get feed image.");
error!("Error: {}", err); error!("Error: {}", dlpath.unwrap_err());
return None; return None;
}; };
info!("Cached img into: {}", dlpath);
return Some(dlpath);
} }
None None
} }
@ -155,6 +169,7 @@ pub fn cache_image(title: &str, image_uri: Option<&str>) -> Option<String> {
mod tests { mod tests {
use super::*; use super::*;
use hammond_data::{DL_DIR, HAMMOND_CACHE}; use hammond_data::{DL_DIR, HAMMOND_CACHE};
use hammond_data::models::NewPodcast;
#[test] #[test]
fn test_get_dl_folder() { fn test_get_dl_folder() {
@ -164,13 +179,20 @@ mod tests {
#[test] #[test]
fn test_cache_image() { fn test_cache_image() {
let img_path = let pd = NewPodcast {
cache_image("New Rustacean", Some("http://newrustacean.com/podcast.png")).unwrap(); 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!( let foo_ = format!(
"{}{}/cover.png", "{}{}/cover.png",
HAMMOND_CACHE.to_str().unwrap(), HAMMOND_CACHE.to_str().unwrap(),
"New Rustacean" "New Rustacean"
); );
assert_eq!(img_path, foo_); assert_eq!(img_path, Some(foo_));
} }
} }

View File

@ -1,15 +1,11 @@
use diesel::result; use diesel::result;
use hammond_data;
use reqwest; use reqwest;
use rss;
use std::io; use std::io;
error_chain! { error_chain! {
foreign_links { foreign_links {
ReqError(reqwest::Error); ReqError(reqwest::Error);
IoError(io::Error); IoError(io::Error);
RSSError(rss::Error);
DieselResultError(result::Error); DieselResultError(result::Error);
HamDBError(hammond_data::errors::Error);
} }
} }

View File

@ -8,8 +8,10 @@ extern crate hyper;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate mime; extern crate mime;
extern crate rand;
extern crate reqwest; extern crate reqwest;
extern crate rss; // extern crate rss;
extern crate tempdir;
pub mod downloader; pub mod downloader;
pub mod errors; pub mod errors;

View File

@ -1,22 +1,27 @@
[package] [package]
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
build = "build.rs"
name = "hammond-gtk" name = "hammond-gtk"
version = "0.1.0" version = "0.1.0"
authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
workspace = "../" workspace = "../"
build = "build.rs"
[dependencies] [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" 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" rayon = "0.8"
hammond-data = {path = "../hammond-data"} [dependencies.gtk]
hammond-downloader = {path = "../hammond-downloader"} features = ["v3_22"]
version = "0.2"
[dependencies.hammond-data]
path = "../hammond-data"
[dependencies.hammond-downloader]
path = "../hammond-downloader"

View File

@ -46,7 +46,7 @@ fn create_flowbox_child(db: &Database, pd: &Podcast) -> gtk::FlowBoxChild {
pd_title.set_text(pd.title()); 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 { if let Some(img) = cover {
pd_cover.set_from_pixbuf(&img); pd_cover.set_from_pixbuf(&img);
}; };

View File

@ -40,7 +40,7 @@ pub fn podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) -> gtk::B
buff.set_text(pd.description()); 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 { if let Some(i) = img {
cover.set_from_pixbuf(&i); cover.set_from_pixbuf(&i);
} }
@ -101,8 +101,8 @@ fn show_played_button(db: &Database, pd: &Podcast, played_button: &gtk::Button)
} }
} }
pub fn get_pixbuf_from_path(pd_title: &str, img_path: Option<&str>) -> Option<Pixbuf> { pub fn get_pixbuf_from_path(pd: &Podcast) -> Option<Pixbuf> {
let img_path = downloader::cache_image(pd_title, img_path); let img_path = downloader::cache_image(pd);
if let Some(i) = img_path { if let Some(i) = img_path {
Pixbuf::new_from_file_at_scale(&i, 256, 256, true).ok() Pixbuf::new_from_file_at_scale(&i, 256, 256, true).ok()
} else { } else {
@ -130,12 +130,21 @@ pub fn update_podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hammond_data::models::NewPodcast;
use super::*; use super::*;
#[test] #[test]
fn test_get_pixbuf_from_path() { fn test_get_pixbuf_from_path() {
let pxbuf = let pd = NewPodcast {
get_pixbuf_from_path("New Rustacean", Some("http://newrustacean.com/podcast.png")); 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()); assert!(pxbuf.is_some());
} }
} }