Skip to content

Commit

Permalink
Add geckodriver url env var
Browse files Browse the repository at this point in the history
  • Loading branch information
RMcTn committed Sep 18, 2023
1 parent 35cd4fd commit 8c04b1d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
18 changes: 15 additions & 3 deletions scraper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ use fantoccini::{Client, ClientBuilder, Locator};
use bin_stuff::{Bin, BinDates};
use log::{error, info};

pub async fn get_stuff(postcode: &str, address: &str) -> Result<Vec<BinDates>, Box<dyn Error>> {
pub async fn get_stuff(
postcode: &str,
address: &str,
driver_url: Option<String>,
) -> Result<Vec<BinDates>, Box<dyn Error>> {
let mut capabilities = Capabilities::new();
let options = serde_json::json!({ "args": ["--headless"] });
capabilities.insert("moz:firefoxOptions".to_string(), options);

info!("Attempting to connect to webdriver client");
let default_driver_url = "http://127.0.0.1:4444".to_string();
if driver_url.is_none() {
info!(
"No driver_url provided. Defaulting to {}",
default_driver_url
);
}
let client = match ClientBuilder::native()
.capabilities(capabilities)
// TODO: Geckodriver URL as env var
.connect("http://127.0.0.1:4444")
.connect(&driver_url.unwrap_or("http://127.0.0.1:4444".to_string()))
.await
{
Ok(client) => client,
Expand All @@ -25,6 +36,7 @@ pub async fn get_stuff(postcode: &str, address: &str) -> Result<Vec<BinDates>, B
return Err(Box::new(e));
}
};
info!("Got webdriver client");

// NOTE: Some of the fields get different IDs when submitting each step it seems
let max_attempts = 3;
Expand Down
8 changes: 7 additions & 1 deletion server/src/email_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub async fn do_the_stuff(
users: &[User],
aws_client: &aws_sdk_sesv2::Client,
from_email_address: &str,
geckodriver_url: String,
) -> Result<(), Box<dyn Error>> {
for person in users {
println!("Found {:?}", person);
Expand All @@ -20,7 +21,12 @@ pub async fn do_the_stuff(
// Need to scrape the page from an actual browser. Tried curl/reqwest requests, but submitting
// the post request would redirect back to the first page. Some sort of request token missing
// when we do this or something (first step of form submission changes the url).
let bins = scraper::get_stuff(&person.postcode, &person.address).await?;
let bins = scraper::get_stuff(
&person.postcode,
&person.address,
Some(geckodriver_url.clone()),
)
.await?;

let today = chrono::Utc::now().date_naive();
let next_collection_date = next_collection_date_from(today);
Expand Down
14 changes: 14 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct AppState {
pool: SqlitePool,
aws_client: Client,
from_email_address: String,
geckodriver_url: String,
}

#[tokio::main]
Expand All @@ -61,6 +62,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
let _aws_secret_access_key =
env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY must be specified");
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be specified");
let geckodriver_url_default = "http://127.0.0.1:4444".to_string();
let geckodriver_url = match env::var("GECKODRIVER_URL") {
Ok(url) => url,
Err(_) => {
info!(
"GECKODRIVER_URL was not specified. Defaulting to {}",
geckodriver_url_default
);
geckodriver_url_default
}
};
let region_provider = RegionProviderChain::default_provider().or_else("eu-west-1");
let config = aws_config::from_env().region(region_provider).load().await;
let aws_client = Client::new(&config);
Expand All @@ -81,6 +93,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
pool,
aws_client,
from_email_address,
geckodriver_url,
};
let scheduler_app_state = app_state.clone();

Expand Down Expand Up @@ -124,6 +137,7 @@ async fn scrape_and_email_stuff(app_state: AppState) {
&people_to_notify,
&app_state.aws_client,
&app_state.from_email_address,
app_state.geckodriver_url,
)
.await
{
Expand Down

0 comments on commit 8c04b1d

Please sign in to comment.