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` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`url` TEXT NOT NULL UNIQUE,
`uri` TEXT NOT NULL UNIQUE,
`last_modified` 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>,
}
#[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)]
#[table_name = "episode"]
@ -66,21 +74,21 @@ pub struct NewEpisode<'a> {
#[derive(Insertable)]
#[table_name = "podcast"]
#[derive(Debug, Clone)]
pub struct NewPodcast<'a> {
pub title: &'a str,
pub uri: &'a str,
pub link: Option<&'a str>,
pub description: Option<&'a str>,
pub image_uri: Option<&'a str>,
pub struct NewPodcast {
pub title: String,
pub uri: String,
pub link: Option<String>,
pub description: Option<String>,
pub image_uri: Option<String>,
}
impl<'a> NewPodcast<'a> {
// fn from_url(uri: &'a str) -> Result<NewPodcast> {
// let chan = Channel::from_url(uri)?;
// let foo = ::parse_feeds::parse_podcast(&chan, uri)?;
// Ok(foo)
// }
impl<'a> NewPodcast {
pub fn from_url(uri: &'a str) -> Result<NewPodcast> {
let chan = Channel::from_url(uri)?;
let foo = ::parse_feeds::parse_podcast(&chan, uri)?;
Ok(foo)
}
// Ignore this atm
// pub fn from_url(uri: &str) -> Result<()> {

View File

@ -2,20 +2,23 @@ use rss::{Channel, Item};
use models;
use errors::*;
pub fn parse_podcast<'a>(pd_chan: &'a Channel, uri: &'a str) -> Result<models::NewPodcast<'a>> {
let title = pd_chan.title();
pub fn parse_podcast(chan: &Channel, uri: &str) -> Result<models::NewPodcast> {
let link = Some(pd_chan.link());
let description = Some(pd_chan.description());
let title = chan.title().to_owned();
let image_uri = match pd_chan.image() {
Some(foo) => Some(foo.url()),
None => None,
};
let link = Some(chan.link().to_owned());
let description = Some(chan.description().to_owned());
// 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 {
uri: uri.to_owned(),
title,
uri,
link,
description,
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 pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "Intercepted with Jeremy Scahill");
// assert_eq!(
// pd.uri,
// "https://feeds.feedburner.com/InterceptedWithJeremyScahill"
// );
assert_eq!(pd.link, Some("https://theintercept.com/podcasts"));
assert_eq!(pd.description, Some(descr));
assert_eq!(pd.title, "Intercepted with Jeremy Scahill".to_string());
assert_eq!(
pd.uri,
"https://feeds.feedburner.com/InterceptedWithJeremyScahill".to_string()
);
assert_eq!(
pd.link,
Some("https://theintercept.com/podcasts".to_string())
);
assert_eq!(pd.description, Some(descr.to_string()));
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 pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "LINUX Unplugged Podcast");
assert_eq!(pd.link, Some("http://www.jupiterbroadcasting.com/"));
assert_eq!(pd.description, Some(descr));
assert_eq!(pd.title, "LINUX Unplugged Podcast".to_string());
assert_eq!(
pd.link,
Some("http://www.jupiterbroadcasting.com/".to_string())
);
assert_eq!(pd.description, Some(descr.to_string()));
assert_eq!(
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 pd = parse_podcast(&channel, uri).unwrap();
assert_eq!(pd.title, "Articles and Investigations - ProPublica");
assert_eq!(pd.link, Some("https://www.propublica.org/feeds/54Ghome"));
assert_eq!(pd.description, Some(descr));
assert_eq!(
pd.title,
"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!(
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! {
source (id) {
id -> Integer,
url -> Text,
uri -> Text,
last_modified -> Nullable<Text>,
http_etag -> Nullable<Text>,
}