diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-07 17:13:48 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-07 17:13:48 +0200 |
commit | 35096ba8d4b064cb0565e202103af91308dcafcc (patch) | |
tree | dd8aee622b570f15ad5c01d10ce224215869ee35 /src/client.rs | |
parent | 0b96cae3425c8781f5b755d52a81bbc7b8b3ef64 (diff) | |
download | weareearth-35096ba8d4b064cb0565e202103af91308dcafcc.tar weareearth-35096ba8d4b064cb0565e202103af91308dcafcc.tar.bz2 weareearth-35096ba8d4b064cb0565e202103af91308dcafcc.tar.zst |
move client to file
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..2699c26 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,93 @@ +use anyhow::{Result, bail}; +use log::{debug, error, info}; +use prost::{Message, bytes::Bytes}; +use reqwest::{ + Client, + header::{HeaderMap, HeaderName, HeaderValue}, +}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tokio::sync::Semaphore; + +use crate::{ + Flags, + cache::Cache, + proto::{BulkMetadata, NodeData, NodeMetadata, PlanetoidMetadata}, +}; + +pub struct GeClient { + counter: AtomicUsize, + client: Client, + cache: Cache, + par_limit: Semaphore, +} + +impl GeClient { + pub async fn new(par_limit: usize, cache: Cache) -> Result<Self> { + Ok(Self { + counter: AtomicUsize::new(0), + par_limit: Semaphore::new(par_limit),cache, + client: Client::builder().default_headers(HeaderMap::from_iter([ + (HeaderName::from_static("user-agent"), HeaderValue::from_static("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36")), + (HeaderName::from_static("referer"), HeaderValue::from_static("https://earth.google.com/")) + ])).build().unwrap() + }) + } + pub async fn download(&self, path: &str) -> Result<Bytes> { + let _permit = self.par_limit.acquire().await?; + if let Some(d) = self.cache.get(path)? { + debug!("cached {path:?}"); + Ok(d.into()) + } else { + let n = self.counter.fetch_add(1, Ordering::Relaxed); + info!("download #{n} {path:?}"); + let res = self + .client + .get(format!("https://kh.google.com/rt/earth/{path}")) + .send() + .await?; + if !res.status().is_success() { + error!("error response: {:?}", res.text().await?); + bail!("error response") + } + let buf = res.bytes().await?; + self.cache.insert(path, &buf)?; + Ok(buf) + } + } + + pub async fn planetoid_metdata(&self) -> Result<PlanetoidMetadata> { + let buf = self.download("PlanetoidMetadata").await?; + Ok(PlanetoidMetadata::decode(buf)?) + } + pub async fn bulk_metadata(&self, path: &str, epoch: u32) -> Result<BulkMetadata> { + let buf = self + .download(&format!("BulkMetadata/pb=!1m2!1s{path}!2u{epoch}")) + .await?; + Ok(BulkMetadata::decode(buf)?) + } + pub async fn node_data( + &self, + abspath: &str, + flags: Flags, + bulk: &BulkMetadata, + node: &NodeMetadata, + ) -> Result<NodeData> { + let texture_format = bulk.default_available_texture_formats(); + let imagery_epoch = node.imagery_epoch.unwrap_or(bulk.default_imagery_epoch()); + let node_epoch = node + .epoch + .unwrap_or(bulk.head_node_key.as_ref().unwrap().epoch.unwrap()); // ? + + let image_epoch_part = if flags.use_image_epoch { + format!("!3u{imagery_epoch}") + } else { + String::new() + }; + let url = format!( + "NodeData/pb=!1m2!1s{abspath}!2u{node_epoch}!2e{texture_format}{image_epoch_part}!4b0" + ); + + let buf = self.download(&url).await?; + Ok(NodeData::decode(buf)?) + } +} |