diff --git a/src/items.rs b/src/items.rs new file mode 100644 index 0000000..51c1304 --- /dev/null +++ b/src/items.rs @@ -0,0 +1,38 @@ +use scraper::{Html, Selector}; +use serde::{Deserialize, Serialize}; +use worker::*; + +#[derive(Serialize, Deserialize)] +pub struct ShopItem { + #[serde(rename = "Full Name")] + pub full_name: String, + + #[serde(rename = "Description")] + pub description: Option, + + #[serde(rename = "Fulfillment Description")] + pub fulfillment_description: Option, + + #[serde(rename = "Cost Hours")] + pub price: i32, +} + +pub type ShopItems = Vec; + +pub async fn try_fetch(shop_url: Url) -> Result { + let mut response = Fetch::Url(shop_url).send().await?; + + let doc_html = response.text().await?; + let doc = Html::parse_document(&doc_html); + let selector = Selector::parse("#__NEXT_DATA__").unwrap(); + let json: serde_json::Value = + serde_json::from_str(&doc.select(&selector).next().unwrap().inner_html())?; + + let available_items = serde_json::from_value( + json.pointer("/props/pageProps/availableItems") + .expect("availableItems not found - is the ARCADE_SHOP_URL correct?") + .clone(), + )?; + + Ok(available_items) +} diff --git a/src/lib.rs b/src/lib.rs index 19a8ada..fe9fba2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ -use scraper::{Html, Selector}; -use serde::{Deserialize, Serialize}; use worker::*; +mod items; + #[event(fetch, respond_with_errors)] pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result { console_error_panic_hook::set_once(); @@ -18,37 +18,10 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result, - - #[serde(rename = "Fulfillment Description")] - fulfillment_description: Option, - - #[serde(rename = "Cost Hours")] - price: i32, -} - async fn run_scrape(env: Env) -> Result { let shop_url = Url::parse(&env.var("ARCADE_SHOP_URL")?.to_string())?; - let mut response = Fetch::Url(shop_url).send().await?; - - let doc_html = response.text().await?; - let doc = Html::parse_document(&doc_html); - let selector = Selector::parse("#__NEXT_DATA__").unwrap(); - let json: serde_json::Value = - serde_json::from_str(&doc.select(&selector).next().unwrap().inner_html())?; - - let available_items: Vec = serde_json::from_value( - json.pointer("/props/pageProps/availableItems") - .expect("availableItems not found - is the ARCADE_SHOP_URL correct?") - .clone(), - )?; + let available_items = items::try_fetch(shop_url).await?; let mut result = Vec::new(); for item in available_items { result.push(format!(