Compare commits

...
3 Commits
Author SHA1 Message Date
asepharyana 4efcf1f0ea feat(image): add brat + animated brat image generation
Deploy Scraper / build-and-deploy (push) Canceled after 0s
Ported from Shirokami-API scraper/image/brat.js (v2 path):
- /image/brat - static brat PNG via brat.siputzx.my.id API
- /image/brat/animated - animated brat GIF
Both return raw image bytes with proper Content-Type (image/png, image/gif)
2026-09-01 20:17:32 +07:00
asepharyana 278d4fe32a feat(tools): add whois(RDAP), iplocation, tinyurl, check-hosting, cek-resi, hargapangan
Ported from Shirokami-API scraper/tool/*.js:
- /tool/whois - RDAP lookup (whois.com HTML .df-raw is dead/JS-rendered since 2026; RDAP returns structured register/expiry/registrar data)
- /tool/iplocation - ipapi.co JSON
- /tool/tinyurl - tinyurl.com API
- /tool/check-hosting - hosting-checker.net API
- /tool/cek-resi - cekresi.com AES-128-CBC encrypted form POST
- /tool/hargapangan - Bapanas government API (upstream unreachable from VPS)

Fixes: scraper crate has no :has-text() pseudo-class -> replaced history-table scan with 2-cell-row detection; Html held across .await makes future non-Send -> scoped doc parsing
2026-09-01 20:12:52 +07:00
asepharyana 7ffc1bdd81 feat(search): add bmkg, jadwal-sholat, weather, yt-search, google endpoints 2026-09-01 19:51:34 +07:00
17 changed files with 1141 additions and 24 deletions

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
//! Application use-cases for image generation.
use crate::domain::error::ScrapingError;
use crate::infrastructure::repository::image as Repo;
pub async fn brat(text: &str) -> Result<Vec<u8>, ScrapingError> {
Repo::fetch_brat(text).await.map_err(ScrapingError::Http)
}
pub async fn brat_animated(text: &str) -> Result<Vec<u8>, ScrapingError> {
Repo::fetch_brat_animated(text)
.await
.map_err(ScrapingError::Http)
}
+3 -9
View File
@@ -13,21 +13,15 @@ pub async fn currency_converter(amount: f64, from: &str, to: &str) -> Result<Val
/// Harga emas Antam. /// Harga emas Antam.
pub async fn harga_emas() -> Result<Value, ScrapingError> { pub async fn harga_emas() -> Result<Value, ScrapingError> {
Repo::fetch_harga_emas() Repo::fetch_harga_emas().await.map_err(ScrapingError::Http)
.await
.map_err(ScrapingError::Http)
} }
/// Kurs BCA (jual/beli). /// Kurs BCA (jual/beli).
pub async fn kurs_bca() -> Result<Value, ScrapingError> { pub async fn kurs_bca() -> Result<Value, ScrapingError> {
Repo::fetch_kurs_bca() Repo::fetch_kurs_bca().await.map_err(ScrapingError::Http)
.await
.map_err(ScrapingError::Http)
} }
/// Server info (OS/CPU/RAM/disk). /// Server info (OS/CPU/RAM/disk).
pub async fn server_info() -> Result<Value, ScrapingError> { pub async fn server_info() -> Result<Value, ScrapingError> {
Repo::fetch_server_info() Repo::fetch_server_info().await.map_err(ScrapingError::Http)
.await
.map_err(ScrapingError::Http)
} }
+3
View File
@@ -1,8 +1,11 @@
pub mod anime; pub mod anime;
pub mod anime2; pub mod anime2;
pub mod downloader; pub mod downloader;
pub mod image;
pub mod komik; pub mod komik;
pub mod misc; pub mod misc;
pub mod proxy; pub mod proxy;
pub mod search;
pub mod stalk; pub mod stalk;
pub mod tools;
pub mod weebs; pub mod weebs;
+29
View File
@@ -0,0 +1,29 @@
//! Application use-cases for search utilities.
use crate::domain::error::ScrapingError;
use crate::infrastructure::repository::search as Repo;
use serde_json::Value;
pub async fn bmkg() -> Result<Value, ScrapingError> {
Repo::fetch_bmkg().await.map_err(ScrapingError::Http)
}
pub async fn jadwal_sholat(kota: &str) -> Result<Value, ScrapingError> {
Repo::fetch_jadwal_sholat(kota)
.await
.map_err(ScrapingError::Http)
}
pub async fn weather(city: &str) -> Result<Value, ScrapingError> {
Repo::fetch_weather(city).await.map_err(ScrapingError::Http)
}
pub async fn google(query: &str) -> Result<Value, ScrapingError> {
Repo::fetch_google(query).await.map_err(ScrapingError::Http)
}
pub async fn yt_search(query: &str) -> Result<Value, ScrapingError> {
Repo::fetch_yt_search(query)
.await
.map_err(ScrapingError::Http)
}
+35
View File
@@ -0,0 +1,35 @@
//! Application use-cases for tools.
use crate::domain::error::ScrapingError;
use crate::infrastructure::repository::tools as Repo;
use serde_json::Value;
pub async fn whois(domain: &str) -> Result<Value, ScrapingError> {
Repo::fetch_whois(domain).await.map_err(ScrapingError::Http)
}
pub async fn ip_location(ip: &str) -> Result<Value, ScrapingError> {
Repo::fetch_ip_location(ip)
.await
.map_err(ScrapingError::Http)
}
pub async fn tinyurl(url: &str) -> Result<Value, ScrapingError> {
Repo::fetch_tinyurl(url).await.map_err(ScrapingError::Http)
}
pub async fn check_hosting(domain: &str) -> Result<Value, ScrapingError> {
Repo::fetch_check_hosting(domain)
.await
.map_err(ScrapingError::Http)
}
pub async fn hargapangan() -> Result<Value, ScrapingError> {
Repo::fetch_hargapangan().await.map_err(ScrapingError::Http)
}
pub async fn cek_resi(resi: String, ekspedisi: Option<String>) -> Result<Value, ScrapingError> {
Repo::fetch_cek_resi(&resi, ekspedisi.as_deref())
.await
.map_err(ScrapingError::Http)
}
+46
View File
@@ -0,0 +1,46 @@
//! Infrastructure — Image generation endpoints (brat).
//!
//! Ported from Shirokami-API `scraper/image/brat.js`.
//! brat v2 uses the external brat.siputzx.my.id API (returns image bytes).
use crate::infrastructure::utils::http_client::http_client;
use reqwest::header::USER_AGENT;
const BRAT_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0 Safari/537.36";
fn urlencode(s: &str) -> String {
s.replace('%', "%25")
.replace('&', "%26")
.replace('?', "%3F")
}
/// Fetch a brat image (static) for the given text.
pub async fn fetch_brat(text: &str) -> Result<Vec<u8>, String> {
let url = format!("https://brat.siputzx.my.id/image?text={}", urlencode(text));
fetch_bytes(&url).await
}
/// Fetch an animated brat GIF for the given text.
pub async fn fetch_brat_animated(text: &str) -> Result<Vec<u8>, String> {
let url = format!("https://brat.siputzx.my.id/gif?text={}", urlencode(text));
fetch_bytes(&url).await
}
async fn fetch_bytes(url: &str) -> Result<Vec<u8>, String> {
let resp = http_client()
.client()
.get(url)
.header(USER_AGENT, BRAT_UA)
.header("X-Forwarded-For", "104.28.209.35")
.header("X-Real-IP", "104.28.209.35")
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
if !resp.status().is_success() {
return Err(format!("HTTP {}", resp.status()));
}
resp.bytes()
.await
.map(|b| b.to_vec())
.map_err(|e| format!("Body: {}", e))
}
+7 -6
View File
@@ -68,11 +68,8 @@ pub async fn fetch_currency_converter(amount: f64, from: &str, to: &str) -> Resu
.collect::<String>(); .collect::<String>();
// Look for patterns like "15,885.00 IDR" in body text // Look for patterns like "15,885.00 IDR" in body text
let re2 = Regex::new(&format!( let re2 = Regex::new(&format!(r"([0-9,.]+)\s+{}", to.to_uppercase()))
r"([0-9,.]+)\s+{}", .map_err(|e| format!("Regex compile: {}", e))?;
to.to_uppercase()
))
.map_err(|e| format!("Regex compile: {}", e))?;
if let Some(caps) = re2.captures(&text) { if let Some(caps) = re2.captures(&text) {
let rate_str = caps.get(1).map(|m| m.as_str()).unwrap_or("0"); let rate_str = caps.get(1).map(|m| m.as_str()).unwrap_or("0");
@@ -228,7 +225,11 @@ pub async fn fetch_kurs_bca() -> Result<Value, String> {
.map(|td| td.text().collect::<String>().trim().to_string()) .map(|td| td.text().collect::<String>().trim().to_string())
.collect(); .collect();
if cols.len() >= 3 && !cols[0].is_empty() && !cols[1].is_empty() && !cols[2].is_empty() { if cols.len() >= 3
&& !cols[0].is_empty()
&& !cols[1].is_empty()
&& !cols[2].is_empty()
{
kurs_data.push(json!({ kurs_data.push(json!({
"currency": cols[0], "currency": cols[0],
"jual": cols[1], "jual": cols[1],
+3
View File
@@ -1,11 +1,14 @@
pub mod alqanime; pub mod alqanime;
pub mod downloader; pub mod downloader;
pub mod image;
pub mod komik; pub mod komik;
pub mod misc; pub mod misc;
pub mod otakudesu; pub mod otakudesu;
pub mod parsers; pub mod parsers;
pub mod proxy; pub mod proxy;
pub mod search;
pub mod stalk; pub mod stalk;
pub mod tools;
pub mod weebs; pub mod weebs;
pub use alqanime::AlqanimeRepository; pub use alqanime::AlqanimeRepository;
+289
View File
@@ -0,0 +1,289 @@
//! Infrastructure — Search utilities.
//!
//! Ported from Shirokami-API `scraper/search/*.js`:
//! bmkg (BMKG earthquake), jadwal-sholat (myquran.com), weather (OpenWeather),
//! google (HTML scrape), yt (via yt-dlp flat-playlist).
use crate::infrastructure::utils::http_client::http_client;
use reqwest::header::USER_AGENT;
use scraper::{Html, Selector};
use serde_json::{json, Value};
use std::process::Stdio;
/// BMKG Indonesia earthquake info (auto-gempa, terkini, dirasakan).
pub async fn fetch_bmkg() -> Result<Value, String> {
let urls = [
"https://data.bmkg.go.id/DataMKG/TEWS/autogempa.json",
"https://data.bmkg.go.id/DataMKG/TEWS/gempaterkini.json",
"https://data.bmkg.go.id/DataMKG/TEWS/gempadirasakan.json",
];
let mut auto = Value::Null;
let mut terkini: Vec<Value> = Vec::new();
let mut dirasakan: Vec<Value> = Vec::new();
for url in urls {
let resp = http_client()
.client()
.get(url)
.header(
USER_AGENT,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
)
.send()
.await
.map_err(|e| format!("HTTP error: {}", e))?;
if !resp.status().is_success() {
return Err(format!("BMKG returned HTTP {}", resp.status()));
}
let data: Value = resp
.json()
.await
.map_err(|e| format!("JSON parse: {}", e))?;
if url.contains("autogempa") {
auto = data
.pointer("/Infogempa/gempa")
.cloned()
.unwrap_or(Value::Null);
} else if url.contains("gempaterkini") {
terkini = data
.pointer("/Infogempa/gempa")
.and_then(|g| g.as_array())
.cloned()
.unwrap_or_default();
} else {
dirasakan = data
.pointer("/Infogempa/gempa")
.and_then(|g| g.as_array())
.cloned()
.unwrap_or_default();
}
}
Ok(json!({
"autogempa": auto,
"gempaterkini": terkini,
"gempadirasakan": dirasakan,
}))
}
/// Jadwal sholat from myquran.com.
pub async fn fetch_jadwal_sholat(kota: &str) -> Result<Value, String> {
// 1. Find city id
let search_url = format!(
"https://api.myquran.com/v2/sholat/kota/cari/{}",
urlencode(kota)
);
let city_data = get_json(&search_url).await?;
if city_data
.get("status")
.and_then(|s| s.as_bool())
.unwrap_or(false)
!= true
{
return Ok(json!({ "error": "Kota tidak ditemukan" }));
}
let id_list = city_data
.get("data")
.and_then(|d| d.as_array())
.cloned()
.unwrap_or_default();
if id_list.is_empty() {
return Ok(json!({ "error": "Kota tidak ditemukan" }));
}
// 2. Today's date (Asia/Jakarta). Use chrono with a fixed offset approximation.
let now = chrono::Local::now();
let (year, month, day) = (
now.format("%Y").to_string(),
now.format("%m").to_string(),
now.format("%d").to_string(),
);
let mut results: Vec<Value> = Vec::new();
for city in id_list.iter().take(5) {
let cid = city.get("id").and_then(|i| i.as_str()).unwrap_or("");
let jadwal_url = format!(
"https://api.myquran.com/v2/sholat/jadwal/{}/{}/{}/{}",
cid, year, month, day
);
if let Ok(sched) = get_json(&jadwal_url).await {
if let Some(d) = sched.get("data") {
results.push(json!({
"lokasi": d.get("lokasi").cloned().unwrap_or(Value::Null),
"daerah": d.get("daerah").cloned().unwrap_or(Value::Null),
"jadwal": d.get("jadwal").cloned().unwrap_or(Value::Null),
}));
}
}
}
if results.is_empty() {
return Ok(json!({ "error": "Tidak dapat mengambil jadwal" }));
}
Ok(json!({ "total": results.len(), "schedules": results }))
}
/// OpenWeather current weather (uses env OPENWEATHER_API_KEY, falls back to source's key).
pub async fn fetch_weather(city: &str) -> Result<Value, String> {
let key = std::env::var("OPENWEATHER_API_KEY")
.unwrap_or_else(|_| "060a6bcfa19809c2cd4d97a212b19273".to_string());
let url = format!(
"https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}",
urlencode(city),
key
);
get_json(&url).await
}
/// Google web search via HTML scraping.
pub async fn fetch_google(query: &str) -> Result<Value, String> {
let url = format!(
"https://www.google.com/search?q={}&safe=off&hl=en&gl=us",
urlencode(query)
);
let resp = http_client()
.client()
.get(&url)
.header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36")
.header("Accept-Language", "en-US")
.send()
.await
.map_err(|e| format!("HTTP error: {}", e))?;
let body = resp
.text()
.await
.map_err(|e| format!("Failed to read body: {}", e))?;
let document = Html::parse_document(&body);
let result_sel =
Selector::parse("div.g, div[data-hveid]").unwrap_or_else(|_| Selector::parse("a").unwrap());
let link_sel = Selector::parse("a").unwrap();
let h3_sel = Selector::parse("h3").unwrap();
let mut results: Vec<Value> = Vec::new();
for node in document.select(&result_sel) {
let link = node
.select(&link_sel)
.find_map(|a| a.value().attr("href"))
.and_then(|h| h.strip_prefix("/url?q="))
.map(|h| h.split('&').next().unwrap_or(h).to_string());
let title = node
.select(&h3_sel)
.next()
.map(|h| h.text().collect::<String>());
if title.is_some() && link.is_some() {
results.push(json!({
"title": title.unwrap_or_default(),
"description": "",
"link": link.unwrap_or_default(),
}));
}
if results.len() >= 10 {
break;
}
}
Ok(json!(results))
}
/// YouTube search via yt-dlp flat playlist (real results, scraping).
pub async fn fetch_yt_search(query: &str) -> Result<Value, String> {
let q = format!("ytsearch15:{}", query);
let output = run_ytdlp(&q).await?;
let mut videos: Vec<Value> = Vec::new();
let arr: Vec<Value> = serde_json::from_str(&output).unwrap_or_default();
for v in arr.iter().take(15) {
videos.push(json!({
"title": v.get("title").cloned().unwrap_or(Value::Null),
"id": v.get("id").cloned().unwrap_or(Value::Null),
"url": v.get("webpage_url").cloned().unwrap_or(Value::Null),
"thumbnail": v.get("thumbnail").cloned().unwrap_or(Value::Null),
"duration": v.get("duration").cloned().unwrap_or(Value::Null),
"views": v.get("view_count").cloned().unwrap_or(Value::Null),
"author": {
"name": v.pointer("/channel").cloned().unwrap_or(Value::Null),
"url": v.get("channel_url").cloned().unwrap_or(Value::Null),
},
}));
}
Ok(json!({ "total": videos.len(), "videos": videos }))
}
// ============================================================================
// Helpers
// ============================================================================
async fn get_json(url: &str) -> Result<Value, String> {
let resp = http_client()
.client()
.get(url)
.header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36")
.send()
.await
.map_err(|e| format!("HTTP error: {}", e))?;
if !resp.status().is_success() {
return Err(format!("HTTP {} from upstream", resp.status()));
}
resp.json::<Value>()
.await
.map_err(|e| format!("JSON parse: {}", e))
}
/// Run yt-dlp with `--dump-json --flat-playlist` for the given search/query arg.
async fn run_ytdlp(query_arg: &str) -> Result<String, String> {
let bins = [
"/usr/local/bin/yt-dlp-native",
"/home/code/hermes-agent/.venv/bin/yt-dlp",
"yt-dlp",
];
let bin = bins
.iter()
.find(|b| std::path::Path::new(b).exists() || **b == "yt-dlp")
.copied()
.unwrap_or("yt-dlp");
let child = tokio::process::Command::new(bin)
.args(["--dump-json", "--flat-playlist", "--no-warnings", query_arg])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.map_err(|e| format!("Failed to spawn yt-dlp: {}", e))?;
let output = child
.wait_with_output()
.await
.map_err(|e| format!("yt-dlp failed: {}", e))?;
let stdout = String::from_utf8(output.stdout).map_err(|e| format!("Invalid UTF-8: {}", e))?;
// `--flat-playlist` outputs multiple JSON lines (one per video).
let mut joined = String::new();
joined.push('[');
let mut first = true;
for line in stdout.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if !first {
joined.push(',');
}
joined.push_str(line);
first = false;
}
joined.push(']');
Ok(joined)
}
fn urlencode(s: &str) -> String {
let mut out = String::new();
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char)
}
b' ' => out.push_str("%20"),
_ => out.push_str(&format!("%{:02X}", b)),
}
}
out
}
+23 -8
View File
@@ -81,8 +81,13 @@ pub async fn fetch_youtube_stalk(username: &str) -> Result<Value, String> {
.map_err(|e| format!("Failed to read body: {}", e))?; .map_err(|e| format!("Failed to read body: {}", e))?;
// Extract var ytInitialData = {...} via brace matching (JSON may contain ";"). // Extract var ytInitialData = {...} via brace matching (JSON may contain ";").
let start = body.find("var ytInitialData = ").ok_or_else(|| "ytInitialData not found".to_string())?; let start = body
let brace = body[start..].find('{').ok_or_else(|| "ytInitialData parse".to_string())? + start; .find("var ytInitialData = ")
.ok_or_else(|| "ytInitialData not found".to_string())?;
let brace = body[start..]
.find('{')
.ok_or_else(|| "ytInitialData parse".to_string())?
+ start;
let json_str = extract_balanced_json(&body, brace)?; let json_str = extract_balanced_json(&body, brace)?;
let parsed: Value = let parsed: Value =
serde_json::from_str(&json_str).map_err(|e| format!("JSON parse: {}", e))?; serde_json::from_str(&json_str).map_err(|e| format!("JSON parse: {}", e))?;
@@ -99,7 +104,10 @@ pub async fn fetch_youtube_stalk(username: &str) -> Result<Value, String> {
"isFamilySafe": Value::Null, "isFamilySafe": Value::Null,
}); });
if let Some(meta) = parsed.get("metadata").and_then(|m| m.get("channelMetadataRenderer")) { if let Some(meta) = parsed
.get("metadata")
.and_then(|m| m.get("channelMetadataRenderer"))
{
let mut set = |dst: &str, src: &str| { let mut set = |dst: &str, src: &str| {
if let Some(v) = meta.get(src) { if let Some(v) = meta.get(src) {
channel[dst] = v.clone(); channel[dst] = v.clone();
@@ -112,7 +120,10 @@ pub async fn fetch_youtube_stalk(username: &str) -> Result<Value, String> {
} }
// Subscriber/video count from header.pageHeaderRenderer // Subscriber/video count from header.pageHeaderRenderer
if let Some(header) = parsed.get("header").and_then(|h| h.get("pageHeaderRenderer")) { if let Some(header) = parsed
.get("header")
.and_then(|h| h.get("pageHeaderRenderer"))
{
if let Some(rows) = header if let Some(rows) = header
.pointer("/content/pageHeaderViewModel/metadata/contentMetadataViewModel/metadataRows") .pointer("/content/pageHeaderViewModel/metadata/contentMetadataViewModel/metadataRows")
{ {
@@ -293,8 +304,10 @@ fn video_from_video_renderer(vd: &Value) -> Value {
.pointer("/thumbnailOverlays") .pointer("/thumbnailOverlays")
.and_then(|o| o.as_array()) .and_then(|o| o.as_array())
.and_then(|arr| { .and_then(|arr| {
arr.iter() arr.iter().find_map(|ov| {
.find_map(|ov| ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText").cloned()) ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText")
.cloned()
})
}) })
{ {
v["duration"] = dur; v["duration"] = dur;
@@ -393,8 +406,10 @@ fn video_from_grid_video(gv: &Value) -> Value {
.pointer("/thumbnailOverlays") .pointer("/thumbnailOverlays")
.and_then(|o| o.as_array()) .and_then(|o| o.as_array())
.and_then(|arr| { .and_then(|arr| {
arr.iter() arr.iter().find_map(|ov| {
.find_map(|ov| ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText").cloned()) ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText")
.cloned()
})
}) })
{ {
v["duration"] = dur; v["duration"] = dur;
+438
View File
@@ -0,0 +1,438 @@
//! Infrastructure — Tool utilities.
//!
//! Ported from Shirokami-API `scraper/tool/*.js` and `scraper/tool/*.js`:
//! whois, ip-location, tinyurl, check-hosting, cek-resi, hargapangan.
use crate::infrastructure::utils::http_client::http_client;
use regex::Regex;
use reqwest::header::USER_AGENT;
use scraper::{Html, Selector};
use serde_json::{json, Value};
use urlencoding::encode as urlencode;
const CHROME_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// WHOIS — RDAP lookup (whois.com HTML `.df-raw` is dead/JS-rendered since 2026;
// RDAP is the structured, reliable server-side source for the same data).
// ---------------------------------------------------------------------------
pub async fn fetch_whois(domain: &str) -> Result<Value, String> {
let domain = domain.to_lowercase();
let tld = domain.rsplit('.').next().unwrap_or("com");
let base = match tld {
"com" | "net" => "https://rdap.verisign.com",
"org" => "https://rdap.org",
"io" => "https://rdap.identitydigital.services",
"co" => "https://rdap.nic.co",
"id" => "https://rdap.idnnic.id",
_ => "https://rdap.org", // generic bootstrap
};
let url = if base == "https://rdap.verisign.com" {
format!("{}/{}/v1/domain/{}", base, tld, domain)
} else {
format!("{}/domain/{}", base, domain)
};
let resp = http_client()
.client()
.get(&url)
.header(
USER_AGENT,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0 Safari/537.36",
)
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(
json!({"domain": domain, "available": true, "valid": true, "data": "No data", "rawData": "No data"}),
);
}
if !resp.status().is_success() {
return Ok(
json!({"domain": domain, "valid": false, "data": "RDAP unavailable", "rawData": "No data"}),
);
}
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
let event_date = |action: &str| -> String {
data.get("events")
.and_then(|e| e.as_array())
.map(|events| {
events
.iter()
.find(|ev| ev.get("eventAction").and_then(|a| a.as_str()) == Some(action))
.and_then(|ev| ev.get("eventDate").and_then(|d| d.as_str()))
.unwrap_or("No data")
.to_string()
})
.unwrap_or_else(|| "No data".to_string())
};
let domain_name = data
.get("ldhName")
.and_then(|v| v.as_str())
.unwrap_or(&domain)
.to_string();
let domain_id = data
.get("handle")
.and_then(|v| v.as_str())
.unwrap_or("No data")
.to_string();
let creation = event_date("registration");
let expiration = event_date("expiration");
let updated = event_date("last changed");
// Registrar name from the registrar entity's vcardArray
let registrar = data
.get("entities")
.and_then(|e| e.as_array())
.map(|entities| {
entities
.iter()
.find(|en| {
en.get("roles")
.and_then(|r| r.as_array())
.map(|roles| roles.iter().any(|ro| ro.as_str() == Some("registrar")))
.unwrap_or(false)
})
.and_then(|reg| {
reg.get("vcardArray")
.and_then(|v| v.as_array())
.and_then(|arr| arr.get(1))
.and_then(|props| props.as_array())
.and_then(|props| {
props.iter().find_map(|p| {
let pa = p.as_array()?;
if pa.get(0)?.as_str() == Some("fn") {
pa.get(3).and_then(|v| v.as_str()).map(|s| s.to_string())
} else {
None
}
})
})
})
.unwrap_or_else(|| "No data".to_string())
})
.unwrap_or_else(|| "No data".to_string());
Ok(json!({
"domain": domain,
"available": false,
"valid": true,
"data": {
"domainName": domain_name,
"registryDomainId": domain_id,
"creationDate": creation,
"expirationDate": expiration,
"updatedDate": updated,
"country": "No data",
"registrar": registrar,
},
"rawData": serde_json::to_string(&data).unwrap_or_default(),
}))
}
// ---------------------------------------------------------------------------
// IP Location — ipapi.co JSON
// ---------------------------------------------------------------------------
pub async fn fetch_ip_location(ip: &str) -> Result<Value, String> {
let url = format!("https://ipapi.co/{}/json/", ip);
let resp = http_client()
.client()
.get(&url)
.header(USER_AGENT, "nodejs-ipapi-v1.02")
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
resp.json().await.map_err(|e| format!("JSON: {}", e))
}
// ---------------------------------------------------------------------------
// TinyURL — tinyurl.com API
// ---------------------------------------------------------------------------
pub async fn fetch_tinyurl(url: &str) -> Result<Value, String> {
let api_url = format!("https://tinyurl.com/api-create.php?url={}", urlencode(url));
let resp = http_client()
.client()
.get(&api_url)
.header(USER_AGENT, CHROME_UA)
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
let short_url = resp.text().await.map_err(|e| format!("Body: {}", e))?;
Ok(json!({"shortUrl": short_url.trim()}))
}
// ---------------------------------------------------------------------------
// Check Hosting — hosting-checker.net API
// ---------------------------------------------------------------------------
pub async fn fetch_check_hosting(domain: &str) -> Result<Value, String> {
let cd = domain.to_lowercase().trim().to_string();
// Strip scheme and www
let cd = cd
.trim_start_matches("http://")
.trim_start_matches("https://")
.trim_start_matches("www.")
.to_string();
let domain_re =
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
if !domain_re.is_match(&cd) {
return Err("Invalid domain format".into());
}
let url = format!("https://hosting-checker.net/api/hosting/{}", cd);
let resp = http_client()
.client()
.get(&url)
.header("authority", "hosting-checker.net")
.header(USER_AGENT, "Postify/1.0.0")
.header(
"referer",
format!("https://hosting-checker.net/websites/{}", cd),
)
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
if !resp.status().is_success() {
return Err(format!("HTTP {}", resp.status()));
}
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
// Pass through the relevant fields (same structure as JS version)
Ok(json!({
"domain": {
"name": data["web"]["domain"],
"original": data["web"]["originalDomain"],
"ipv6_support": data["web"]["ipV6Support"],
},
"web": {
"ips": data["web"]["lookups"],
"providers": data["web"]["providers"],
},
"nameserver": data["nameserver"],
"mail": {"incoming": data["incomingMail"], "outgoing": data["outgoingMail"]},
}))
}
// ---------------------------------------------------------------------------
// Harga Pangan (BAPANAS) — Indonesian government food prices
// ---------------------------------------------------------------------------
const BAPANAS_KEY: &str = "zHWbt7U2qvPoUDkiUgvnOqYrtj3zClR7unnH2G4apE7HcMV4QyNC6BSD0yV3uvSHqS91TxwE8aMDTiCznmGceEX3zQmO1Xwq7TJblotIt2CpwvK6YjRKDJwcgMJwav9p4RshM3nfuFyurSQQv9BhueMJ0HJ778oD";
pub async fn fetch_hargapangan() -> Result<Value, String> {
let url = "https://api-panelhargav2.badanpangan.go.id/api/front/harga-pangan-informasi";
let resp = http_client()
.client()
.get(url)
.header(USER_AGENT, CHROME_UA)
.header("accept", "application/json")
.header("x-api-key", BAPANAS_KEY)
.header("origin", "https://panelharga.badanpangan.go.id")
.header("referer", "https://panelharga.badanpangan.go.id/")
.query(&[("level_harga_id", "1")])
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?;
if !resp.status().is_success() {
return Err(format!("HTTP {}", resp.status()));
}
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
if data.get("status").and_then(|s| s.as_str()) == Some("success") {
Ok(data.get("data").cloned().unwrap_or(Value::Null))
} else {
Ok(Value::Null)
}
}
// ---------------------------------------------------------------------------
// Cek Resi (cekresi.com) — Indonesian package tracking
// ---------------------------------------------------------------------------
fn aes_encrypt_resi(resi: &str) -> String {
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{block_padding::Pkcs7, BlockEncryptMut, KeyIvInit};
type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
let key_hex = "79540e250fdb16afac03e19c46dbdeb3";
let iv_hex = "eb2bb9425e81ffa942522e4414e95bd0";
let key_bytes = hex::decode(key_hex).expect("bad key hex");
let iv_bytes = hex::decode(iv_hex).expect("bad iv hex");
let key = GenericArray::from_slice(&key_bytes);
let iv = GenericArray::from_slice(&iv_bytes);
let data = resi.as_bytes();
let mut buf = vec![0u8; data.len() + 16];
buf[..data.len()].copy_from_slice(data);
let ct = Aes128CbcEnc::new(key, iv)
.encrypt_padded_mut::<Pkcs7>(&mut buf, data.len())
.expect("encrypt");
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, ct)
}
fn detect_ekspedisi(resi: &str) -> Option<&'static str> {
let upper = resi.to_uppercase();
let map = [
("jne", "JNE"),
("pos", "POS"),
("tiki", "TIKI"),
("jnt", "J&T"),
("sicepat", "SICEPAT"),
("anteraja", "ANTERAJA"),
("ninja", "NINJA"),
("lionparcel", "LIONPARCEL"),
];
for (prefix, code) in map {
if upper.starts_with(code) {
return Some(prefix);
}
}
None
}
pub async fn fetch_cek_resi(noresi: &str, ekspedisi: Option<&str>) -> Result<Value, String> {
let noresi = noresi.trim().to_uppercase();
let ekspedisi = match ekspedisi {
Some(e) => e.to_string(),
None => detect_ekspedisi(&noresi)
.ok_or_else(|| "Ekspedisi tidak ditemukan dari nomor resi".to_string())?
.to_string(),
};
let timers = aes_encrypt_resi(&noresi);
// 1. GET the form to extract viewstate + secret_key
let client = http_client().client();
let html = client
.get("https://cekresi.com/")
.header(USER_AGENT, CHROME_UA)
.send()
.await
.map_err(|e| format!("HTTP: {}", e))?
.text()
.await
.map_err(|e| format!("Body: {}", e))?;
let (viewstate, secret_key) = {
let doc = Html::parse_document(&html);
let input_sel = Selector::parse(r#"input[name="viewstate"]"#).unwrap();
let secret_sel = Selector::parse(r#"input[name="secret_key"]"#).unwrap();
let vs = doc
.select(&input_sel)
.next()
.and_then(|e| e.value().attr("value"))
.unwrap_or("")
.to_string();
let sk = doc
.select(&secret_sel)
.next()
.and_then(|e| e.value().attr("value"))
.unwrap_or("")
.to_string();
(vs, sk)
};
// 2. POST the tracking request
let w = &format!("{:x}", fastrand::u32(..));
let post_url = format!(
"https://apa2.cekresi.com/cekresi/resi/initialize.php?ui=e0ad7e971ce77822056ba7a155f85c11&p=1&w={}",
w
);
let form = [
("viewstate", viewstate.as_str()),
("secret_key", secret_key.as_str()),
("e", &ekspedisi),
("noresi", &noresi),
("timers", &timers),
];
let resp = client.post(&post_url)
.header(USER_AGENT, "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36")
.header("referer", "https://cekresi.com/")
.header("origin", "https://cekresi.com")
.form(&form)
.send().await.map_err(|e| format!("HTTP: {}", e))?;
let body = resp.text().await.map_err(|e| format!("Body: {}", e))?;
let doc2 = Html::parse_document(&body);
// 3. Parse results
let alert_sel = Selector::parse(".alert.alert-success").unwrap();
let success = doc2.select(&alert_sel).next().is_some();
if success {
let message = doc2
.select(&alert_sel)
.next()
.map(|e| e.text().collect::<String>().trim().to_string())
.unwrap_or_default();
let table_sel = Selector::parse("table.table-striped tbody tr").unwrap();
let mut status = String::new();
let mut tanggal = String::new();
for row in doc2.select(&table_sel) {
let tds: Vec<_> = row.select(&Selector::parse("td").unwrap()).collect();
if tds.len() >= 3 {
let label = tds[0].text().collect::<String>().trim().to_string();
let value = tds[2].text().collect::<String>().trim().to_string();
if label == "Tanggal Pengiriman" {
tanggal = value;
} else if label == "Status" {
status = value;
}
}
}
let last_pos = Selector::parse("#last_position").unwrap();
let last_position = doc2
.select(&last_pos)
.next()
.map(|e| e.text().collect::<String>().trim().to_string())
.unwrap_or_default();
// History: scan all tables for the one whose rows have exactly 2 cells
// (tanggal, keterangan). The status table above uses 3 cells, so the
// 2-cell table after the "History" heading is unambiguous.
let mut history: Vec<Value> = Vec::new();
let table_sel_all = Selector::parse("table").unwrap();
let td_sel = Selector::parse("td").unwrap();
for table in doc2.select(&table_sel_all) {
let mut rows: Vec<Value> = Vec::new();
for tr in table.select(&Selector::parse("tbody tr").unwrap()) {
let tds: Vec<_> = tr.select(&td_sel).collect();
if tds.len() == 2 {
rows.push(json!({
"tanggal": tds[0].text().collect::<String>().trim(),
"keterangan": tds[1].text().collect::<String>().trim(),
}));
}
}
// A table with 2-cell rows is the history table; skip its header row.
if rows.len() > 1 {
history = rows.into_iter().skip(1).collect();
break;
}
}
Ok(json!({
"success": true,
"message": message,
"data": {
"resi": noresi,
"ekspedisi": doc2.select(&Selector::parse("#nama_expedisi").unwrap()).next()
.map(|e| e.text().collect::<String>().trim().to_string()).unwrap_or_default(),
"status": status,
"tanggalKirim": tanggal,
"lastPosition": last_position,
"history": history,
}
}))
} else {
let alert_err = Selector::parse(".alert.alert-danger, .alert.alert-warning").unwrap();
let message = doc2
.select(&alert_err)
.next()
.map(|e| e.text().collect::<String>().trim().to_string())
.unwrap_or_else(|| "Tidak dapat mengambil informasi resi".to_string());
Ok(json!({"success": false, "message": message}))
}
}
+5 -1
View File
@@ -169,7 +169,11 @@ fn join_names(v: Option<&Value>) -> String {
v.and_then(|g| g.as_array()) v.and_then(|g| g.as_array())
.map(|arr| { .map(|arr| {
arr.iter() arr.iter()
.filter_map(|g| g.get("name").and_then(|n| n.as_str()).map(|s| s.to_string())) .filter_map(|g| {
g.get("name")
.and_then(|n| n.as_str())
.map(|s| s.to_string())
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", ") .join(", ")
}) })
+41
View File
@@ -0,0 +1,41 @@
//! Axum handlers for image generation endpoints.
//!
//! Returns raw image bytes with the proper Content-Type.
use axum::body::Body;
use axum::extract::Query;
use axum::http::{header, StatusCode};
use axum::response::Response;
use serde::Deserialize;
use crate::application::image as use_cases;
use crate::presentation::error::AppError;
#[derive(Deserialize)]
pub struct BratParams {
pub text: Option<String>,
}
pub async fn brat_handler(Query(params): Query<BratParams>) -> Result<Response, AppError> {
let text = params
.text
.ok_or_else(|| AppError::BadRequest("text parameter is required".into()))?;
let bytes = use_cases::brat(&text).await?;
Ok(Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "image/png")
.body(Body::from(bytes))
.expect("valid response"))
}
pub async fn brat_animated_handler(Query(params): Query<BratParams>) -> Result<Response, AppError> {
let text = params
.text
.ok_or_else(|| AppError::BadRequest("text parameter is required".into()))?;
let bytes = use_cases::brat_animated(&text).await?;
Ok(Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "image/gif")
.body(Body::from(bytes))
.expect("valid response"))
}
+3
View File
@@ -2,8 +2,11 @@ pub mod anime;
pub mod anime2; pub mod anime2;
pub mod downloader; pub mod downloader;
pub mod health; pub mod health;
pub mod image;
pub mod komik; pub mod komik;
pub mod misc; pub mod misc;
pub mod proxy; pub mod proxy;
pub mod search;
pub mod stalk; pub mod stalk;
pub mod tools;
pub mod weebs; pub mod weebs;
+62
View File
@@ -0,0 +1,62 @@
//! Axum handlers for search utilities.
//!
//! Ported from Shirokami-API `scraper/search/*.js`.
use axum::extract::Query;
use axum::http::StatusCode;
use axum::Json;
use serde::Deserialize;
use serde_json::Value;
use crate::application::search as use_cases;
use crate::presentation::error::AppError;
#[derive(Deserialize)]
pub struct SearchParams {
pub query: Option<String>,
pub q: Option<String>,
pub kota: Option<String>,
pub city: Option<String>,
}
fn require<'a>(v: Option<&'a String>, name: &str) -> Result<String, AppError> {
v.cloned()
.ok_or_else(|| AppError::BadRequest(format!("Missing '{}' parameter", name)))
}
/// GET /search/bmkg
pub async fn bmkg_handler() -> Result<(StatusCode, Json<Value>), AppError> {
Ok((StatusCode::OK, Json(use_cases::bmkg().await?)))
}
/// GET /search/jadwal-sholat?kota=jakarta
pub async fn jadwal_sholat_handler(
Query(p): Query<SearchParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let kota = require(p.kota.as_ref(), "kota")?;
Ok((StatusCode::OK, Json(use_cases::jadwal_sholat(&kota).await?)))
}
/// GET /search/weather?city=jakarta
pub async fn weather_handler(
Query(p): Query<SearchParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let city = require(p.city.as_ref().or(p.q.as_ref()), "city")?;
Ok((StatusCode::OK, Json(use_cases::weather(&city).await?)))
}
/// GET /search/google?query=rust
pub async fn google_handler(
Query(p): Query<SearchParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let query = require(p.query.as_ref().or(p.q.as_ref()), "query")?;
Ok((StatusCode::OK, Json(use_cases::google(&query).await?)))
}
/// GET /search/yt?query=rickroll
pub async fn yt_handler(
Query(p): Query<SearchParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let query = require(p.query.as_ref().or(p.q.as_ref()), "query")?;
Ok((StatusCode::OK, Json(use_cases::yt_search(&query).await?)))
}
+85
View File
@@ -0,0 +1,85 @@
//! Axum handlers for tools.
use axum::extract::Query;
use axum::http::StatusCode;
use axum::Json;
use serde::Deserialize;
use serde_json::Value;
use crate::application::tools as use_cases;
use crate::presentation::error::AppError;
#[derive(Deserialize)]
pub struct DomainParams {
pub domain: Option<String>,
}
#[derive(Deserialize)]
pub struct IpParams {
pub ip: Option<String>,
}
#[derive(Deserialize)]
pub struct UrlParams {
pub url: Option<String>,
}
#[derive(Deserialize)]
pub struct ResiParams {
pub resi: Option<String>,
pub ekspedisi: Option<String>,
}
pub async fn whois_handler(
Query(params): Query<DomainParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let domain = params
.domain
.ok_or_else(|| AppError::BadRequest("domain parameter is required".into()))?;
Ok((StatusCode::OK, Json(use_cases::whois(&domain).await?)))
}
pub async fn ip_location_handler(
Query(params): Query<IpParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let ip = params
.ip
.ok_or_else(|| AppError::BadRequest("ip parameter is required".into()))?;
Ok((StatusCode::OK, Json(use_cases::ip_location(&ip).await?)))
}
pub async fn tinyurl_handler(
Query(params): Query<UrlParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let url = params
.url
.ok_or_else(|| AppError::BadRequest("url parameter is required".into()))?;
Ok((StatusCode::OK, Json(use_cases::tinyurl(&url).await?)))
}
pub async fn check_hosting_handler(
Query(params): Query<DomainParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let domain = params
.domain
.ok_or_else(|| AppError::BadRequest("domain parameter is required".into()))?;
Ok((
StatusCode::OK,
Json(use_cases::check_hosting(&domain).await?),
))
}
pub async fn hargapangan_handler() -> Result<(StatusCode, Json<Value>), AppError> {
Ok((StatusCode::OK, Json(use_cases::hargapangan().await?)))
}
pub async fn cek_resi_handler(
Query(params): Query<ResiParams>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let ResiParams { resi, ekspedisi } = params;
let resi = resi.ok_or_else(|| AppError::BadRequest("resi parameter is required".into()))?;
Ok((
StatusCode::OK,
Json(use_cases::cek_resi(resi, ekspedisi).await?),
))
}
+55
View File
@@ -303,6 +303,61 @@ pub fn build_router(app_state: Arc<AppState>) -> anyhow::Result<Router> {
"/weebs/nsfw-waifu", "/weebs/nsfw-waifu",
axum::routing::get(crate::presentation::handler::weebs::nsfw_waifu_handler), axum::routing::get(crate::presentation::handler::weebs::nsfw_waifu_handler),
) )
// Search routes
.route(
"/search/bmkg",
axum::routing::get(crate::presentation::handler::search::bmkg_handler),
)
.route(
"/search/jadwal-sholat",
axum::routing::get(crate::presentation::handler::search::jadwal_sholat_handler),
)
.route(
"/search/weather",
axum::routing::get(crate::presentation::handler::search::weather_handler),
)
.route(
"/search/google",
axum::routing::get(crate::presentation::handler::search::google_handler),
)
.route(
"/search/yt",
axum::routing::get(crate::presentation::handler::search::yt_handler),
)
// Tool routes
.route(
"/tool/whois",
axum::routing::get(crate::presentation::handler::tools::whois_handler),
)
.route(
"/tool/iplocation",
axum::routing::get(crate::presentation::handler::tools::ip_location_handler),
)
.route(
"/tool/tinyurl",
axum::routing::get(crate::presentation::handler::tools::tinyurl_handler),
)
.route(
"/tool/check-hosting",
axum::routing::get(crate::presentation::handler::tools::check_hosting_handler),
)
.route(
"/tool/hargapangan",
axum::routing::get(crate::presentation::handler::tools::hargapangan_handler),
)
.route(
"/tool/cek-resi",
axum::routing::get(crate::presentation::handler::tools::cek_resi_handler),
)
// Image routes
.route(
"/image/brat",
axum::routing::get(crate::presentation::handler::image::brat_handler),
)
.route(
"/image/brat/animated",
axum::routing::get(crate::presentation::handler::image::brat_animated_handler),
)
// Health // Health
.route( .route(
"/health", "/health",