Skip to content

Commit

Permalink
feat: limit once post articles count.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjiahan committed May 10, 2024
1 parent d0f688f commit 8355cca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cron_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn run_every_10_30pm(redis: &Redis, conf: &Conf) {
let _ = rt.block_on(go_weekly::send_feishu_msg(
redis,
go_weekly_conf.webhooks.clone(),
go_weekly_conf.once_post_limit,
));
},
));
Expand Down
18 changes: 16 additions & 2 deletions src/go_weekly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ impl fmt::Display for Article {
pub async fn send_feishu_msg(
redis: &redis_base::Redis,
webhooks: Vec<String>,
once_post_limit: u8,
) -> anyhow::Result<()> {
info!("start fetching go weekly blogs");
let (rss, articles) = get_rss_articles(Some(redis)).await?;
let (rss, articles) = get_rss_articles(Some(redis), once_post_limit).await?;
let client = reqwest::Client::new();
for wa in articles {
if wa.articles.is_empty() {
Expand Down Expand Up @@ -219,7 +220,12 @@ fn trim_str(str: &str) -> String {

async fn get_rss_articles(
redis: Option<&redis_base::Redis>,
mut once_post_limit: u8,
) -> anyhow::Result<(Rss, Vec<WeeklyArticle>)> {
const DEFAULT_ONCE_POST_LIMIT: u8 = 5;
if once_post_limit == 0 {
once_post_limit = DEFAULT_ONCE_POST_LIMIT
}
let data = send_request().await?;
let rss = resolve_xml_data(&data)?;
let mut articles = vec![];
Expand All @@ -233,6 +239,14 @@ async fn get_rss_articles(
true
}
})
.take_while(|_| {
if once_post_limit > 0 {
once_post_limit -= 1;
true
} else {
false
}
})
.collect();
let art_count = arts.len();
articles.push(WeeklyArticle {
Expand Down Expand Up @@ -264,7 +278,7 @@ mod tests {

#[tokio::test]
async fn test_get_rss_articles() -> anyhow::Result<()> {
let (rss, _) = get_rss_articles(None).await?;
let (rss, _) = get_rss_articles(None, 0).await?;
assert_eq!(rss.channel.title, "Golang Weekly".to_string());
assert_eq!(
rss.channel.description,
Expand Down

0 comments on commit 8355cca

Please sign in to comment.