Skip to content

Commit

Permalink
improvement: optimized create client
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglun committed Sep 21, 2023
1 parent c93e821 commit 5fb40d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
12 changes: 6 additions & 6 deletions src-tauri/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ impl Default for UserConfig {
threads: 1,
theme: String::from('1'),
update_interval: 0,
local_proxy: Some(LocalProxy {
ip: "".to_string(),
port: "".to_string(),
}),

local_proxy: None,
customize_style: CustomizeStyle::default(),
}
}
Expand All @@ -81,7 +77,11 @@ impl UserConfig {
}

fn update_proxy(&mut self, ip: String, port: String) -> &mut UserConfig {
self.local_proxy = Some(LocalProxy { ip, port });
if ip == "" && port == "" {
self.local_proxy = None;
} else {
self.local_proxy = Some(LocalProxy { ip, port });
}

self
}
Expand Down
29 changes: 21 additions & 8 deletions src-tauri/src/core/scraper.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use reqwest::{self};
use scraper::{self, Selector};
use serde::Serialize;
use tokio::sync::{Mutex};
use std::sync::Arc;
use tokio::sync::mpsc::{channel};
use tokio::sync::mpsc::channel;
use tokio::sync::Mutex;

use crate::feed;
use crate::cmd;
use crate::feed;

#[derive(Debug, Default, Serialize)]
pub struct PageScraper {
Expand Down Expand Up @@ -105,11 +105,13 @@ impl PageScraper {
println!("start fetch image : {:?}", &url);
let image_url = Self::get_first_image_or_og_image(&url).await;
println!("end fetch image, we get image: {:?}", image_url);
tx_clone.send((url.clone().to_string(), image_url)).await.unwrap();
tx_clone
.send((url.clone().to_string(), image_url))
.await
.unwrap();
let mut counter = counter_clone.lock().await;
*counter += 1;
});

}

let _counter_clone = counter.clone();
Expand All @@ -120,7 +122,6 @@ impl PageScraper {
println!("counter {:?} len {:?}", *counter, urls.len());

if *counter >= urls.len() {

print!("stop!!!!!!");
tokio::task::yield_now().await;
return image_urls;
Expand All @@ -135,7 +136,16 @@ impl PageScraper {
mod tests {
use uuid::Uuid;

use super::*;
use super::*;
#[tokio::test]
async fn test_get_best_image() {
let url = "https://post.smzdm.com/p/akkvmm3e/";
let res = PageScraper::get_first_image_or_og_image(url)
.await
.unwrap_or("".to_string());

println!("res ==> {:?} ", res);
}

#[tokio::test]
async fn test_from_str() {
Expand All @@ -152,7 +162,10 @@ use super::*;

println!("articles: {:?}", articles);

let urls = articles.into_iter().map(|a| a.link ).collect::<Vec<String>>();
let urls = articles
.into_iter()
.map(|a| a.link)
.collect::<Vec<String>>();
let res = PageScraper::get_first_images_or_og_images_async(urls, 5).await;

println!("res: {:?}", res);
Expand Down
35 changes: 18 additions & 17 deletions src-tauri/src/feed/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use feed_rs::parser;
use log::info;
use reqwest;

use crate::core::config;
Expand All @@ -9,26 +10,26 @@ pub mod folder;

pub fn create_client() -> reqwest::Client {
let user_config = config::get_user_config();
let client = match user_config {
Some(user_config) => match user_config.local_proxy {
Some(proxy) => {
let mut scheme = String::from("socks5h://");
let client_builder = reqwest::Client::builder();

scheme.push_str(&proxy.ip.to_string());
scheme.push_str(":");
scheme.push_str(&proxy.port.to_string());
if let Some(user_config) = user_config {
if let Some(proxy) = user_config.local_proxy {
info!("user_config.local_proxy {:?}", proxy);

reqwest::Client::builder()
.proxy(reqwest::Proxy::all(scheme).unwrap())
.build()
.unwrap()
}
None => reqwest::Client::builder().build().unwrap(),
},
None => reqwest::Client::builder().build().unwrap(),
};
let mut scheme = String::from("socks5h://");

scheme.push_str(&proxy.ip.to_string());
scheme.push_str(":");
scheme.push_str(&proxy.port.to_string());

return client_builder
.proxy(reqwest::Proxy::all(scheme).unwrap())
.build()
.unwrap();
}
}

client
client_builder.build().unwrap()
}

/// request feed, parse Feeds
Expand Down

0 comments on commit 5fb40d1

Please sign in to comment.