[#28] Added basic ivago scraper
parent
0877cc590c
commit
85611e2d59
|
@ -19,6 +19,14 @@ test = true
|
||||||
bench = true
|
bench = true
|
||||||
doc = true
|
doc = true
|
||||||
doctest = true
|
doctest = true
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "populate_ivago"
|
||||||
|
path = "src/populate_ivago.rs"
|
||||||
|
test = false
|
||||||
|
bench = false
|
||||||
|
doc = false
|
||||||
|
doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = "0.4.7"
|
rocket = "0.4.7"
|
||||||
|
|
|
@ -6,7 +6,7 @@ use diesel::PgConnection;
|
||||||
|
|
||||||
pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result<Vec<Street>, FejError> {
|
pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result<Vec<Street>, FejError> {
|
||||||
Ok(ivago_streets
|
Ok(ivago_streets
|
||||||
.filter(name.like(search_term))
|
.filter(name.ilike(format!("%{}%", search_term)))
|
||||||
.or_filter(city.like(search_term))
|
.or_filter(city.ilike(format!("%{}%", search_term)))
|
||||||
.load(db_con)?)
|
.load(db_con)?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use diesel::Queryable;
|
use crate::schema::ivago_streets;
|
||||||
|
use diesel::{Insertable, Queryable};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
use rocket::request::FromFormValue;
|
use rocket::request::FromFormValue;
|
||||||
|
@ -6,7 +7,8 @@ use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
/// Represents a street in a given city
|
/// Represents a street in a given city
|
||||||
#[derive(Queryable)]
|
#[derive(Queryable, Insertable)]
|
||||||
|
#[table_name = "ivago_streets"]
|
||||||
pub struct Street {
|
pub struct Street {
|
||||||
name: String,
|
name: String,
|
||||||
city: String,
|
city: String,
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
use diesel::{insert_into, Connection, PgConnection, RunQueryDsl};
|
||||||
|
use fej::ivago::search_streets;
|
||||||
|
use fej::schema::ivago_streets::dsl::*;
|
||||||
|
|
||||||
|
const ABC: &str = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
|
|
||||||
|
let db_conn = PgConnection::establish(&database_url)
|
||||||
|
.expect(&format!("Error connecting to {}", database_url));
|
||||||
|
|
||||||
|
// Doing this linearly is good enough I'd say
|
||||||
|
for c in ABC.chars() {
|
||||||
|
if let Ok(streets) = search_streets(&c.to_string()) {
|
||||||
|
insert_into(ivago_streets).values(streets).execute(&db_conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue