fix: refactor config
This commit is contained in:
@@ -3,7 +3,7 @@ use std::ops::Deref;
|
||||
use loco_rs::app::{AppContext, Initializer};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
use super::{AppMikanConfig, MIKAN_BASE_URL};
|
||||
use super::AppMikanConfig;
|
||||
use crate::{config::AppConfigExt, fetch::HttpClient};
|
||||
|
||||
static APP_MIKAN_CLIENT: OnceCell<AppMikanClient> = OnceCell::new();
|
||||
@@ -14,12 +14,10 @@ pub struct AppMikanClient {
|
||||
}
|
||||
|
||||
impl AppMikanClient {
|
||||
pub fn new(mut config: AppMikanConfig) -> loco_rs::Result<Self> {
|
||||
pub fn new(config: AppMikanConfig) -> loco_rs::Result<Self> {
|
||||
let http_client =
|
||||
HttpClient::new(config.http_client.take()).map_err(loco_rs::Error::wrap)?;
|
||||
let base_url = config
|
||||
.base_url
|
||||
.unwrap_or_else(|| String::from(MIKAN_BASE_URL));
|
||||
HttpClient::from_config(config.http_client).map_err(loco_rs::Error::wrap)?;
|
||||
let base_url = config.base_url;
|
||||
Ok(Self {
|
||||
http_client,
|
||||
base_url,
|
||||
@@ -55,7 +53,7 @@ impl Initializer for AppMikanClientInitializer {
|
||||
|
||||
async fn before_run(&self, app_context: &AppContext) -> loco_rs::Result<()> {
|
||||
let config = &app_context.config;
|
||||
let app_mikan_conf = config.get_app_conf()?.mikan.unwrap_or_default();
|
||||
let app_mikan_conf = config.get_app_conf()?.mikan;
|
||||
|
||||
APP_MIKAN_CLIENT.get_or_try_init(|| AppMikanClient::new(app_mikan_conf))?;
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::fetch::HttpClientConfig;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AppMikanConfig {
|
||||
pub http_client: Option<HttpClientConfig>,
|
||||
pub base_url: Option<String>,
|
||||
pub http_client: HttpClientConfig,
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use chrono::DateTime;
|
||||
use dlsignal::core::BITTORRENT_MIME_TYPE;
|
||||
use itertools::Itertools;
|
||||
use reqwest::IntoUrl;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use torrent::core::BITTORRENT_MIME_TYPE;
|
||||
use url::Url;
|
||||
|
||||
use super::{
|
||||
web_parser::{parse_mikan_episode_id_from_homepage, MikanEpisodeHomepage},
|
||||
AppMikanClient,
|
||||
};
|
||||
use crate::{extract::errors::ParseError, fetch::bytes::download_bytes_with_client};
|
||||
use crate::{extract::errors::ParseError, fetch::bytes::fetch_bytes};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct MikanRssItem {
|
||||
@@ -228,7 +228,7 @@ pub async fn parse_mikan_rss_channel_from_rss_link(
|
||||
url: impl IntoUrl,
|
||||
) -> eyre::Result<MikanRssChannel> {
|
||||
let http_client = client.map(|s| s.deref());
|
||||
let bytes = download_bytes_with_client(http_client, url.as_str()).await?;
|
||||
let bytes = fetch_bytes(http_client, url.as_str()).await?;
|
||||
|
||||
let channel = rss::Channel::read_from(&bytes[..])?;
|
||||
|
||||
@@ -297,7 +297,7 @@ pub async fn parse_mikan_rss_channel_from_rss_link(
|
||||
mod tests {
|
||||
use std::assert_matches::assert_matches;
|
||||
|
||||
use torrent::core::BITTORRENT_MIME_TYPE;
|
||||
use dlsignal::core::BITTORRENT_MIME_TYPE;
|
||||
|
||||
use crate::extract::mikan::{
|
||||
parse_mikan_rss_channel_from_rss_link, MikanBangumiAggregationRssChannel,
|
||||
|
||||
@@ -18,7 +18,7 @@ use crate::{
|
||||
app::AppContextExt,
|
||||
dal::DalContentCategory,
|
||||
extract::html::parse_style_attr,
|
||||
fetch::{html::download_html_with_client, image::download_image_with_client},
|
||||
fetch::{html::fetch_html, image::fetch_image},
|
||||
models::subscribers,
|
||||
};
|
||||
|
||||
@@ -95,7 +95,7 @@ pub async fn parse_mikan_bangumi_poster_from_origin_poster_src(
|
||||
origin_poster_src: Url,
|
||||
) -> eyre::Result<MikanBangumiPosterMeta> {
|
||||
let http_client = client.map(|s| s.deref());
|
||||
let poster_data = download_image_with_client(http_client, origin_poster_src.clone()).await?;
|
||||
let poster_data = fetch_image(http_client, origin_poster_src.clone()).await?;
|
||||
Ok(MikanBangumiPosterMeta {
|
||||
origin_poster_src,
|
||||
poster_data: Some(poster_data),
|
||||
@@ -127,8 +127,7 @@ pub async fn parse_mikan_bangumi_poster_from_origin_poster_src_with_cache(
|
||||
});
|
||||
}
|
||||
|
||||
let poster_data =
|
||||
download_image_with_client(Some(mikan_client.deref()), origin_poster_src.clone()).await?;
|
||||
let poster_data = fetch_image(Some(mikan_client.deref()), origin_poster_src.clone()).await?;
|
||||
|
||||
let poster_str = dal_client
|
||||
.store_object(
|
||||
@@ -153,7 +152,7 @@ pub async fn parse_mikan_bangumi_meta_from_mikan_homepage(
|
||||
) -> eyre::Result<MikanBangumiMeta> {
|
||||
let http_client = client.map(|s| s.deref());
|
||||
let url_host = url.origin().unicode_serialization();
|
||||
let content = download_html_with_client(http_client, url.as_str()).await?;
|
||||
let content = fetch_html(http_client, url.as_str()).await?;
|
||||
let html = Html::parse_document(&content);
|
||||
|
||||
let bangumi_fansubs = html
|
||||
@@ -276,7 +275,7 @@ pub async fn parse_mikan_episode_meta_from_mikan_homepage(
|
||||
) -> eyre::Result<MikanEpisodeMeta> {
|
||||
let http_client = client.map(|s| s.deref());
|
||||
let url_host = url.origin().unicode_serialization();
|
||||
let content = download_html_with_client(http_client, url.as_str()).await?;
|
||||
let content = fetch_html(http_client, url.as_str()).await?;
|
||||
|
||||
let html = Html::parse_document(&content);
|
||||
|
||||
@@ -401,6 +400,8 @@ pub async fn parse_mikan_episode_meta_from_mikan_homepage(
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn parse_mikan_bangumis_from_user_home(_client: Option<&AppMikanClient>, _url: Url) {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::assert_matches::assert_matches;
|
||||
|
||||
Reference in New Issue
Block a user