Trying insertable stucts that might own their data.

This commit is contained in:
Jordan Petridis 2017-09-19 08:15:54 +03:00
parent c204a61ff7
commit ffda7c6fc8
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
5 changed files with 114 additions and 38 deletions

View File

@ -1,6 +1,6 @@
CREATE TABLE `source` ( CREATE TABLE `source` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`url` TEXT NOT NULL UNIQUE, `uri` TEXT NOT NULL UNIQUE,
`last_modified` TEXT, `last_modified` TEXT,
`http_etag` TEXT `http_etag` TEXT
); );

49
src/index_feed.rs Normal file
View File

@ -0,0 +1,49 @@
use diesel::prelude::*;
use diesel;
use rss::Channel;
use schema;
use dbqueries;
use errors::*;
use models::NewPodcast;
pub fn foo() {
let inpt = vec![
"https://feeds.feedburner.com/InterceptedWithJeremyScahill",
"http://feeds.feedburner.com/linuxunplugged",
"http://feeds.propublica.org/propublica/main",
];
let db = ::establish_connection();
for feed in inpt.iter() {
match insert_feed(&db, feed) {
Ok(_) => {}
Err(foo) => {
debug!("Error: {}", foo);
debug!("Skipping...");
continue;
}
}
}
update_podcasts(&db);
}
fn insert_feed(connection: &SqliteConnection, url: &str) -> Result<()> {
let foo = NewPodcast::from_url(url)?;
diesel::insert(&foo).into(schema::podcast::table).execute(
connection,
)?;
Ok(())
}
fn update_podcasts(connection: &SqliteConnection) {
let pds = dbqueries::get_podcasts(connection).unwrap();
info!("{:?}", pds);
// for pd in pds {
// println!("{:?}" pd.uri);
// }
}

View File

@ -47,6 +47,14 @@ pub struct Source {
http_etag: Option<String>, http_etag: Option<String>,
} }
#[derive(Insertable)]
#[table_name = "source"]
#[derive(Debug, Clone)]
pub struct NewSource<'a> {
pub uri: &'a str,
pub last_modified: Option<&'a str>,
pub http_etag: Option<&'a str>,
}
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "episode"] #[table_name = "episode"]
@ -66,21 +74,21 @@ pub struct NewEpisode<'a> {
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "podcast"] #[table_name = "podcast"]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct NewPodcast<'a> { pub struct NewPodcast {
pub title: &'a str, pub title: String,
pub uri: &'a str, pub uri: String,
pub link: Option<&'a str>, pub link: Option<String>,
pub description: Option<&'a str>, pub description: Option<String>,
pub image_uri: Option<&'a str>, pub image_uri: Option<String>,
} }
impl<'a> NewPodcast<'a> { impl<'a> NewPodcast {
// fn from_url(uri: &'a str) -> Result<NewPodcast> { pub fn from_url(uri: &'a str) -> Result<NewPodcast> {
// let chan = Channel::from_url(uri)?; let chan = Channel::from_url(uri)?;
// let foo = ::parse_feeds::parse_podcast(&chan, uri)?; let foo = ::parse_feeds::parse_podcast(&chan, uri)?;
// Ok(foo) Ok(foo)
// } }
// Ignore this atm // Ignore this atm
// pub fn from_url(uri: &str) -> Result<()> { // pub fn from_url(uri: &str) -> Result<()> {

View File

@ -2,20 +2,23 @@ use rss::{Channel, Item};
use models; use models;
use errors::*; use errors::*;
pub fn parse_podcast<'a>(pd_chan: &'a Channel, uri: &'a str) -> Result<models::NewPodcast<'a>> { pub fn parse_podcast(chan: &Channel, uri: &str) -> Result<models::NewPodcast> {
let title = pd_chan.title();
let link = Some(pd_chan.link()); let title = chan.title().to_owned();
let description = Some(pd_chan.description());
let image_uri = match pd_chan.image() { let link = Some(chan.link().to_owned());
Some(foo) => Some(foo.url()), let description = Some(chan.description().to_owned());
None => None,
}; // let image_uri = match chan.image() {
// Some(foo) => Some(foo.url().to_owned()),
// None => None,
// };
let image_uri = chan.image().map(|foo| foo.url().to_owned());
let foo = models::NewPodcast { let foo = models::NewPodcast {
uri: uri.to_owned(),
title, title,
uri,
link, link,
description, description,
image_uri, image_uri,
@ -78,13 +81,16 @@ mod tests {
let descr = "The people behind The Intercepts fearless reporting and incisive commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and others—discuss the crucial issues of our time: national security, civil liberties, foreign policy, and criminal justice. Plus interviews with artists, thinkers, and newsmakers who challenge our preconceptions about the world we live in."; let descr = "The people behind The Intercepts fearless reporting and incisive commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and others—discuss the crucial issues of our time: national security, civil liberties, foreign policy, and criminal justice. Plus interviews with artists, thinkers, and newsmakers who challenge our preconceptions about the world we live in.";
let pd = parse_podcast(&channel, uri).unwrap(); let pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "Intercepted with Jeremy Scahill"); assert_eq!(pd.title, "Intercepted with Jeremy Scahill".to_string());
// assert_eq!( assert_eq!(
// pd.uri, pd.uri,
// "https://feeds.feedburner.com/InterceptedWithJeremyScahill" "https://feeds.feedburner.com/InterceptedWithJeremyScahill".to_string()
// ); );
assert_eq!(pd.link, Some("https://theintercept.com/podcasts")); assert_eq!(
assert_eq!(pd.description, Some(descr)); pd.link,
Some("https://theintercept.com/podcasts".to_string())
);
assert_eq!(pd.description, Some(descr.to_string()));
assert_eq!(pd.image_uri, None); assert_eq!(pd.image_uri, None);
@ -97,12 +103,17 @@ mod tests {
let descr = "An open show powered by community LINUX Unplugged takes the best attributes of open collaboration and focuses them into a weekly lifestyle show about Linux."; let descr = "An open show powered by community LINUX Unplugged takes the best attributes of open collaboration and focuses them into a weekly lifestyle show about Linux.";
let pd = parse_podcast(&channel, uri).unwrap(); let pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "LINUX Unplugged Podcast"); assert_eq!(pd.title, "LINUX Unplugged Podcast".to_string());
assert_eq!(pd.link, Some("http://www.jupiterbroadcasting.com/")); assert_eq!(
assert_eq!(pd.description, Some(descr)); pd.link,
Some("http://www.jupiterbroadcasting.com/".to_string())
);
assert_eq!(pd.description, Some(descr.to_string()));
assert_eq!( assert_eq!(
pd.image_uri, pd.image_uri,
Some("http://michaeltunnell.com/images/linux-unplugged.jpg") Some(
"http://michaeltunnell.com/images/linux-unplugged.jpg".to_string(),
)
); );
@ -115,12 +126,20 @@ mod tests {
let descr = "Latest Articles and Investigations from ProPublica, an independent, non-profit newsroom that produces investigative journalism in the public interest."; let descr = "Latest Articles and Investigations from ProPublica, an independent, non-profit newsroom that produces investigative journalism in the public interest.";
let pd = parse_podcast(&channel, uri).unwrap(); let pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "Articles and Investigations - ProPublica"); assert_eq!(
assert_eq!(pd.link, Some("https://www.propublica.org/feeds/54Ghome")); pd.title,
assert_eq!(pd.description, Some(descr)); "Articles and Investigations - ProPublica".to_string()
);
assert_eq!(
pd.link,
Some("https://www.propublica.org/feeds/54Ghome".to_string())
);
assert_eq!(pd.description, Some(descr.to_string()));
assert_eq!( assert_eq!(
pd.image_uri, pd.image_uri,
Some("https://assets.propublica.org/propublica-rss-logo.png") Some(
"https://assets.propublica.org/propublica-rss-logo.png".to_string(),
)
); );
} }

View File

@ -28,7 +28,7 @@ table! {
table! { table! {
source (id) { source (id) {
id -> Integer, id -> Integer,
url -> Text, uri -> Text,
last_modified -> Nullable<Text>, last_modified -> Nullable<Text>,
http_etag -> Nullable<Text>, http_etag -> Nullable<Text>,
} }