Start printing out items

This commit is contained in:
SkyfallWasTaken 2024-06-29 13:09:42 +01:00
parent ab4ebcf521
commit 0db502e835

View file

@ -10,9 +10,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
router router
.on_async("/", |_req, ctx| async move { .on_async("/", |_req, ctx| async move {
let body = "Hello, World!"; let body = run_scrape(ctx.env).await?;
run_scrape(ctx.env).await?;
Response::ok(body) Response::ok(body)
}) })
@ -22,11 +20,8 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct ShopItem { struct ShopItem {
#[serde(rename = "Name")] #[serde(rename = "Full Name")]
name: String, full_name: String,
#[serde(rename = "Small Name")]
small_name: Option<String>,
#[serde(rename = "Description")] #[serde(rename = "Description")]
description: Option<String>, description: Option<String>,
@ -38,7 +33,7 @@ struct ShopItem {
price: i32, price: i32,
} }
async fn run_scrape(env: Env) -> Result<()> { async fn run_scrape(env: Env) -> Result<String> {
let shop_url = Url::parse(&env.var("ARCADE_SHOP_URL")?.to_string())?; let shop_url = Url::parse(&env.var("ARCADE_SHOP_URL")?.to_string())?;
let mut response = Fetch::Url(shop_url).send().await?; let mut response = Fetch::Url(shop_url).send().await?;
@ -54,16 +49,15 @@ async fn run_scrape(env: Env) -> Result<()> {
.clone(), .clone(),
)?; )?;
let mut result = Vec::new();
for item in available_items { for item in available_items {
let item_name = item.small_name.unwrap_or(item.name.clone()); result.push(format!(
let item_description = item "`{full_name}` - {price} {}",
.description if item.price == 1 { "ticket" } else { "tickets" },
.unwrap_or(item.fulfillment_description.unwrap_or("".to_string())); full_name = item.full_name.trim(),
let item_price = item.price; price = item.price,
));
let message = format!("{}: {} - ${}", item_name, item_description, item_price);
console_debug!("{message}");
} }
Ok(()) Ok(result.join("\n"))
} }