| 1 | use std::sync::Arc; |
| 2 | use std::time::Instant; |
| 3 | |
| 4 | use graph::Graph; |
| 5 | use session::{DEFAULT_ENDPOINT, Session}; |
| 6 | use store::Store; |
| 7 | use teams_core::{SyncEngine, images}; |
| 8 | |
| 9 | const CHATS_TO_SCAN: usize = 30; |
| 10 | const IMAGES_TO_FETCH: usize = 2; |
| 11 | |
| 12 | #[tokio::main] |
| 13 | async fn main() { |
| 14 | let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned()); |
| 15 | let session = Session::connect(&endpoint).await.expect("browser"); |
| 16 | let engine = SyncEngine::new( |
| 17 | Graph::new(session), |
| 18 | Arc::new(Store::open_in_memory().unwrap()), |
| 19 | ); |
| 20 | engine.refresh_sidebar().await.expect("sidebar"); |
| 21 | let chats = engine.sidebar().unwrap().chats; |
| 22 | let mut scanned = 0; |
| 23 | let mut messages = 0; |
| 24 | let mut with_images = Vec::new(); |
| 25 | for chat in chats.iter().take(CHATS_TO_SCAN) { |
| 26 | if engine.fetch_newer(&chat.id).await.is_err() { |
| 27 | continue; |
| 28 | } |
| 29 | scanned += 1; |
| 30 | for record in engine.open_conversation(&chat.id).unwrap() { |
| 31 | messages += 1; |
| 32 | with_images.extend(images(&record)); |
| 33 | } |
| 34 | } |
| 35 | let graph_hosted = with_images |
| 36 | .iter() |
| 37 | .filter(|image| image.url.starts_with("https://graph.microsoft.com/")) |
| 38 | .count(); |
| 39 | println!( |
| 40 | "chats scanned {scanned} messages {messages} images {} graph-hosted {graph_hosted}", |
| 41 | with_images.len() |
| 42 | ); |
| 43 | let mut fetched = 0; |
| 44 | let mut total_bytes = 0; |
| 45 | let started = Instant::now(); |
| 46 | for image in with_images |
| 47 | .iter() |
| 48 | .filter(|image| image.url.starts_with("https://graph.microsoft.com/")) |
| 49 | .take(IMAGES_TO_FETCH) |
| 50 | { |
| 51 | let outcome = engine.fetch_image(image).await; |
| 52 | match (outcome, engine.image(image.key())) { |
| 53 | (Ok(_), Some(stored)) => { |
| 54 | fetched += 1; |
| 55 | total_bytes += stored.bytes.len(); |
| 56 | } |
| 57 | (Err(error), _) => println!( |
| 58 | "fetch failed: {}", |
| 59 | error.to_string().chars().take(60).collect::<String>() |
| 60 | ), |
| 61 | _ => println!("fetch returned nothing"), |
| 62 | } |
| 63 | } |
| 64 | println!( |
| 65 | "fetched {fetched} images, {total_bytes} bytes, {} ms", |
| 66 | started.elapsed().as_millis() |
| 67 | ); |
| 68 | } |