new changes

This commit is contained in:
Ace
2026-06-23 15:29:34 +02:00
parent c355d0e2e5
commit d51c84997a
+40 -16
View File
@@ -287,8 +287,8 @@ async fn handle_chat(
if has_listing || (has_show && has_job) || (has_show && msg_lower.contains("links")) || msg_lower.contains("recent leads") { if has_listing || (has_show && has_job) || (has_show && msg_lower.contains("links")) || msg_lower.contains("recent leads") {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(); let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
let base_url = "http://localhost:3008/scrape/facebook"; let base_url = "http://localhost:3008/scrape/facebook";
let mut req_builder = state.http_client.post(base_url).timeout(Duration::from_secs(300)); use std::fmt::Write;
let mut service_url = base_url.to_string();
if let Ok(Some((_, path))) = sqlx::query_as::<_, (uuid::Uuid, String)>( if let Ok(Some((_, path))) = sqlx::query_as::<_, (uuid::Uuid, String)>(
"SELECT id, profile_path FROM facebook_accounts \ "SELECT id, profile_path FROM facebook_accounts \
WHERE is_active = TRUE AND flagged = FALSE \ WHERE is_active = TRUE AND flagged = FALSE \
@@ -297,27 +297,48 @@ async fn handle_chat(
.fetch_optional(&state.db) .fetch_optional(&state.db)
.await .await
{ {
req_builder = req_builder.query(&[("profile_path", &path), ("force", &"true".to_string())]); let encoded: String = path.chars().map(|c| match c {
'A'..='Z' | 'a'..='z' | '0'..='9' | '.' | '-' | '_' | '~' => c.to_string(),
_ => format!("%{:02X}", c as u8),
}).collect();
write!(service_url, "?profile_path={}&force=true", encoded).unwrap();
info!("Calling Python scrape at: {}?profile_path=...&force=true", base_url);
} else { } else {
warn!("No active Facebook account found for on-demand scrape"); warn!("No active Facebook account found for on-demand scrape");
} }
let req_builder = state.http_client.post(&service_url);
match req_builder.send().await { match req_builder.send().await {
Ok(resp) => { Ok(resp) => {
if let Ok(scrape_resp) = resp.json::<ScrapeResponse>().await { let status = resp.status();
info!("Scraped {} leads from Facebook", scrape_resp.leads.len()); let body = resp.text().await.unwrap_or_default();
let mut store = state.leads.lock().await; info!("Python scrape response ({}): {} bytes", status, body.len());
for item in &scrape_resp.leads { if body.starts_with('{') {
store.push(Lead { match serde_json::from_str::<ScrapeResponse>(&body) {
title: truncate(&item.title, 120), Ok(scrape_resp) => {
url: item.url.clone(), info!("Scraped {} leads from Facebook", scrape_resp.leads.len());
source: item.source.clone().unwrap_or_else(|| "facebook".to_string()), if scrape_resp.leads.is_empty() && scrape_resp.error.is_some() {
found_at: now, warn!("Python returned error: {:?}", scrape_resp.error);
author: truncate(&item.author, 60), }
date: truncate(&item.date, 30), let mut store = state.leads.lock().await;
content: truncate(&item.content, 300), for item in &scrape_resp.leads {
}); store.push(Lead {
title: truncate(&item.title, 120),
url: item.url.clone(),
source: item.source.clone().unwrap_or_else(|| "facebook".to_string()),
found_at: now,
author: truncate(&item.author, 60),
date: truncate(&item.date, 30),
content: truncate(&item.content, 300),
});
}
}
Err(e) => {
warn!("Failed to parse Python scrape response: {} body: {}", e, &body[..body.len().min(200)]);
}
} }
} else {
warn!("Python returned non-JSON response: {}", &body[..body.len().min(200)]);
} }
} }
Err(e) => { Err(e) => {
@@ -497,6 +518,9 @@ async fn main() {
return; return;
} }
}; };
// Initial delay to let user on-demand requests take priority
tokio::time::sleep(Duration::from_secs(300)).await;
loop { loop {
// 10% random cycle skip — makes pattern non-periodic // 10% random cycle skip — makes pattern non-periodic
if rand::thread_rng().gen_range(0..100) < 10 { if rand::thread_rng().gen_range(0..100) < 10 {