Move items scraper to items.rs

This commit is contained in:
SkyfallWasTaken 2024-07-01 17:57:09 +01:00
parent 2dd99c3c31
commit 4995a1e172
2 changed files with 41 additions and 30 deletions

38
src/items.rs Normal file
View file

@ -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<String>,
#[serde(rename = "Fulfillment Description")]
pub fulfillment_description: Option<String>,
#[serde(rename = "Cost Hours")]
pub price: i32,
}
pub type ShopItems = Vec<ShopItem>;
pub async fn try_fetch(shop_url: Url) -> Result<ShopItems> {
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)
}

View file

@ -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<Response> {
console_error_panic_hook::set_once();
@ -18,37 +18,10 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
.await
}
#[derive(Serialize, Deserialize)]
struct ShopItem {
#[serde(rename = "Full Name")]
full_name: String,
#[serde(rename = "Description")]
description: Option<String>,
#[serde(rename = "Fulfillment Description")]
fulfillment_description: Option<String>,
#[serde(rename = "Cost Hours")]
price: i32,
}
async fn run_scrape(env: Env) -> Result<String> {
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<ShopItem> = 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!(