summaryrefslogtreecommitdiff
path: root/.config/nushell/web-images-doc.nu
diff options
context:
space:
mode:
Diffstat (limited to '.config/nushell/web-images-doc.nu')
-rw-r--r--.config/nushell/web-images-doc.nu38
1 files changed, 38 insertions, 0 deletions
diff --git a/.config/nushell/web-images-doc.nu b/.config/nushell/web-images-doc.nu
new file mode 100644
index 0000000..a474003
--- /dev/null
+++ b/.config/nushell/web-images-doc.nu
@@ -0,0 +1,38 @@
+# Download images from a website and merge them to a (PDF) document
+export def main [
+ url: string, # URL of the website
+ --output_filename: string = 'out.pdf', # Output filename, the file type must be compatible with `magick`
+ --query: string = 'img' # Selector query, e.g. to target a class test-img use '.test-img'
+] {
+ let temp_dir = $"temp_($output_filename | split row "." | get 0)"
+ mkdir $temp_dir
+ print $"Created temporary directory ($temp_dir)"
+
+ try {
+ try {
+ http get $url | query web --query $query -m --attribute 'src' | flatten | enumerate | each {|imgsrc|
+ let index = $imgsrc.index + 1
+ let filename = $"($temp_dir)/($index | fill -a 'right' -c '0' -w 5).png"
+ print $"Downloading image ($index)..."
+ http get $imgsrc.item | save $filename
+ print $"Image saved to ($filename)"
+ sleep 1sec
+ }
+ } catch { |err|
+ print $"\nFailed to download images"
+ print $"Error: ($err.msg)"
+ }
+
+ let downloaded_images = (ls $temp_dir | get name | sort)
+ try {
+ magick ...$downloaded_images -border 100x100 $output_filename
+ print $"Success! Created ($output_filename) with ($downloaded_images | length) pages."
+ } catch { |err|
+ print "\nFailed to create PDF. Make sure ImageMagick is installed."
+ print $"Error: ($err.msg)"
+ return
+ }
+ } finally {
+ rm -rf $temp_dir
+ }
+}