add backup api for globe

This commit is contained in:
dr-frmr 2024-09-18 15:54:33 -04:00
parent 85f22776f1
commit 3ac646a708
No known key found for this signature in database

View File

@ -24,7 +24,7 @@ fn init(_our: Address) {
"User-Agent".to_string(),
"ipapi.co/#rust-v1.5".to_string(),
)])),
60,
10,
vec![],
) {
Ok(response) => match serde_json::from_slice::<serde_json::Value>(response.body()) {
@ -39,8 +39,40 @@ fn init(_our: Address) {
println!("Failed to parse location: {e:?}");
}
},
Err(e) => {
println!("Failed to fetch location: {e:?}");
Err(_) => {
// try other API
match http::client::send_request_await_response(
http::Method::GET,
url::Url::parse("http://ip-api.com/json/").unwrap(),
Some(std::collections::HashMap::from([(
"User-Agent".to_string(),
"curl/7.54.1".to_string(),
)])),
10,
vec![],
) {
Ok(response) => {
match serde_json::from_slice::<serde_json::Value>(response.body()) {
Ok(location) => {
if location.get("lat").is_some() && location.get("lon").is_some() {
let location_json = serde_json::json!({
"latitude": location.get("lat").unwrap(),
"longitude": location.get("lon").unwrap(),
});
break location_json;
} else {
println!("Failed to parse location: {location:?}");
}
}
Err(e) => {
println!("Failed to parse location: {e:?}");
}
}
}
Err(e) => {
println!("Failed to fetch location: {e:?}");
}
}
}
};
std::thread::sleep(std::time::Duration::from_secs(5));