[#28] Added basic ivago scraper

pull/35/head
Jef Roosens 2021-04-17 23:20:41 +02:00
parent 0877cc590c
commit 85611e2d59
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 33 additions and 4 deletions

View File

@ -19,6 +19,14 @@ test = true
bench = true
doc = true
doctest = true
[[bin]]
name = "populate_ivago"
path = "src/populate_ivago.rs"
test = false
bench = false
doc = false
doctest = false
[dependencies]
rocket = "0.4.7"

View File

@ -6,7 +6,7 @@ use diesel::PgConnection;
pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result<Vec<Street>, FejError> {
Ok(ivago_streets
.filter(name.like(search_term))
.or_filter(city.like(search_term))
.filter(name.ilike(format!("%{}%", search_term)))
.or_filter(city.ilike(format!("%{}%", search_term)))
.load(db_con)?)
}

View File

@ -1,4 +1,5 @@
use diesel::Queryable;
use crate::schema::ivago_streets;
use diesel::{Insertable, Queryable};
use regex::Regex;
use rocket::http::RawStr;
use rocket::request::FromFormValue;
@ -6,7 +7,8 @@ use serde::ser::{Serialize, SerializeStruct, Serializer};
use std::convert::TryFrom;
/// Represents a street in a given city
#[derive(Queryable)]
#[derive(Queryable, Insertable)]
#[table_name = "ivago_streets"]
pub struct Street {
name: String,
city: String,

View File

@ -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);
}
}
}