Skip to content

Commit

Permalink
refactor: use crate rss to parse XML. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
RinChanNOWWW authored Jun 7, 2023
1 parent d6d3b5d commit 9db0d84
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 262 deletions.
158 changes: 124 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ futures = "0.3.28"
log = "0.4"
pretty_env_logger = "0.4"
reqwest = { version = "0.11", features = ["json"] }
rss = { version = "2.0" }
serde = { version = "1.0.145", features = ["derive"] }
serfig = "0.0.2"
tokio = { version = "1.28.0", features = ["rt-multi-thread", "macros"] }
yaserde = "0.8.0"
yaserde_derive = "0.8.0"

[patch.crates-io]
rss = { git = "https://github.com/RinChanNOWWW/rss", branch = "patch-mikan" }
71 changes: 27 additions & 44 deletions src/source/byrbt/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,35 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use yaserde_derive::YaDeserialize;
use yaserde_derive::YaSerialize;
use std::io::BufRead;

#[derive(Debug, YaDeserialize, YaSerialize, Default)]
pub struct ByrbtRSSContent {
pub channel: Channel,
}
use chrono::DateTime;
use chrono::Local;

#[derive(Debug, YaDeserialize, YaSerialize, Default)]
pub struct Channel {
pub title: String,
pub link: String,
pub description: String,
pub language: String,
pub copyright: String,
#[yaserde(rename = "managingEditor")]
pub managing_editor: String,
#[yaserde(rename = "webMaster")]
pub web_master: String,
#[yaserde(rename = "pubDate")]
pub pub_date: String,
pub generator: String,
pub docs: String,
#[yaserde(rename = "item")]
pub items: Vec<ByrbtRSSItem>,
}
use crate::Item;
use crate::Result;

#[derive(Debug, YaDeserialize, YaSerialize, Default)]
pub struct Image {
pub url: String,
pub title: String,
pub link: String,
pub width: String,
pub height: String,
pub description: String,
}
pub struct Byrbt;

impl Byrbt {
pub fn parse_items<R: BufRead>(content: R) -> Result<Vec<Item>> {
let channel = rss::Channel::read_from(content)?;

#[derive(Debug, YaDeserialize, YaSerialize, Default)]
pub struct ByrbtRSSItem {
pub title: String,
pub link: String,
pub description: String,
pub author: String,
pub category: String,
pub comments: String,
pub guid: String,
#[yaserde(rename = "pubDate")]
pub pub_date: String,
Ok(channel
.items
.into_iter()
.map(|item| {
println!("{:?}", item);
let date = item.pub_date.unwrap();
let pub_date = DateTime::parse_from_rfc2822(&date)
.unwrap()
.with_timezone(&Local {});
Item {
title: item.title.unwrap(),
pub_date,
url: item.link.unwrap(),
}
})
.collect::<Vec<_>>())
}
}
14 changes: 4 additions & 10 deletions src/source/byrbt/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use std::sync::Arc;
use std::time::Duration;

use anyhow::Error;

use super::item::ByrbtRSSContent;
use super::Byrbt;
use crate::source::Item;
use crate::source::Source;
use crate::source::SourcePtr;
Expand Down Expand Up @@ -55,19 +53,15 @@ impl Source for ByrbtSource {
.map(|rss| {
let rss = rss.clone();
async move {
let content = reqwest::get(rss).await?.text().await?;
yaserde::de::from_str::<ByrbtRSSContent>(&content).map_err(Error::msg)
let content = reqwest::get(rss).await?.bytes().await?;
Byrbt::parse_items(&content[..])
}
})
.collect::<Vec<_>>();

let contents = futures::future::try_join_all(handles).await?;

let items = contents
.into_iter()
.flat_map(|content| content.channel.items)
.map(Item::from)
.collect::<Vec<_>>();
let items = contents.into_iter().flatten().collect::<Vec<_>>();

Ok(items)
}
Expand Down
Loading

0 comments on commit 9db0d84

Please sign in to comment.