2025-05-29 12:07:36 -04:00
|
|
|
use std::path::PathBuf;
|
2025-07-02 20:11:43 -04:00
|
|
|
use std::str::FromStr;
|
2025-05-29 12:07:36 -04:00
|
|
|
|
|
|
|
use crate::files;
|
2025-07-02 20:11:43 -04:00
|
|
|
use rusqlite::{params, Connection, Result};
|
2025-05-29 12:07:36 -04:00
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
use super::files::*;
|
|
|
|
use super::net::*;
|
|
|
|
use chrono::Utc; //Maybe use a different time?
|
2025-01-10 07:32:12 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Cache is the in-memory database
|
|
|
|
Any changes/updates are written to file database
|
|
|
|
*/
|
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
struct Item {
|
2025-01-10 07:32:12 -05:00
|
|
|
title: String,
|
2025-07-02 20:11:43 -04:00
|
|
|
content: Option<String>,
|
2025-01-10 07:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const DB_LOCATION: &str = "rsscar.db";
|
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
fn get_db_path() -> PathBuf {
|
2025-05-29 12:07:36 -04:00
|
|
|
get_data_directory().join(DB_LOCATION)
|
|
|
|
}
|
2025-01-10 07:32:12 -05:00
|
|
|
|
|
|
|
const feeds_table_create: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
|
|
|
|
'feedID' INTEGER NOT NULL,
|
|
|
|
'title' TEXT NOT NULL,
|
|
|
|
'description' TEXT,
|
|
|
|
'icon' BLOB,
|
2025-05-29 12:07:36 -04:00
|
|
|
'url' text not null unique on conflict replace,
|
2025-01-10 07:32:12 -05:00
|
|
|
'subscribed' INTEGER,
|
|
|
|
'last_updated' TEXT ,
|
|
|
|
PRIMARY KEY('feedID')
|
|
|
|
);";
|
2025-07-02 20:11:43 -04:00
|
|
|
|
2025-01-10 07:32:12 -05:00
|
|
|
const feeds_index_create: &str = "CREATE INDEX IF NOT EXISTS 'subscribed_feeds_idx' ON 'feeds' (
|
|
|
|
'feedID' ASC
|
|
|
|
) WHERE 'subscribed' = 1;";
|
2025-05-28 14:53:05 -04:00
|
|
|
|
|
|
|
/* */
|
2025-01-10 07:32:12 -05:00
|
|
|
const items_table_create: &str = "CREATE TABLE IF NOT EXISTS 'items' (
|
|
|
|
'itemID' INTEGER NOT NULL,
|
|
|
|
'title' TEXT NOT NULL,
|
|
|
|
'icon' BLOB,
|
2025-05-29 12:07:36 -04:00
|
|
|
'url' text not null unique on conflict replace,
|
2025-01-10 07:32:12 -05:00
|
|
|
'description' TEXT,
|
|
|
|
'content' TEXT,
|
|
|
|
'read' INTEGER DEFAULT 0,
|
|
|
|
PRIMARY KEY('itemID')
|
|
|
|
);";
|
|
|
|
const items_index_create: &str = "CREATE INDEX IF NOT EXISTS 'items_idx' on 'items'('itemID' ASC);";
|
|
|
|
|
|
|
|
pub fn initialize() {
|
2025-07-02 20:11:43 -04:00
|
|
|
let path = get_db_path();
|
|
|
|
println!("Database at {} initialized", path.as_os_str().display());
|
|
|
|
let conn = Connection::open(path).unwrap();
|
|
|
|
conn.execute(feeds_table_create, []).unwrap();
|
|
|
|
conn.execute(feeds_index_create, []).unwrap();
|
|
|
|
conn.execute(items_table_create, []).unwrap();
|
|
|
|
conn.execute(items_index_create, []).unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
conn.close();
|
|
|
|
println!("Database Initialized.")
|
|
|
|
}
|
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
pub fn add_feed(url: &str) {
|
2025-05-29 12:07:36 -04:00
|
|
|
let conn = Connection::open(get_db_path()).unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
let feed = load_rss(url).unwrap();
|
|
|
|
let new_feed = feed.clone();
|
2025-07-02 20:11:43 -04:00
|
|
|
let time = Utc::now().to_rfc2822();
|
|
|
|
conn.execute(
|
|
|
|
"insert into feeds(title,url,description,last_updated) values(?1,?2,?3,?4)",
|
|
|
|
[feed.title, url.to_owned(), feed.description, time],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
conn.close();
|
|
|
|
store_items(new_feed);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store_items(feed: rss::Channel) {
|
2025-05-29 12:07:36 -04:00
|
|
|
let conn = Connection::open(get_db_path()).unwrap();
|
2025-07-02 20:11:43 -04:00
|
|
|
feed.items.iter().for_each(|i: &rss::Item| {
|
|
|
|
conn.execute(
|
|
|
|
"insert into items(url,title,description,content) values(?1,?2,?3,?4)",
|
|
|
|
[
|
|
|
|
i.link.clone(),
|
|
|
|
i.title.clone(),
|
|
|
|
i.description.clone(),
|
|
|
|
i.content.clone(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
});
|
2025-01-10 07:32:12 -05:00
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
pub fn return_item() -> String {
|
2025-05-29 12:07:36 -04:00
|
|
|
let conn = Connection::open(get_db_path()).unwrap();
|
2025-07-02 20:11:43 -04:00
|
|
|
let item = conn
|
|
|
|
.query_row(
|
|
|
|
"select title,content from items where rowid=?1",
|
|
|
|
[],
|
|
|
|
|row| {
|
|
|
|
Ok(Item {
|
|
|
|
title: row.get(0).unwrap(),
|
|
|
|
content: row.get(1).unwrap(),
|
|
|
|
})
|
|
|
|
},
|
2025-01-10 07:32:12 -05:00
|
|
|
)
|
2025-07-02 20:11:43 -04:00
|
|
|
.unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
match item.content {
|
|
|
|
Some(content) => content,
|
2025-07-02 20:11:43 -04:00
|
|
|
None => panic!(),
|
2025-01-10 07:32:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-02 20:11:43 -04:00
|
|
|
pub struct Feed {
|
|
|
|
feedID: u8,
|
|
|
|
title: String,
|
|
|
|
description: Option<String>,
|
|
|
|
icon: Option<String>,
|
|
|
|
url: String,
|
|
|
|
subscribed: Option<String>, //needs to be bool
|
|
|
|
last_updated: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_feeds() -> Vec<Feed> {
|
|
|
|
let conn = Connection::open(get_db_path()).unwrap();
|
|
|
|
let mut stmt = conn.prepare("select feedID,title,description,icon,url,subscribed,last_updated from feeds").unwrap();
|
|
|
|
let rows: Result<Vec<Feed>> = stmt
|
|
|
|
.query_map([], |row| {
|
|
|
|
Ok(Feed {
|
|
|
|
feedID: row.get(0).unwrap(),
|
|
|
|
title: row.get(1).unwrap(),
|
|
|
|
description: row.get(2).unwrap(),
|
|
|
|
icon: row.get(3).unwrap(),
|
|
|
|
url: row.get(4).unwrap(),
|
|
|
|
subscribed: row.get(5).unwrap(),
|
|
|
|
last_updated: row.get(6).unwrap(),
|
|
|
|
})
|
|
|
|
}).unwrap().collect();
|
|
|
|
match rows {
|
|
|
|
Ok(r) => {
|
|
|
|
println!("Feed found\n{}",r[0].title);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
Err(e) => {panic!()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct ReturnedFeedURLs {
|
|
|
|
url: String,
|
2025-01-10 07:32:12 -05:00
|
|
|
}
|
|
|
|
pub fn update_feeds() {
|
|
|
|
//get feeds
|
2025-05-29 12:07:36 -04:00
|
|
|
let conn = Connection::open(get_db_path()).unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
let mut stmt = conn.prepare("select url from feeds").unwrap();
|
2025-07-02 20:11:43 -04:00
|
|
|
let rows = stmt
|
|
|
|
.query_map([], |row| {
|
|
|
|
Ok(ReturnedFeedURLs {
|
|
|
|
url: row.get(0).unwrap(),
|
|
|
|
})
|
2025-01-10 07:32:12 -05:00
|
|
|
})
|
2025-07-02 20:11:43 -04:00
|
|
|
.unwrap();
|
2025-01-10 07:32:12 -05:00
|
|
|
let mut urls: Vec<String> = Vec::new();
|
2025-07-02 20:11:43 -04:00
|
|
|
for feed in rows {
|
2025-01-10 07:32:12 -05:00
|
|
|
let url = feed.unwrap().url.clone();
|
|
|
|
urls.push(url);
|
|
|
|
}
|
|
|
|
stmt.finalize();
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
for u in urls {
|
|
|
|
store_items(load_rss(&u).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
//for each feed
|
2025-07-02 20:11:43 -04:00
|
|
|
// insert items into database
|
|
|
|
|
2025-01-10 07:32:12 -05:00
|
|
|
//close out
|
2025-07-02 20:11:43 -04:00
|
|
|
}
|