From bef3452fa2a5223bb3c1f35766c4dd9feccf6772 Mon Sep 17 00:00:00 2001 From: nain <126972030+nain-F49FF806@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:58:24 +0000 Subject: [PATCH] make scrape_expiries a API method and not an associated func this is to fix uniffi complaint: associated methods not supported --- src/api.rs | 32 ++++++++++++++++---------------- src/main.rs | 5 +++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/api.rs b/src/api.rs index 35f5125..4c03dd9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -26,22 +26,6 @@ impl API { } Self { base: url, opts } } - - pub fn scrape_expiries(url: Url) -> PbResult> { - let client = reqwest::blocking::Client::new(); - let response = client.get(url).send()?; - response.error_for_status_ref()?; - let html = response.text()?; - let document = Html::parse_document(&html); - let expiries_selector = Selector::parse("#expiration + ul > li > a").unwrap(); - let mut expiries = Vec::new(); - for expiry_anchor in document.select(&expiries_selector) { - if let Some(expiry) = expiry_anchor.attr("data-expiration") { - expiries.push(expiry.to_string()); - } - } - Ok(expiries) - } } impl API { @@ -224,6 +208,22 @@ impl API { } } + pub fn scrape_expiries(&self) -> PbResult> { + let client = reqwest::blocking::Client::new(); + let response = client.get(self.base()).send()?; + response.error_for_status_ref()?; + let html = response.text()?; + let document = Html::parse_document(&html); + let expiries_selector = Selector::parse("#expiration + ul > li > a").unwrap(); + let mut expiries = Vec::new(); + for expiry_anchor in document.select(&expiries_selector) { + if let Some(expiry) = expiry_anchor.attr("data-expiration") { + expiries.push(expiry.to_string()); + } + } + Ok(expiries) + } + pub fn base(&self) -> Url { self.base.clone() } diff --git a/src/main.rs b/src/main.rs index 47878c6..640fc6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -172,8 +172,9 @@ fn handle_comment(opts: &Opts) -> PbResult<()> { } fn handle_scrape(opts: &Opts) -> PbResult<()> { - let url = opts.get_url().clone(); - let expiries = API::scrape_expiries(url)?; + let url = opts.get_url(); + let api = API::new(url.clone(), opts.clone()); + let expiries = api.scrape_expiries()?; std::io::stdout().write_all(format!("{:?}", expiries).as_bytes())?; writeln!(std::io::stdout())?; Ok(())