aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariot Tsitoara <[email protected]>2021-01-02 10:31:21 +0100
committerMariot Tsitoara <[email protected]>2021-01-02 10:31:21 +0100
commit3a3e59a26a8233908f5ab9887501cbe93836a437 (patch)
tree1882ecf827f2c842b6e9ff0cc35cbd51bd5a5842
parentAdd lib examples to README (diff)
downloadchan-downloader-3a3e59a26a8233908f5ab9887501cbe93836a437.tar.xz
chan-downloader-3a3e59a26a8233908f5ab9887501cbe93836a437.zip
Use reqwest blocking client
-rw-r--r--Cargo.toml4
-rw-r--r--src/bin.rs10
-rw-r--r--src/cli.yml2
-rw-r--r--src/lib.rs34
4 files changed, 28 insertions, 22 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 9b55de8..d0c9ea7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "chan-downloader"
description = "CLI to download all images/webms of a 4chan thread"
-version = "0.1.4"
+version = "0.1.5"
authors = ["Mariot Tsitoara <[email protected]>"]
edition = "2018"
license = "MIT"
@@ -24,4 +24,4 @@ clap = {version = "2.33.3", features = ["yaml"]}
indicatif = "0.15.0"
lazy_static = "1.4.0"
regex = "1.4.2"
-reqwest = "0.9.19"
+reqwest = { version = "0.10", features = ["blocking"] }
diff --git a/src/bin.rs b/src/bin.rs
index da32445..8134d92 100644
--- a/src/bin.rs
+++ b/src/bin.rs
@@ -6,7 +6,8 @@ use std::fs::create_dir_all;
use clap::App;
use indicatif::{ProgressBar, ProgressStyle};
-use reqwest::Client;
+use reqwest::Error;
+use reqwest::blocking::Client;
use chan_downloader::{get_image_links, get_page_content, get_thread_infos, save_image};
@@ -16,11 +17,11 @@ fn main() {
let thread = matches.value_of("thread").unwrap();
let output = matches.value_of("output").unwrap_or("downloads");
- download_thread(thread, &output);
+ download_thread(thread, &output).unwrap();
}
-fn download_thread(thread_link: &str, output: &str) {
- let client = Client::new();
+fn download_thread(thread_link: &str, output: &str) -> Result<(), Error> {
+ let client = Client::builder().user_agent("reqwest").build()?;
let workpath = env::current_dir().unwrap();
let (board_name, thread_id) = get_thread_infos(thread_link);
@@ -60,4 +61,5 @@ fn download_thread(thread_link: &str, output: &str) {
}
Err(err) => eprintln!("Error: {}", err),
}
+ Ok(())
}
diff --git a/src/cli.yml b/src/cli.yml
index 1fb84d0..0b82bb8 100644
--- a/src/cli.yml
+++ b/src/cli.yml
@@ -1,5 +1,5 @@
name: chan-downloader
-version: "0.1.4"
+version: "0.1.5"
author: "Mariot Tsitoara <[email protected]>"
about: CLI to download all images/webms of a 4chan thread
args:
diff --git a/src/lib.rs b/src/lib.rs
index 6f7c312..f26af74 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,7 +12,8 @@ use std::fs::File;
use std::io::copy;
use regex::{CaptureMatches, Regex};
-use reqwest::{Client, Error};
+use reqwest::Error;
+use reqwest::blocking::{Client};
/// Saves the image from the url to the given path.
/// Returns the path on success
@@ -20,10 +21,10 @@ use reqwest::{Client, Error};
/// # Examples
///
/// ```
-/// use reqwest::Client;
+/// use reqwest::blocking::Client;
/// use std::env;
/// use std::fs::remove_file;
-/// let client = Client::new();
+/// let client = Client::builder().user_agent("reqwest").build().unwrap();
/// let workpath = env::current_dir().unwrap().join("1489266570954.jpg");
/// let url = "https://i.4cdn.org/wg/1489266570954.jpg";
/// let answer = chan_downloader::save_image(url, workpath.to_str().unwrap(), &client).unwrap();
@@ -32,11 +33,12 @@ use reqwest::{Client, Error};
/// remove_file(answer).unwrap();
/// ```
pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Error> {
- let mut response = client.get(url).send()?;
+ let response = client.get(url).send()?;
if response.status().is_success() {
let mut dest = File::create(path).unwrap();
- copy(&mut response, &mut dest).unwrap();
+ let content = response.text()?;
+ copy(&mut content.as_bytes(), &mut dest).unwrap();
}
Ok(String::from(path))
}
@@ -46,8 +48,8 @@ pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Erro
/// # Examples
///
/// ```
-/// use reqwest::Client;
-/// let client = Client::new();
+/// use reqwest::blocking::Client;
+/// let client = Client::builder().user_agent("reqwest").build().unwrap();
/// let url = "https://boards.4chan.org/wg/thread/6872254";
/// match chan_downloader::get_page_content(url, &client) {
/// Ok(page) => println!("Content: {}", page),
@@ -55,8 +57,9 @@ pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Erro
/// }
/// ```
pub fn get_page_content(url: &str, client: &Client) -> Result<String, Error> {
- let mut response = client.get(url).send()?;
- Ok(response.text().unwrap())
+ let response = client.get(url).send()?;
+ let content = response.text()?;
+ Ok(content)
}
/// Returns the board name and thread id.
@@ -84,8 +87,8 @@ pub fn get_thread_infos(url: &str) -> (&str, &str) {
/// # Examples
///
/// ```
-/// use reqwest::Client;
-/// let client = Client::new();
+/// use reqwest::blocking::Client;
+/// let client = Client::builder().user_agent("reqwest").build().unwrap();
/// let url = "https://boards.4chan.org/wg/thread/6872254";
/// match chan_downloader::get_page_content(url, &client) {
/// Ok(page_string) => {
@@ -141,19 +144,20 @@ mod tests {
#[test]
fn it_gets_page_content() {
- use reqwest::Client;
- let client = Client::new();
+ use reqwest::blocking::Client;
+ let client = Client::builder().user_agent("reqwest").build().unwrap();
let url = "https://raw.githubusercontent.com/mariot/chan-downloader/master/.gitignore";
let result = get_page_content(url, &client).unwrap();
assert_eq!(result, "/target/\nCargo.lock\n**/*.rs.bk\n");
+ assert_eq!(4, 2+2);
}
#[test]
fn it_saves_image() {
- use reqwest::Client;
+ use reqwest::blocking::Client;
use std::env;
use std::fs::remove_file;
- let client = Client::new();
+ let client = Client::builder().user_agent("reqwest").build().unwrap();
let workpath = env::current_dir().unwrap().join("1489266570954.jpg");
let url = "https://i.4cdn.org/wg/1489266570954.jpg";
let answer = save_image(url, workpath.to_str().unwrap(), &client).unwrap();