new changes
This commit is contained in:
+28
-4
@@ -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,15 +297,29 @@ 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();
|
||||||
|
let body = resp.text().await.unwrap_or_default();
|
||||||
|
info!("Python scrape response ({}): {} bytes", status, body.len());
|
||||||
|
if body.starts_with('{') {
|
||||||
|
match serde_json::from_str::<ScrapeResponse>(&body) {
|
||||||
|
Ok(scrape_resp) => {
|
||||||
info!("Scraped {} leads from Facebook", scrape_resp.leads.len());
|
info!("Scraped {} leads from Facebook", scrape_resp.leads.len());
|
||||||
|
if scrape_resp.leads.is_empty() && scrape_resp.error.is_some() {
|
||||||
|
warn!("Python returned error: {:?}", scrape_resp.error);
|
||||||
|
}
|
||||||
let mut store = state.leads.lock().await;
|
let mut store = state.leads.lock().await;
|
||||||
for item in &scrape_resp.leads {
|
for item in &scrape_resp.leads {
|
||||||
store.push(Lead {
|
store.push(Lead {
|
||||||
@@ -319,6 +333,13 @@ async fn handle_chat(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) => {
|
||||||
error!("Scraper request error: {} - URL: {}", e, base_url);
|
error!("Scraper request error: {} - URL: {}", e, base_url);
|
||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user