BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / examples / cleanup_recordings.rs

cleanup_recordings.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
cleanup_recordings.rs 2.0 KiB Download Raw
1use chrono::{DateTime, Duration, Utc};
2use serde_json::Value;
3use session::{DEFAULT_ENDPOINT, Method, Scope, Session};
4
5const LIST_URL: &str = "https://graph.microsoft.com/v1.0/me/drive/root:/Videos/Recordings:/children?$select=id,name,createdDateTime&$top=50";
6const SUBJECT: &str = "Native client test";
7
8#[tokio::main]
9async fn main() {
10 let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned());
11 let session = Session::connect(&endpoint).await.expect("browser");
12 let scope = Scope::graph("Files.ReadWrite.All");
13 let listed = session
14 .request(Method::Get, LIST_URL, &scope, None)
15 .await
16 .expect("list");
17 println!("# list: HTTP {}", listed.status);
18 let cutoff = Utc::now() - Duration::minutes(60);
19 let items = listed
20 .body
21 .get("value")
22 .and_then(Value::as_array)
23 .cloned()
24 .unwrap_or_default();
25 let mut deleted = 0;
26 for item in items {
27 let name = item.get("name").and_then(Value::as_str).unwrap_or_default();
28 let created = item
29 .get("createdDateTime")
30 .and_then(Value::as_str)
31 .and_then(|text| DateTime::parse_from_rfc3339(text).ok())
32 .map(|time| time.with_timezone(&Utc));
33 let recent = created.is_some_and(|time| time > cutoff);
34 if recent {
35 println!(
36 "# recent recording, starts with test subject: {}",
37 name.starts_with(SUBJECT)
38 );
39 }
40 if recent && name.starts_with(SUBJECT) {
41 let id = item.get("id").and_then(Value::as_str).unwrap_or_default();
42 let url = format!("https://graph.microsoft.com/v1.0/me/drive/items/{id}");
43 let answer = session
44 .request(Method::Delete, &url, &scope, None)
45 .await
46 .expect("delete");
47 println!("# delete recent test recording: HTTP {}", answer.status);
48 deleted += 1;
49 }
50 }
51 println!("# deleted {deleted} test recording(s)");
52}