even better

This commit is contained in:
2025-07-25 21:40:21 +02:00
parent b1220399b1
commit c14d78b2b0
22 changed files with 359 additions and 32 deletions

2
coffee/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target
Cargo.lock

11
coffee/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "coffee"
version = "0.1.0"
edition = "2024"
[dependencies]
axum = "0.6"
reqwest = "0.12.22"
scraper = "0.23.1"
serde = { version = "1.0.219", features = ["derive"] }
tokio = { version = "1.46.1", features = ["full"] }

1
coffee/README Normal file
View File

@@ -0,0 +1 @@
CHATGPT GENERATED BECAUSE LAZY

43
coffee/gen_js.py Normal file
View File

@@ -0,0 +1,43 @@
import requests
import json
response = requests.get("https://gist.githubusercontent.com/erdem/8c7d26765831d0f9a8c62f02782ae00d/raw/248037cd701af0a4957cce340dabb0fd04e38f4c/countries.json")
response.raise_for_status()
response = json.loads(response.text)
out = ""
for item in response:
tz = item["timezones"]
name = item["name"]
for x in tz:
out += f"{x}|{name}|"
print("""function userCountry() {
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
if(tz==null) return null;
const c=\""""+out+"""\".split("|");
for(let i=0;i<c.length;i+=2){
if(c[i]===timezone){
return c[i+1];
}}
return null;
}
async function byCountry(country) {
const url = `http://127.0.0.1:3000/price/${encodeURIComponent(country)}`;
try {
const response = await fetch(url);
if(!response.ok){throw new Error(`HTTP error ${response.status}`);}
const data = await response.json();
return data.price;
} catch (error) {
console.error("Failed to fetch price:", error);
return null;
}}
const c = userCountry();
if(c!=null){
byCountry(c).then(price => console.log("coffe price: " + price));
}
""")

67
coffee/src/main.rs Normal file
View File

@@ -0,0 +1,67 @@
use axum::{
extract::Path,
response::Json,
routing::get,
Router,
};
use reqwest::Client;
use scraper::{Html, Selector};
use serde::Serialize;
use std::net::SocketAddr;
#[derive(Serialize)]
struct PriceResponse {
price: f64,
}
async fn by_country(Path(country): Path<String>) -> Json<PriceResponse> {
let url = format!("https://coffeestics.com/countries/{}", country);
// Fetch the page
let response = Client::new()
.get(&url)
.send()
.await
.expect("Failed to fetch page")
.text()
.await
.expect("Failed to get text");
// Parse HTML
let document = Html::parse_document(&response);
// Create selector that matches the element you want
let selector = Selector::parse("body > div:nth-of-type(1) > div:nth-of-type(1) > section:nth-of-type(3) > div > div > div:nth-of-type(1) > div:nth-of-type(3) > a > div:nth-of-type(2)")
.unwrap();
// Extract text and parse as float
let price_str = document
.select(&selector)
.next()
.expect("Element not found")
.text()
.collect::<String>();
let price: f64 = price_str.trim().trim_start_matches('$')
.parse()
.expect("Failed to parse price");
Json(PriceResponse { price })
}
#[tokio::main]
async fn main() {
// Build our router
let app = Router::new()
.route("/price/:country", get(by_country));
// Run server
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}