1use baidu_netdisk_sdk::{BaiduNetDiskClient, Category, CategorySearchOptions};
2use log::info;
3use tokio::time::{sleep, Duration};
4
5async fn wait_for_rate_limit() {
6 sleep(Duration::from_millis(500)).await;
7}
8
9#[tokio::main]
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
12
13 let client = BaiduNetDiskClient::builder().build()?;
14 info!("Client created successfully");
15
16 client.load_token_from_env()?;
17 info!("Token loaded successfully");
18
19 let test_dir = "/apps/product";
20
21 println!("=== Baidu NetDisk Category Test ===\n");
22
23 println!("Testing directory: {}\n", test_dir);
24
25 println!("=== Part 1: Global Category Counts (All files) ===");
26 println!("----------------------------------------");
27 test_global_counts(&client).await?;
28
29 println!("\n\n=== Part 2: Category Counts in {} ===", test_dir);
30 println!("----------------------------------------");
31 test_directory_counts(&client, &test_dir).await?;
32
33 println!("\n\n=== Part 3: List Files in Each Category ===");
34 println!("----------------------------------------");
35 test_list_category_files(&client, &test_dir).await?;
36
37 println!("\n\n=== Category Test Completed ===");
38
39 Ok(())
40}
41
42async fn test_global_counts(client: &BaiduNetDiskClient) -> Result<(), Box<dyn std::error::Error>> {
43 let categories = [
44 (Category::Video, "Video"),
45 (Category::Music, "Music"),
46 (Category::Image, "Image"),
47 (Category::Document, "Document"),
48 (Category::Application, "Application"),
49 (Category::Other, "Other"),
50 (Category::Torrent, "Torrent"),
51 ];
52
53 for (category, name) in categories {
54 let options = CategorySearchOptions::new()
55 .parent_path("/")
56 .recursion(1)
57 .limit(1);
58
59 match client
60 .file()
61 .search_category_files_with_options(&category.as_u32().to_string(), options)
62 .await
63 {
64 Ok((_, total)) => {
65 println!(" {}: {} files", name, total);
66 }
67 Err(e) => {
68 println!(" {}: Error - {}", name, e);
69 }
70 }
71
72 wait_for_rate_limit().await;
73 }
74
75 Ok(())
76}
77
78async fn test_directory_counts(
79 client: &BaiduNetDiskClient,
80 dir: &str,
81) -> Result<(), Box<dyn std::error::Error>> {
82 let categories = [
83 (Category::Video, "Video"),
84 (Category::Music, "Music"),
85 (Category::Image, "Image"),
86 (Category::Document, "Document"),
87 (Category::Application, "Application"),
88 (Category::Other, "Other"),
89 (Category::Torrent, "Torrent"),
90 ];
91
92 for (category, name) in categories {
93 let options = CategorySearchOptions::new()
94 .parent_path(dir)
95 .recursion(1)
96 .limit(1);
97
98 match client
99 .file()
100 .search_category_files_with_options(&category.as_u32().to_string(), options)
101 .await
102 {
103 Ok((_, total)) => {
104 println!(" {}: {} files", name, total);
105 }
106 Err(e) => {
107 println!(" {}: Error - {}", name, e);
108 }
109 }
110
111 wait_for_rate_limit().await;
112 }
113
114 Ok(())
115}
116
117async fn test_list_category_files(
118 client: &BaiduNetDiskClient,
119 dir: &str,
120) -> Result<(), Box<dyn std::error::Error>> {
121 let categories = [
122 (1, "Video"),
123 (2, "Music"),
124 (3, "Image"),
125 (4, "Document"),
126 (5, "Application"),
127 (6, "Other"),
128 (7, "Torrent"),
129 ];
130
131 for (category, name) in categories {
132 println!("\n [{}] Files:", name);
133
134 let options = CategorySearchOptions::new()
135 .parent_path(dir)
136 .recursion(1)
137 .limit(10);
138
139 match client
140 .file()
141 .search_category_files_with_options(&category.to_string(), options)
142 .await
143 {
144 Ok((files, total)) => {
145 if files.is_empty() {
146 println!(" (no files)");
147 } else {
148 for file in files.iter().take(5) {
149 let size_str = file
150 .size
151 .map(|s| format!("{:.2} MB", s as f64 / (1024.0 * 1024.0)))
152 .unwrap_or_else(|| "N/A".to_string());
153 println!(" - {} ({})", file.name, size_str);
154 }
155 if files.len() > 5 && total > 5 {
156 println!(" ... and {} more files", total - 5);
157 }
158 println!(" Total in this category: {}", total);
159 }
160 }
161 Err(e) => {
162 println!(" Error - {}", e);
163 }
164 }
165
166 wait_for_rate_limit().await;
167 }
168
169 Ok(())
170}