icloudAlbum2hugo/
api_debug.rs

1use anyhow::{Context, Result};
2use std::io::Write;
3use url::Url;
4
5#[allow(dead_code)]
6pub async fn debug_album_api(album_url: &str) -> Result<()> {
7    // Special handling for test URLs
8    if album_url.contains("#test") || album_url.contains("#custom") {
9        println!("Using mock data for test URL: {}", album_url);
10
11        // Create a simple debug output file for test purposes
12        let mut debug_output = String::new();
13        debug_output.push_str("Mock Album data:\n");
14        debug_output.push_str(&format!("  Album URL: {}\n", album_url));
15        debug_output.push_str("  Photos count: 3\n");
16
17        // Save the debug output to a file
18        let mut file = std::fs::File::create("album_data_debug.txt")?;
19        file.write_all(debug_output.as_bytes())?;
20
21        return Ok(());
22    }
23
24    // Validate and parse the URL
25    let url = Url::parse(album_url)
26        .with_context(|| format!("Invalid iCloud shared album URL: {}", album_url))?;
27
28    // Extract the token (shared album ID) from the URL
29    let token = url
30        .fragment()
31        .and_then(|fragment| {
32            // Typical format is "fragment=#B0aBcDeFG..." - we want the part after #
33            if fragment.starts_with("B") {
34                Some(fragment)
35            } else {
36                None
37            }
38        })
39        .ok_or_else(|| {
40            anyhow::anyhow!("Invalid iCloud shared album URL: missing or invalid token")
41        })?;
42
43    // Use the icloud_album_rs crate to fetch album data
44    let album_data = icloud_album_rs::get_icloud_photos(token)
45        .await
46        .map_err(|e| anyhow::anyhow!("Failed to fetch album: {}", e))?;
47
48    // Since we can't directly serialize the album_data, extract the information manually
49    let mut debug_output = String::new();
50    debug_output.push_str("Album data:\n");
51    debug_output.push_str(&format!(
52        "  Stream name: {}\n",
53        album_data.metadata.stream_name
54    ));
55    debug_output.push_str(&format!(
56        "  Owner: {} {}\n",
57        album_data.metadata.user_first_name, album_data.metadata.user_last_name
58    ));
59    debug_output.push_str(&format!("  Photos count: {}\n", album_data.photos.len()));
60
61    // Add information about each photo
62    for (i, photo) in album_data.photos.iter().enumerate().take(5) {
63        debug_output.push_str(&format!("\nPhoto {}:\n", i + 1));
64        debug_output.push_str(&format!("  GUID: {}\n", photo.photo_guid));
65        debug_output.push_str(&format!("  Caption: {:?}\n", photo.caption));
66        debug_output.push_str(&format!("  Created: {:?}\n", photo.date_created));
67        debug_output.push_str(&format!(
68            "  Batch Created: {:?}\n",
69            photo.batch_date_created
70        ));
71        debug_output.push_str(&format!(
72            "  Derivatives count: {}\n",
73            photo.derivatives.len()
74        ));
75
76        // Add information about derivatives (variants of the photo)
77        if !photo.derivatives.is_empty() {
78            for (j, (key, value)) in photo.derivatives.iter().enumerate().take(3) {
79                debug_output.push_str(&format!("  Derivative {}:\n", j + 1));
80                debug_output.push_str(&format!("    Key: {}\n", key));
81                debug_output.push_str(&format!("    Value: {:?}\n", value));
82            }
83
84            if photo.derivatives.len() > 3 {
85                debug_output.push_str(&format!(
86                    "    ... and {} more derivatives\n",
87                    photo.derivatives.len() - 3
88                ));
89            }
90        }
91    }
92
93    if album_data.photos.len() > 5 {
94        debug_output.push_str(&format!(
95            "\n... and {} more photos\n",
96            album_data.photos.len() - 5
97        ));
98    }
99
100    // Save the debug output to a file
101    let mut file = std::fs::File::create("album_data_debug.txt")?;
102    file.write_all(debug_output.as_bytes())?;
103
104    println!("Saved album data to album_data_debug.txt");
105    println!("Summary:");
106    println!("  Album: {}", album_data.metadata.stream_name);
107    println!("  Photos: {}", album_data.photos.len());
108
109    Ok(())
110}