mirror of
https://github.com/SkyfallWasTaken/arcade-monitor.git
synced 2024-11-24 01:43:44 +00:00
Get items from Arcade Shop
This commit is contained in:
commit
ab4ebcf521
5 changed files with 1347 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
target
|
||||
node_modules
|
||||
.wrangler
|
||||
.dev.vars
|
1235
Cargo.lock
generated
Normal file
1235
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
27
Cargo.toml
Normal file
27
Cargo.toml
Normal file
|
@ -0,0 +1,27 @@
|
|||
[package]
|
||||
name = "arcade-monitor"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["SkyfallWasTaken <mahadkalam1@proton.me>"]
|
||||
|
||||
[package.metadata.release]
|
||||
release = false
|
||||
|
||||
# https://github.com/rustwasm/wasm-pack/issues/1247
|
||||
[package.metadata.wasm-pack.profile.release]
|
||||
wasm-opt = false
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
worker = "0.3"
|
||||
worker-macros = "0.3"
|
||||
console_error_panic_hook = { version = "0.1.1" }
|
||||
serde = { version = "1.0.203", features = ["derive"] }
|
||||
serde_json = "1.0.118"
|
||||
scraper = "0.19.0"
|
||||
|
||||
# We don't use getrandom, but we enable the "js" feature to enable
|
||||
# WebAssembly support.
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
69
src/lib.rs
Normal file
69
src/lib.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use scraper::{Html, Selector};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use worker::*;
|
||||
|
||||
#[event(fetch, respond_with_errors)]
|
||||
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let router = Router::new();
|
||||
|
||||
router
|
||||
.on_async("/", |_req, ctx| async move {
|
||||
let body = "Hello, World!";
|
||||
|
||||
run_scrape(ctx.env).await?;
|
||||
|
||||
Response::ok(body)
|
||||
})
|
||||
.run(req, env)
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ShopItem {
|
||||
#[serde(rename = "Name")]
|
||||
name: String,
|
||||
|
||||
#[serde(rename = "Small Name")]
|
||||
small_name: Option<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<()> {
|
||||
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(),
|
||||
)?;
|
||||
|
||||
for item in available_items {
|
||||
let item_name = item.small_name.unwrap_or(item.name.clone());
|
||||
let item_description = item
|
||||
.description
|
||||
.unwrap_or(item.fulfillment_description.unwrap_or("".to_string()));
|
||||
let item_price = item.price;
|
||||
|
||||
let message = format!("{}: {} - ${}", item_name, item_description, item_price);
|
||||
console_debug!("{message}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
12
wrangler.toml
Normal file
12
wrangler.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
name = "arcade-monitor"
|
||||
main = "build/worker/shim.mjs"
|
||||
compatibility_date = "2024-06-29"
|
||||
|
||||
[build]
|
||||
command = "cargo install worker-build && worker-build --release"
|
||||
|
||||
[triggers]
|
||||
crons = ["0 * * * *"] # Run every hour
|
||||
|
||||
[vars]
|
||||
ARCADE_SHOP_URL = "https://hackclub.com/arcade/shop/"
|
Loading…
Reference in a new issue