fetch_feed method
This commit is contained in:
parent
3fbd14191a
commit
a3c8a4adc0
48
src/feeds.rs
48
src/feeds.rs
@ -43,6 +43,30 @@ pub struct NewFeed {
|
|||||||
pub categorization: Vec<String>,
|
pub categorization: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct FeedError;
|
||||||
|
|
||||||
|
async fn fetch_feed(url: &Url) -> Result<feed_rs::model::Feed, FeedError> {
|
||||||
|
// Fetch the feed content
|
||||||
|
let response = reqwest::get(url.as_ref()).await.map_err(|e| {
|
||||||
|
eprintln!("Failed to fetch feed: {}", e);
|
||||||
|
FeedError
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let content = response.text().await.map_err(|e| {
|
||||||
|
eprintln!("Failed to read response body: {}", e);
|
||||||
|
FeedError
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// Parse the feed
|
||||||
|
let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| {
|
||||||
|
eprintln!("Failed to parse feed content: {}", e);
|
||||||
|
FeedError
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(feed_data)
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/feeds", data = "<new_feed>")]
|
#[post("/feeds", data = "<new_feed>")]
|
||||||
pub async fn create_feed(
|
pub async fn create_feed(
|
||||||
mut db: Connection<Db>,
|
mut db: Connection<Db>,
|
||||||
@ -56,28 +80,15 @@ pub async fn create_feed(
|
|||||||
return Err(Status::UnprocessableEntity);
|
return Err(Status::UnprocessableEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the feed content
|
let feed_data = fetch_feed(&new_feed.url)
|
||||||
let response = reqwest::get(new_feed.url.as_ref()).await.map_err(|e| {
|
.await
|
||||||
eprintln!("Failed to fetch feed: {}", e);
|
.map_err(|_| Status::UnprocessableEntity)?;
|
||||||
Status::UnprocessableEntity
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let content = response.text().await.map_err(|e| {
|
|
||||||
eprintln!("Failed to read response body: {}", e);
|
|
||||||
Status::UnprocessableEntity
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Parse the feed
|
|
||||||
let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| {
|
|
||||||
eprintln!("Failed to parse feed content: {}", e);
|
|
||||||
Status::UnprocessableEntity
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Use the feed title as the name, or URL if no title is available
|
// Use the feed title as the name, or URL if no title is available
|
||||||
let name = feed_data
|
let name = feed_data
|
||||||
.title
|
.title
|
||||||
.map(|t| t.content)
|
.map(|t| t.content)
|
||||||
.unwrap_or_else(|| new_feed.url.host_str().unwrap_or("Unknown").to_string());
|
.unwrap_or_else(|| new_feed.url.host_str().unwrap_or("<Unknown>").to_string());
|
||||||
|
|
||||||
let mut feed = Feed::new(name, new_feed.url, user.user_id);
|
let mut feed = Feed::new(name, new_feed.url, user.user_id);
|
||||||
feed.categorization = new_feed.categorization;
|
feed.categorization = new_feed.categorization;
|
||||||
@ -156,7 +167,8 @@ pub async fn list_feeds(
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Parse categorization from JSON value
|
// Parse categorization from JSON value
|
||||||
let categorization: Vec<String> = serde::json::from_value(row.categorization).map_err(|e| {
|
let categorization: Vec<String> =
|
||||||
|
serde::json::from_value(row.categorization).map_err(|e| {
|
||||||
eprintln!("Failed to parse categorization: {}", e);
|
eprintln!("Failed to parse categorization: {}", e);
|
||||||
Status::InternalServerError
|
Status::InternalServerError
|
||||||
})?;
|
})?;
|
||||||
|
Loading…
Reference in New Issue
Block a user