preparing to handle media
This commit is contained in:
parent
1934888ee9
commit
24374a266d
2 changed files with 91 additions and 30 deletions
90
src/db.rs
90
src/db.rs
|
@ -24,7 +24,7 @@ const FEEDS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
|
||||||
'feedID' INTEGER NOT NULL,
|
'feedID' INTEGER NOT NULL,
|
||||||
'title' TEXT NOT NULL,
|
'title' TEXT NOT NULL,
|
||||||
'description' TEXT,
|
'description' TEXT,
|
||||||
'icon' BLOB,
|
'icon' text,
|
||||||
'url' text not null unique,
|
'url' text not null unique,
|
||||||
'subscribed' INTEGER NOT NULL default 0,
|
'subscribed' INTEGER NOT NULL default 0,
|
||||||
'last_updated' TEXT ,
|
'last_updated' TEXT ,
|
||||||
|
@ -40,7 +40,7 @@ const ITEMS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'items' (
|
||||||
'itemID' INTEGER NOT NULL,
|
'itemID' INTEGER NOT NULL,
|
||||||
'feedID' INTEGER NOT NULL,
|
'feedID' INTEGER NOT NULL,
|
||||||
'title' TEXT NOT NULL,
|
'title' TEXT NOT NULL,
|
||||||
'icon' BLOB,
|
'icon' text,
|
||||||
'url' text not null unique on conflict replace,
|
'url' text not null unique on conflict replace,
|
||||||
'description' TEXT,
|
'description' TEXT,
|
||||||
'content' TEXT,
|
'content' TEXT,
|
||||||
|
@ -88,29 +88,68 @@ pub fn initialize() {
|
||||||
println!("Database Initialized.")
|
println!("Database Initialized.")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_feed(url: &str) -> Option<usize> {
|
pub fn get_feed_id_by_url(url: &str) -> Option<usize> {
|
||||||
let conn = get_db();
|
let conn = get_db();
|
||||||
|
let mut stmt = conn
|
||||||
|
.prepare("select feedID from feeds where url=?1")
|
||||||
|
.unwrap();
|
||||||
|
match stmt.query_row([url], |row| row.get(0)) {
|
||||||
|
Ok(i) => Some(i),
|
||||||
|
Err(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_feed(url: &str) -> Option<usize> {
|
||||||
let feed = load_rss(url).unwrap();
|
let feed = load_rss(url).unwrap();
|
||||||
let new_feed = feed.clone();
|
|
||||||
let time = Utc::now().to_rfc2822();
|
let time = Utc::now().to_rfc2822();
|
||||||
|
let image = if let Some(i) = feed.image() {
|
||||||
|
i.url().to_owned()
|
||||||
|
} else {
|
||||||
|
"".to_owned()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut id = get_feed_id_by_url(url);
|
||||||
|
let conn = get_db();
|
||||||
|
match id {
|
||||||
|
Some(i) => {
|
||||||
match conn.execute(
|
match conn.execute(
|
||||||
"insert into feeds(title,url,description,last_updated) values(?1,?2,?3,?4)",
|
"update feeds set last_updated=?1,icon=?3,title=?4,description=?5 where feedID = ?2;",
|
||||||
[feed.title, url.to_string(), feed.description, time],
|
[time,i.to_string(),image,feed.title.to_owned(),feed.description.to_owned()]
|
||||||
){
|
){
|
||||||
Ok(_) => {}
|
Ok(_) => {println!("Updated feed.");}
|
||||||
|
Err(e) => {println!("Error updating feed.\n{}",e);}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
match conn.execute(
|
||||||
|
"insert into feeds(title,url,icon,description,last_updated) values(?1,?2,?3,?4,?5)",
|
||||||
|
[
|
||||||
|
feed.title.to_owned(),
|
||||||
|
url.to_string(),
|
||||||
|
image,
|
||||||
|
feed.description.to_owned(),
|
||||||
|
time,
|
||||||
|
],
|
||||||
|
) {
|
||||||
|
Ok(_) => {
|
||||||
|
id = get_feed_id_by_url(url)
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Couldn't add feed:{}\nError:{}", url, e);
|
println!("Couldn't add feed:{}\nError:{}", url, e);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut stmt = conn
|
match id {
|
||||||
.prepare("select feedID from feeds where url=?1")
|
Some(i) => {
|
||||||
.unwrap();
|
store_items(feed, i);
|
||||||
let id: usize = stmt.query_row([url], |row| row.get(0)).unwrap();
|
Some(i)
|
||||||
//need to get the feed_id from the DB and then make sure items are mapped to feed
|
|
||||||
store_items(new_feed, id);
|
}
|
||||||
Some(id)
|
None => {None}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_items(feed_id: usize) -> Result<usize> {
|
fn remove_items(feed_id: usize) -> Result<usize> {
|
||||||
|
@ -127,16 +166,22 @@ pub fn remove_feed(feed_id: usize) {
|
||||||
println!("Failed to delete feed by id: {}\nError:{}", feed_id, e);
|
println!("Failed to delete feed by id: {}\nError:{}", feed_id, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store_items(feed: rss::Channel, feed_id: usize) {
|
pub fn store_items(feed: rss::Channel, feed_id: usize) {
|
||||||
let conn = Connection::open(get_db_path()).unwrap();
|
let conn = Connection::open(get_db_path()).unwrap();
|
||||||
feed.items.into_iter().for_each(|i: rss::Item| {
|
feed.items.into_iter().for_each(|i: rss::Item| {
|
||||||
let t = i.clone();
|
let t = i.clone();
|
||||||
conn.execute(
|
let image = match i.itunes_ext() {
|
||||||
"insert into items(url,title,description,content,feedID,date,media)
|
Some(ext) => match ext.image() {
|
||||||
values(?1,?2,?3,?4,?5,?6,?7)",
|
Some(img) => img.to_owned(),
|
||||||
|
None => "".to_owned(),
|
||||||
|
},
|
||||||
|
None => "".to_owned(),
|
||||||
|
};
|
||||||
|
match conn.execute(
|
||||||
|
"insert into items(url,title,description,content,feedID,date,media,icon)
|
||||||
|
values(?1,?2,?3,?4,?5,?6,?7,?8)",
|
||||||
[
|
[
|
||||||
i.link,
|
i.link,
|
||||||
i.title,
|
i.title,
|
||||||
|
@ -150,9 +195,14 @@ pub fn store_items(feed: rss::Channel, feed_id: usize) {
|
||||||
None => Some("".to_owned()),
|
None => Some("".to_owned()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Some(image),
|
||||||
],
|
],
|
||||||
)
|
) {
|
||||||
.ok();
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Failed to add item.\n{}", e)
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
conn.close().unwrap();
|
conn.close().unwrap();
|
||||||
}
|
}
|
||||||
|
|
11
src/files.rs
11
src/files.rs
|
@ -11,3 +11,14 @@ pub fn get_data_directory() -> std::path::PathBuf {
|
||||||
};
|
};
|
||||||
dirs.data_dir().to_owned()
|
dirs.data_dir().to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_cache_directory() -> std::path::PathBuf {
|
||||||
|
let dirs = ProjectDirs::from("rocks","gabe","RSSCar").expect("Failed to get paths");
|
||||||
|
match fs::create_dir(dirs.cache_dir()){
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
|
||||||
|
Err(_) => {println!("Error creating cache directory")}
|
||||||
|
};
|
||||||
|
dirs.config_dir().to_owned()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue