Skip to content

Commit b15e135

Browse files
author
Alexander Kiselev
committed
some digikey and mouser fixes, plus version bump
1 parent e29e860 commit b15e135

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "datasheet-cli"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2024"
55
rust-version = "1.85"
66

src/digikey.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,34 @@ fn cmd_download(
351351
println!(" URL: {}", datasheet_url);
352352
println!(" Output: {}", output_path.display());
353353

354-
// Download the datasheet
354+
// Download the datasheet with proper headers (distributor CDNs require User-Agent)
355355
let response = ureq::get(datasheet_url)
356+
.set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
357+
.set("Accept", "application/pdf,*/*")
356358
.call()
357359
.map_err(|e| format!("Failed to download datasheet: {}", e))?;
358360

361+
// Verify we got a PDF, not an HTML error/redirect page
362+
let content_type = response.content_type().to_string();
363+
359364
let mut file =
360365
File::create(&output_path).map_err(|e| format!("Failed to create output file: {}", e))?;
361366

362367
let mut reader = response.into_reader();
363-
std::io::copy(&mut reader, &mut file)
368+
let bytes_written = std::io::copy(&mut reader, &mut file)
364369
.map_err(|e| format!("Failed to write datasheet: {}", e))?;
365370

366-
println!("Datasheet downloaded successfully!");
371+
// Check if we got HTML instead of PDF (bot protection / redirect)
372+
if content_type.contains("text/html") || bytes_written < 1024 {
373+
let _ = std::fs::remove_file(&output_path);
374+
return Err(format!(
375+
"Download returned HTML instead of PDF (content-type: {}). \
376+
Distributor may be blocking automated downloads for this URL.",
377+
content_type
378+
));
379+
}
380+
381+
println!("Datasheet downloaded successfully! ({:.1} KB)", bytes_written as f64 / 1024.0);
367382

368383
Ok(())
369384
}

src/mouser.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,19 +335,36 @@ fn cmd_download(
335335
println!(" URL: {}", datasheet_url);
336336
println!(" Output: {}", output_path.display());
337337

338-
// Download the datasheet
338+
// Download the datasheet with proper headers (Mouser CDN requires User-Agent)
339339
let response = ureq::get(datasheet_url)
340+
.set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
341+
.set("Accept", "application/pdf,*/*")
342+
.set("Referer", "https://www.mouser.com/")
340343
.call()
341344
.map_err(|e| format!("Failed to download datasheet: {}", e))?;
342345

346+
// Verify we got a PDF, not an HTML error/redirect page
347+
let content_type = response.content_type().to_string();
348+
343349
let mut file =
344350
File::create(&output_path).map_err(|e| format!("Failed to create output file: {}", e))?;
345351

346352
let mut reader = response.into_reader();
347-
std::io::copy(&mut reader, &mut file)
353+
let bytes_written = std::io::copy(&mut reader, &mut file)
348354
.map_err(|e| format!("Failed to write datasheet: {}", e))?;
349355

350-
println!("Datasheet downloaded successfully!");
356+
// Check if we got HTML instead of PDF (bot protection / redirect)
357+
if content_type.contains("text/html") || bytes_written < 1024 {
358+
// Remove the invalid file
359+
let _ = std::fs::remove_file(&output_path);
360+
return Err(format!(
361+
"Download returned HTML instead of PDF (content-type: {}). \
362+
Mouser may be blocking automated downloads for this URL.",
363+
content_type
364+
));
365+
}
366+
367+
println!("Datasheet downloaded successfully! ({:.1} KB)", bytes_written as f64 / 1024.0);
351368

352369
Ok(())
353370
}

0 commit comments

Comments
 (0)