From f09cdc4f3bbc3dac4a50e18f60de479589df334f Mon Sep 17 00:00:00 2001 From: Sergio Ribera <56278796+SergioRibera@users.noreply.github.com> Date: Tue, 21 May 2024 00:19:15 -0400 Subject: [PATCH] fix: remove fetch to external blog --- src/main.rs | 21 +---------- src/utils/fetch_dev_to.rs | 73 ------------------------------------ src/utils/fetch_hashnode.rs | 74 ------------------------------------- src/utils/mod.rs | 3 -- 4 files changed, 1 insertion(+), 170 deletions(-) delete mode 100644 src/utils/fetch_dev_to.rs delete mode 100644 src/utils/fetch_hashnode.rs diff --git a/src/main.rs b/src/main.rs index 74b941d..785cef0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use pages::{ }; use ssg::Ssg; use tokio::sync::RwLock; -use utils::{fetch_dev_to::fetch_dev_to, fetch_hashnode::fetch_hashnode, generate_feed_rss}; +use utils::generate_feed_rss; use crate::pages::{article_page::ArticlePage, home::Homepage}; @@ -186,25 +186,6 @@ async fn list_articles() -> Result, Box> { let esta_semana_en_rust_folder = fs::read_dir("./esta_semana_en_rust")?; articles.append(&mut posts_from_folder(esta_semana_en_rust_folder)?); - if !cfg!(debug_assertions) { - let dev_to_articles = fetch_dev_to().await?; - let hashnode_articles = fetch_hashnode().await?; - - articles.append( - &mut dev_to_articles - .into_iter() - .map(Article::from) - .collect::>(), - ); - - articles.append( - &mut hashnode_articles - .into_iter() - .map(Article::from) - .collect::>(), - ); - } - articles.sort_by(|a, b| b.date.cmp(&a.date)); Ok(articles) diff --git a/src/utils/fetch_dev_to.rs b/src/utils/fetch_dev_to.rs deleted file mode 100644 index d58f53b..0000000 --- a/src/utils/fetch_dev_to.rs +++ /dev/null @@ -1,73 +0,0 @@ -use std::collections::HashMap; - -use reqwest::{ - header::{HeaderMap, HeaderValue}, - Client, -}; -use serde_json::value::Value; - -use crate::models::devto_article::DevToArticles; - -pub async fn fetch_dev_to() -> Result { - let url = "https://dev.to/api/articles?tag=rust,%20spanish"; - let mut headers = HeaderMap::new(); - headers.append("Accept", HeaderValue::from_static("application/json")); - - let client = Client::builder() - .user_agent("RustLangEs") - .default_headers(headers) - .build()?; - - let mut resp = client - .get(url) - .send() - .await? - .json::() - .await?; - - let url = "https://dev.to/api/articles?tag=rust,%20espaƱol"; - - resp.extend( - client - .get(url) - .send() - .await? - .json::() - .await?, - ); - - for article in &mut resp { - let article_complete = get_article_by_id(article.id, &client).await?; - let Value::String(content) = article_complete.get("body_markdown").unwrap() else { - continue; - }; - let Value::String(content_html) = article_complete.get("body_html").unwrap() else { - continue; - }; - article.tag_list = article - .tag_list - .clone() - .iter() - .map(|tag| tag.to_lowercase().replace(' ', "-")) - .collect(); - article.content = Some(content.to_string()); - article.content_html = Some(content_html.to_string()); - } - - Ok(resp) -} - -pub async fn get_article_by_id( - id: u32, - client: &Client, -) -> Result, reqwest::Error> { - let url = format!("https://dev.to/api/articles/{id}"); - let resp = client - .get(&url) - .send() - .await? - .json::>() - .await?; - - Ok(resp) -} diff --git a/src/utils/fetch_hashnode.rs b/src/utils/fetch_hashnode.rs deleted file mode 100644 index 8d80960..0000000 --- a/src/utils/fetch_hashnode.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::models::hashnode_article::{Data, HashNodeArticle, HashnodeResponse}; -use serde::Serialize; - -#[derive(Serialize)] -struct GraphQLQuery<'a> { - query: &'a str, -} - -pub async fn fetch_hashnode() -> Result, Box> { - let url = "https://gql.hashnode.com/"; - let client = reqwest::Client::new(); - - let graphql_query = r#" - { - publication(host: "miuler.com"){ - posts(first: 1, filter: { - tagSlugs: ["rust"] - }){ - edges{ - node { - slug - title - tags { - name - } - publishedAt - content { - markdown - } - brief - publication { - links { - hashnode - website - github - twitter - } - author { - username - } - } - } - } - } - } - } - "#; - let query = GraphQLQuery { - query: graphql_query, - }; - - let resp = client - .post(url) - .header("Accept", "application/json") - .header("User-Agent", "RustLangEs") - .json(&query) - .send() - .await? - .json::() - .await?; - - let Some(Data { publication }) = resp.data else { - return Err("No data found".into()); - }; - - let articles = publication - .posts - .edges - .iter() - .map(|post| post.node.clone()) - .collect::>(); - - Ok(articles) -} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f8e1df9..0a832fd 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,9 +4,6 @@ use rss::{validation::Validate, Category, ChannelBuilder, Item}; use crate::models::article::Article; -pub mod fetch_dev_to; -pub mod fetch_hashnode; - pub fn generate_feed_rss( articles: &[Article], out_file: &str,