diff options
| author | Fuwn <[email protected]> | 2024-01-12 17:23:28 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-01-12 17:23:28 -0800 |
| commit | 83284cd99bc77232e934c0eb505b7def2410c8d2 (patch) | |
| tree | 0a63d6f3a38e40b03b9e02381cd1af0e622b45e1 | |
| parent | fix(main): wait for geckodriver to be killed (diff) | |
| download | rin-83284cd99bc77232e934c0eb505b7def2410c8d2.tar.xz rin-83284cd99bc77232e934c0eb505b7def2410c8d2.zip | |
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/web.rs | 11 |
3 files changed, 13 insertions, 8 deletions
@@ -36,3 +36,6 @@ serde = { version = "1.0.111", features = ["derive"] } # Time chrono = "0.4.31" + +# Networking +port_scanner = "0.1.5" diff --git a/src/main.rs b/src/main.rs index 941b299..84ae088 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ #![recursion_limit = "128"] #![allow(clippy::cast_precision_loss)] -use thirtyfour::error::WebDriverResult; +use {port_scanner::request_open_port, thirtyfour::error::WebDriverResult}; mod schedule; mod web; @@ -24,6 +24,7 @@ struct Cache { #[tokio::main] async fn main() -> WebDriverResult<()> { + let port = request_open_port().unwrap_or(9515); let cache = format!("{}/rin_cache", std::env::temp_dir().display()); if std::path::Path::new(&cache).exists() @@ -53,8 +54,8 @@ async fn main() -> WebDriverResult<()> { } let mut geckodriver = - web::geckodriver().expect("failed to start geckodriver"); - let driver = web::webdriver().await.expect("failed to start webdriver"); + web::geckodriver(port).expect("failed to start geckodriver"); + let driver = web::webdriver(port).await.expect("failed to start webdriver"); let markdown = schedule::to_markdown(&driver).await.expect("failed to get schedule"); let schedule = schedule::markdown_to_map(&markdown); @@ -1,20 +1,21 @@ use {thirtyfour::WebDriver, tokio::process::Command}; -pub fn geckodriver() -> Result<tokio::process::Child, std::io::Error> { +pub fn geckodriver(port: u16) -> Result<tokio::process::Child, std::io::Error> { Command::new("geckodriver") - .arg("--port=9515") + .arg(format!("--port={port}")) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .spawn() } -pub async fn webdriver() -> Result<WebDriver, thirtyfour::error::WebDriverError> -{ +pub async fn webdriver( + port: u16, +) -> Result<WebDriver, thirtyfour::error::WebDriverError> { let mut caps = thirtyfour::DesiredCapabilities::firefox(); caps.set_headless().expect("failed to set headless"); - let driver = WebDriver::new("http://localhost:9515", caps) + let driver = WebDriver::new(&format!("http://localhost:{port}"), caps) .await .expect("failed to connect to webdriver"); |