Fetching data with channels
This commit is contained in:
parent
f42e558db9
commit
22871f5789
3 changed files with 225 additions and 37 deletions
|
|
@ -1,9 +1,12 @@
|
|||
use std::error::Error;
|
||||
use reqwest::{
|
||||
IntoUrl,
|
||||
Client,
|
||||
use std::{
|
||||
error::Error,
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
use reqwest::Url;
|
||||
use chrono::{
|
||||
Utc,
|
||||
DateTime,
|
||||
};
|
||||
use rss::Channel as RawChannel;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
||||
|
||||
|
|
@ -26,33 +29,42 @@ impl AdapterOptions {
|
|||
pub async fn create(self) -> Result<Adapter> {
|
||||
let db = sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.connect(&self.database_url).await?;
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
Ok(Adapter { db })
|
||||
Ok(Adapter { db, client })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Adapter {
|
||||
db: sqlx::SqlitePool,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl Adapter {
|
||||
pub async fn get_all_users(&self) -> Result<Vec<User>> {
|
||||
let users_query = sqlx::query!("SELECT id, name FROM users")
|
||||
.fetch_all(&self.db).await?;
|
||||
let users = sqlx::query_as!(
|
||||
User,
|
||||
"SELECT id, name FROM users"
|
||||
).fetch_all(&self.db).await?;
|
||||
|
||||
let mut all_users: Vec<User> = Vec::with_capacity(users_query.len());
|
||||
|
||||
for user in users_query {
|
||||
all_users.push(User {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(all_users)
|
||||
Ok(users)
|
||||
}
|
||||
|
||||
// pub async fn update_channels(&self) -> Result<()> {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// async fn get_all_channels(&self) -> Result<Vec<impl Channel>> {
|
||||
// let users = sqlx::query_as!(
|
||||
// Channel,
|
||||
// "SELECT id FROM channels"
|
||||
// ).fetch_all(&self.db).await?;
|
||||
//
|
||||
// Ok(users)
|
||||
// }
|
||||
|
||||
fn get_pool(&self) -> &sqlx::SqlitePool { &self.db }
|
||||
fn get_client(&self) -> &reqwest::client { &self.client }
|
||||
}
|
||||
|
||||
pub struct User {
|
||||
|
|
@ -61,15 +73,15 @@ pub struct User {
|
|||
}
|
||||
|
||||
impl User {
|
||||
pub async fn get_by_id(adapter: &Adapter, id: i64) -> Result<Self> {
|
||||
let user = sqlx::query!("SELECT name FROM users WHERE id = ?", id)
|
||||
.fetch_one(adapter.get_pool()).await?;
|
||||
|
||||
Ok(Self {
|
||||
id: id,
|
||||
name: user.name,
|
||||
})
|
||||
}
|
||||
// async fn get_by_id(adapter: &Adapter, id: i64) -> Result<Self> {
|
||||
// let user = sqlx::query!("SELECT name FROM users WHERE id = ?", id)
|
||||
// .fetch_one(adapter.get_pool()).await?;
|
||||
//
|
||||
// Ok(Self {
|
||||
// id: id,
|
||||
// name: user.name,
|
||||
// })
|
||||
// }
|
||||
|
||||
pub async fn create(adapter: &Adapter, name: &str) -> Result<Self> {
|
||||
let result = sqlx::query!("INSERT INTO users (name) VALUES (?)", name)
|
||||
|
|
@ -93,21 +105,123 @@ impl User {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_feeds(&self, adapter: &Adapter) -> Result<Vec<Feed>> {
|
||||
let feeds = sqlx::query_as!(
|
||||
Feed,
|
||||
"SELECT id FROM feeds WHERE user_id = ?",
|
||||
self.id
|
||||
).fetch_all(adapter.get_pool()).await?;
|
||||
|
||||
Ok(feeds)
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str { &self.name }
|
||||
pub fn id(&self) -> i64 { self.id }
|
||||
}
|
||||
|
||||
pub struct Feed {
|
||||
id: i64,
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub async fn get_items(
|
||||
&self, adapter: &Adapter, limit: u8, offset: u32) -> Result<Vec<Item>> {
|
||||
let items = sqlx::query_as!(
|
||||
Item,
|
||||
"SELECT item_id as id FROM feed_items
|
||||
WHERE feed_id = ? AND archived = FALSE
|
||||
ORDER BY score DESC
|
||||
LIMIT ? OFFSET ?",
|
||||
self.id, limit, offset
|
||||
).fetch_all(adapter.get_pool()).await?;
|
||||
|
||||
Ok(items)
|
||||
}
|
||||
|
||||
pub async fn get_channels(&self, adapter: &Adapter) -> Result<Vec<Channel>> {
|
||||
let db_channels = sqlx::query!(
|
||||
"SELECT c.id as `id!`, c.title, c.link, c.description, c.last_fetched
|
||||
FROM channels c
|
||||
JOIN feed_channels fc on c.id = fc.channel_id
|
||||
WHERE fc.feed_id = ?",
|
||||
self.id
|
||||
).fetch_all(adapter.get_pool()).await?;
|
||||
let mut channels = Vec::with_capacity(db_channels.len());
|
||||
for db_channel in db_channels {
|
||||
channels.push(Channel {
|
||||
id: db_channel.id,
|
||||
title: db_channel.title,
|
||||
link: Url::parse(&db_channel.link)?,
|
||||
description: db_channel.description,
|
||||
last_fetched: db_channel.last_fetched.as_deref()
|
||||
.map(DateTime::parse_from_rfc2822)
|
||||
.transpose()?
|
||||
.map(|dt| dt.with_timezone(&Utc)),
|
||||
})
|
||||
}
|
||||
Ok(channels)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Channel {
|
||||
pub channel: rss::Channel,
|
||||
id: i64,
|
||||
title: String,
|
||||
link: Url,
|
||||
description: Option<String>,
|
||||
last_fetched: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
pub async fn fetch_channel<T: IntoUrl>(
|
||||
client: &Client, url: T) -> Result<Channel> {
|
||||
let content = client.get(url)
|
||||
.send().await?
|
||||
.bytes().await?;
|
||||
impl Channel {
|
||||
pub async fn fetch(mut self, adapter: &Adapter) -> Result<Self> {
|
||||
let bytestream = adapter.get_client().get(self.link.clone())
|
||||
.send().await?
|
||||
.bytes().await?;
|
||||
|
||||
let raw_channel = RawChannel::read_from(&content[..])?;
|
||||
println!("{}", raw_channel.title);
|
||||
Ok(Channel { channel: raw_channel })
|
||||
let rss_channel = rss::Channel::read_from(&bytestream[..])?;
|
||||
self.title = rss_channel.title;
|
||||
self.link = Url::parse(&rss_channel.link)?;
|
||||
self.description = Some(rss_channel.description);
|
||||
let now = Utc::now();
|
||||
self.last_fetched = Some(now);
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE channels
|
||||
SET title = ?, link = ?, description = ?,
|
||||
last_fetched = ?
|
||||
WHERE id = ?",
|
||||
self.title, self.link.as_str(), self.description, now.to_rfc2822(),
|
||||
self.id
|
||||
).execute(adapter.get_pool()).await?;
|
||||
|
||||
fn get_or_create_guid(item: &rss::Item) -> String {
|
||||
if let Some(guid) = item.guid() {
|
||||
return guid.value().to_string();
|
||||
}
|
||||
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
item.link().unwrap_or("").hash(&mut hasher);
|
||||
item.title().unwrap_or("").hash(&mut hasher);
|
||||
item.description().unwrap_or("").hash(&mut hasher);
|
||||
|
||||
format!("gen-{:x}", hasher.finish())
|
||||
}
|
||||
|
||||
for item in rss_channel.items {
|
||||
sqlx::query!(
|
||||
"INSERT OR IGNORE INTO items
|
||||
(channel_id, guid, fetched_at, title, description, content)
|
||||
VALUES (?, ?, ?, ?, ?, ?)",
|
||||
self.id, get_or_create_guid(&item), now.to_rfc2822(),
|
||||
item.title().unwrap_or(""), item.description().unwrap_or(""),
|
||||
item.content().unwrap_or("")
|
||||
)
|
||||
.execute(adapter.get_pool())
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Item {
|
||||
id: i64,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue