bunkr-client 0.2.2

A Rust library and CLI tool for uploading files to Bunkr.cr
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#[cfg(feature = "download")]
use crate::TokenResponse;
use crate::core::types::{AlbumFile, FailedOperationInfo};
#[cfg(feature = "download")]
use crate::core::types::DownloadResponse;
use anyhow::{Result, anyhow};
#[cfg(feature = "download")]
use json5;
use std::sync::{Arc, Mutex};
#[cfg(feature = "download")]
use reqwest::{Client, header};
#[cfg(feature = "download")]
use regex::Regex;
#[cfg(feature = "download")]
use serde_json;
#[cfg(feature = "download")]
use std::path::Path;
#[cfg(feature = "download")]
use std::sync::OnceLock;
#[cfg(feature = "download")]
use std::time::Duration;
#[cfg(feature = "download")]
use tokio::io::AsyncWriteExt;
#[cfg(feature = "download")]
use tokio::task::JoinSet;

#[cfg(feature = "ui")]
use crate::ui::ui::UIState;
#[cfg(not(feature = "ui"))]
use crate::core::types::UIState;

#[cfg(not(feature = "ui"))]
impl UIState {
    pub fn add_current_operation(&mut self, _name: String, _progress: f64, _size: u64) {}
    pub fn update_progress(&mut self, _name: &str, _progress: f64) {}
    pub fn remove_current_operation(&mut self, _name: &str, _url: Option<&str>) {}
    pub fn add_failed_operation(&mut self, _name: String, _info: FailedOperationInfo) {}
    pub fn add_processed_bytes(&mut self, _bytes: u64) {}
}

pub struct BunkrDownloader {
    #[cfg(feature = "download")]
    client: Client,
    #[cfg(feature = "download")]
    headers: header::HeaderMap,
    #[cfg(feature = "download")]
    album_files_regex: OnceLock<Regex>,
    #[cfg(feature = "download")]
    trailing_comma_regex: OnceLock<Regex>,
    #[cfg(feature = "download")]
    keys_regex: OnceLock<Regex>,
    #[cfg(feature = "download")]
    id_regex: OnceLock<Regex>,
    #[cfg(feature = "download")]
    orig_regex: OnceLock<Regex>,
}

#[cfg(not(feature = "download"))]
impl BunkrDownloader {
    pub async fn new() -> Result<Self> {
        Err(anyhow!("Download feature is not enabled."))
    }

    pub async fn get_files(&self, _album_url: &str) -> Result<Vec<AlbumFile>> {
        Err(anyhow!("Download feature is not enabled."))
    }

    pub async fn download_file(&self, _file: &AlbumFile, _output_dir: &str, _ui_state: Option<Arc<Mutex<UIState>>>) -> Result<()> {
        Err(anyhow!("Download feature is not enabled."))
    }

    pub async fn download_files(&self, _files: Vec<AlbumFile>, _output_dir: &str, _batch_size: usize, _ui_state: Option<Arc<Mutex<UIState>>>) -> Result<()> {
        Err(anyhow!("Download feature is not enabled."))
    }
}

#[cfg(feature = "download")]
impl BunkrDownloader {
    pub async fn new() -> Result<Self> {
        let client = Client::new();

        let mut headers = header::HeaderMap::new();
        headers.insert("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36".parse()?);
        headers.insert("Accept", "application/json, text/plain, */*".parse()?);
        headers.insert("Accept-Language", "en-US,en;q=0.9".parse()?);
        headers.insert("Content-Type", "application/json".parse()?);
        headers.insert("Origin", "https://bunkr.su".parse()?);

        let album_files_regex = OnceLock::new();
        album_files_regex.get_or_init(|| Regex::new(r"window\.albumFiles\s*=\s*\[\s*([\s\S]*?)\s*\]\s*;").unwrap());

        let trailing_comma_regex = OnceLock::new();
        trailing_comma_regex.get_or_init(|| Regex::new(r",(\s*)([}\]])").unwrap());

        let keys_regex = OnceLock::new();
        keys_regex.get_or_init(|| Regex::new(r"(?m)^(\s*)(\w+)(\s*):").unwrap());

        let id_regex = OnceLock::new();
        id_regex.get_or_init(|| Regex::new(r#"data-file-id="(\d+)""#).unwrap());

        let orig_regex = OnceLock::new();
        orig_regex.get_or_init(|| Regex::new(r#"<h1 class="text-subs font-semibold text-base sm:text-lg truncate">([^<]+)</h1>"#).unwrap());

        Ok(Self {
            client,
            headers,
            album_files_regex,
            trailing_comma_regex,
            keys_regex,
            id_regex,
            orig_regex,
        })
    }

    pub async fn get_files(&self, album_url: &str) -> Result<Vec<AlbumFile>> {
        if album_url.contains("/a/") {
            self.get_album_files(album_url).await
        } else if album_url.contains("/f/") {
            let file = self.get_single_file(album_url).await?;
            Ok(vec![file])
        } else {
            Err(anyhow!("Unsupported URL: {}", album_url))
        }
    }

    async fn get_album_files(&self, album_url: &str) -> Result<Vec<AlbumFile>> {
        // Album URL
        // Ensure the URL has advanced=1
        let url = if album_url.contains("?") {
            format!("{}&advanced=1", album_url)
        } else {
            format!("{}?advanced=1", album_url)
        };

        let response = self.client.get(&url).send().await?;
        let html = response.text().await?;

        // Regex to extract the window.albumFiles array
        let re = self.album_files_regex.get().unwrap();

        let captures = re.captures(&html)
            .ok_or_else(|| anyhow!("Could not find albumFiles in the page"))?;

        let js_array = &captures[1];

        // Convert JavaScript object notation to JSON
        let json_str = self.js_to_json(js_array)?;

        // Parse the JS-like array using json5 (supports single quotes, trailing commas, etc.)
        let files: Vec<AlbumFile> = json5::from_str(&json_str)?;

        Ok(files)
    }

    async fn get_single_file(&self, file_url: &str) -> Result<AlbumFile> {
        // Individual file URL
        let response = self.client.get(file_url).send().await?;
        let html = response.text().await?;

        // Extract file id from <div id="fileTracker" data-file-id="...">
        let id_re = self.id_regex.get().unwrap();
        let id: i64 = id_re.captures(&html)
            .and_then(|c| c[1].parse().ok())
            .ok_or_else(|| anyhow!("Could not find file id"))?;

        // Extract original filename from <h1 class="text-subs font-semibold text-base sm:text-lg truncate">
        let orig_re = self.orig_regex.get().unwrap();
        let original = orig_re.captures(&html)
            .and_then(|c| Some(c[1].to_string()))
            .ok_or_else(|| anyhow!("Could not find file name"))?;

        // Create AlbumFile with extracted data, defaults for others
        let file = AlbumFile {
            id,
            name: original.clone(),
            original,
            slug: "".to_string(),
            file_type: "".to_string(),
            extension: "".to_string(),
            size: 0,
            timestamp: "".to_string(),
            thumbnail: "".to_string(),
            cdn_endpoint: "".to_string(),
        };

        Ok(file)
    }

    fn js_to_json(&self, js_str: &str) -> Result<String> {
        // Replace JavaScript object syntax with JSON
        let mut json = js_str.to_string();

        // Remove trailing commas before closing braces/brackets
        let re_trailing_comma = self.trailing_comma_regex.get().unwrap();
        json = re_trailing_comma.replace_all(&json, "$2").to_string();

        // Replace unquoted keys with quoted keys
        // This is a simple approach; for more complex cases, a proper JS parser might be needed
        let re_keys = self.keys_regex.get().unwrap();
        json = re_keys.replace_all(&json, "$1\"$2\"$3:").to_string();

        // Wrap in array brackets if not already
        let json = format!("[{}]", json);

        Ok(json)
    }

    pub async fn download_file(&self, file: &AlbumFile, output_dir: &str, ui_state: Option<Arc<Mutex<UIState>>>) -> Result<()> {
        Self::download_file_owned(
            self.client.clone(),
            self.headers.clone(),
            Self::owned_album_file(file),
            output_dir.to_string(),
            ui_state,
        ).await
    }

    fn owned_album_file(file: &AlbumFile) -> AlbumFile {
        AlbumFile {
            id: file.id,
            name: file.name.clone(),
            original: file.original.clone(),
            slug: file.slug.clone(),
            file_type: file.file_type.clone(),
            extension: file.extension.clone(),
            size: file.size,
            timestamp: file.timestamp.clone(),
            thumbnail: file.thumbnail.clone(),
            cdn_endpoint: file.cdn_endpoint.clone(),
        }
    }

    async fn fetch_json<T: serde::de::DeserializeOwned>(
        client: &Client,
        method: reqwest::Method,
        url: &str,
        headers: Option<&header::HeaderMap>,
        body: Option<serde_json::Value>,
        label: &str,
    ) -> Result<T> {
        let mut last_error = None;
        for attempt in 0..3 {
            if attempt > 0 {
                tokio::time::sleep(Duration::from_millis(500 * (1 << attempt))).await;
            }
            let mut builder = client.request(method.clone(), url);
            if let Some(h) = headers {
                builder = builder.headers(h.clone());
            }
            if let Some(ref b) = body {
                builder = builder.json(b);
            }
            match builder.send().await {
                Ok(resp) => {
                    let status = resp.status();
                    let body_text = resp.text().await?;
                    if !status.is_success() {
                        last_error = Some(anyhow!(
                            "{} API returned status {}: {}",
                            label, status, &body_text[..body_text.len().min(200)]
                        ));
                        continue;
                    }
                    match serde_json::from_str::<T>(&body_text) {
                        Ok(val) => return Ok(val),
                        Err(e) => {
                            last_error = Some(anyhow!(
                                "Failed to parse {} response (status {}): {} - body: {}",
                                label, status, e, &body_text[..body_text.len().min(200)]
                            ));
                            continue;
                        }
                    }
                }
                Err(e) => {
                    last_error = Some(anyhow!("{} request failed: {}", label, e));
                    continue;
                }
            }
        }
        Err(anyhow!(
            "{} failed after 3 retries: {}",
            label,
            last_error.unwrap()
        ))
    }

    async fn download_file_owned(
        client: Client,
        headers: header::HeaderMap,
        file: AlbumFile,
        output_dir: String,
        ui_state: Option<Arc<Mutex<UIState>>>,
    ) -> Result<()> {
        let file_path = Path::new(&output_dir).join(&file.original);
        if file_path.exists() {
            return Ok(());
        }

        let api_url = "https://dl.bunkr.cr/api/_001_v2";

        let mut last_error = None;
        for attempt in 0..3 {
            if attempt > 0 {
                tokio::time::sleep(Duration::from_millis(1000 * (1 << attempt))).await;
            }

            let body = serde_json::json!({ "id": file.id.to_string() });

            let download_resp = match Self::fetch_json::<DownloadResponse>(
                &client,
                reqwest::Method::POST,
                api_url,
                Some(&headers),
                Some(body),
                "download_url",
            ).await {
                Ok(r) => r,
                Err(e) => { last_error = Some(e); continue; }
            };

            let token_url = format!("https://glb-apisign.cdn.cr/sign?path={}", download_resp.path);

            let token_resp = match Self::fetch_json::<TokenResponse>(
                &client,
                reqwest::Method::GET,
                &token_url,
                None,
                None,
                "token",
            ).await {
                Ok(r) => r,
                Err(e) => { last_error = Some(e); continue; }
            };

            let full_url = format!("{}{}?n={}&token={}&ex={}", download_resp.mediafiles, download_resp.path, download_resp.original, token_resp.token, token_resp.ex);

            let mut download_headers = header::HeaderMap::new();
            download_headers.insert("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:151.0) Gecko/20100101 Firefox/151.0".parse()?);
            download_headers.insert("Accept", "*/*".parse()?);
            download_headers.insert("Accept-Language", "en-US,en;q=0.5".parse()?);
            download_headers.insert("Referer", "https://dl.bunkrr.cr/".parse()?);

            let response = match client.get(&full_url).headers(download_headers).send().await {
                Ok(r) => r,
                Err(e) => { last_error = Some(anyhow!("{}", e)); continue; }
            };

            if !response.status().is_success() {
                last_error = Some(anyhow!("Failed to download file: {}", response.status()));
                continue;
            }

            let total_size = response.content_length().unwrap_or(file.size as u64);
            let mut downloaded = 0u64;

            match tokio::fs::File::create(&file_path).await {
                Ok(mut file_handle) => {
                    let result: Result<()> = async {
                        let mut stream = response;
                        while let Some(chunk) = stream.chunk().await.map_err(|e| anyhow!("{}", e))? {
                            file_handle.write_all(&chunk).await?;
                            downloaded += chunk.len() as u64;

                            if let Some(ref state) = ui_state {
                                let mut state = state.lock().unwrap();
                                let progress = if total_size > 0 { (downloaded as f64 / total_size as f64).min(1.0) } else { 0.0 };
                                state.update_progress(&file.original, progress);
                                state.add_processed_bytes(chunk.len() as u64);
                            }
                        }
                        Ok(())
                    }.await;

                    match result {
                        Ok(()) => return Ok(()),
                        Err(e) => {
                            let _ = tokio::fs::remove_file(&file_path).await;
                            last_error = Some(e);
                            continue;
                        }
                    }
                }
                Err(e) => {
                    last_error = Some(anyhow!("Failed to create file: {}", e));
                    continue;
                }
            }
        }

        Err(anyhow!(
            "Download failed after 3 retries: {}",
            last_error.unwrap()
        ))
    }

    pub async fn download_files(&self, files: Vec<AlbumFile>, output_dir: &str, batch_size: usize, ui_state: Option<Arc<Mutex<UIState>>>) -> Result<()> {
        let batch_size = batch_size.max(1);
        let output_dir = output_dir.to_string();
        let client = self.client.clone();
        let headers = self.headers.clone();

        let mut files_iter = files.into_iter();
        let mut join_set = JoinSet::new();

        let mut spawn_next = |join_set: &mut JoinSet<(AlbumFile, Result<()>)>| {
            let Some(file) = files_iter.next() else {
                return;
            };

            let client = client.clone();
            let headers = headers.clone();
            let output_dir = output_dir.clone();
            let ui_state = ui_state.clone();

            join_set.spawn(async move {
                if let Some(ref state) = ui_state {
                    let mut state = state.lock().unwrap();
                    state.add_current_operation(file.original.clone(), 0.0, file.size as u64);
                }

                let file_for_result = BunkrDownloader::owned_album_file(&file);
                let result = BunkrDownloader::download_file_owned(client, headers, file, output_dir, ui_state.clone()).await;
                (file_for_result, result)
            });
        };

        for _ in 0..batch_size {
            spawn_next(&mut join_set);
        }

        while let Some(result) = join_set.join_next().await {
            if let Ok((file, result)) = result {
                match result {
                    Ok(_) => {
                        if let Some(ref state) = ui_state {
                            let mut state = state.lock().unwrap();
                            state.remove_current_operation(&file.original, None);
                        }
                    }
                    Err(e) => {
                        if let Some(ref state) = ui_state {
                            let mut state = state.lock().unwrap();
                            let info = FailedOperationInfo {
                                path: file.original.clone(),
                                error: e.to_string(),
                                file_size: file.size as u64,
                                status_code: None,
                            };
                            state.add_failed_operation(file.original.clone(), info);
                        }
                    }
                }
            }

            spawn_next(&mut join_set);
        }

        Ok(())
    }
}