-
-
Save mu-suwi/53fd3ca7a3911e86650ed1ecac1d48a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env nu | |
| # MuSuwi.Mom Upload - Ms. Mu | |
| # | |
| # a shell-script to automate the multi-step process of uploading art to | |
| # my website musuwi.mom | |
| let data_dir = "<path to local data directory>" | |
| let remote_data_dir = "<path to server data directory>" | |
| let msm_ip = "<username>@<server ip>" | |
| let msm_key = "~/.ssh/[DATA EXPUNGED]" | |
| def main [...files: string] { | |
| if ($files | all {|x| $x ends-with ".md" or $x ends-with ".html"}) { | |
| # blog post(s) | |
| $files | each {|file| process-post $file} | |
| } else if ($files | all {|x| $x ends-with ".png"}) { | |
| # art post with image(s) | |
| process-art $files | |
| } else { | |
| echo "looking for .md or .html files or batches of .png files" | |
| } | |
| } | |
| def process-art [files: list] { | |
| # structure the kdl | |
| let ktable = { | |
| #current unix time makes an awesome maximum-lazy sequential id | |
| id: (date now | into int | $in // 10_000_000_000), | |
| art: ( | |
| $files | each {|i| | |
| imgcat -Hw 40 ($i | path expand); | |
| $i | path basename | |
| } | |
| ), | |
| tags: ( | |
| print "enter TAGS for this upload (space separated):"; | |
| input | |
| ), | |
| title: ( | |
| print "enter TITLE for this upload:"; | |
| input | |
| ), | |
| description: ( | |
| print "enter DESCRIPTION for this upload:"; | |
| multiline | |
| ), | |
| date: ( | |
| print $"enter DATE \(or enter for (date now | format date "%Y-%m-%d")\):"; | |
| input --default (date now | format date "%Y-%m-%d") | |
| ), | |
| } | |
| # append to the kdl file | |
| art-kdl $ktable | save --append ($data_dir ++ "art.kdl") | |
| # copy files to local data dir + generate thumbnails | |
| $files | each {|i| | |
| cp ($i | path expand) ($data_dir ++ "art/full/") | |
| fairy-magick ($i | path basename) | |
| } | |
| # the scary phase (uploading) | |
| $files | path basename | each {|i| | |
| [("art/full/" ++ $i) ("art/mids/" ++ $i) ("art/thumbs/" ++ $i)] | |
| } | flatten | append "art.kdl" | each {|file| | |
| (scp -i $msm_key -P 2022 | |
| ($data_dir ++ $file) | |
| ($msm_ip ++ ":" ++ $remote_data_dir ++ $file)) | |
| } | |
| } | |
| def process-post [files: list] { | |
| # structure the kdl | |
| let ktable = { | |
| id: (date now | into int | $in // 10_000_000_000), | |
| filename: ($files | path basename) | |
| title: ( | |
| print "enter TITLE for this upload:"; | |
| input | |
| ), | |
| tags: ( | |
| print "enter TAGS for this upload (space separated):"; | |
| input | |
| ), | |
| mood: ( | |
| print "what's your mood?"; | |
| input | |
| ), | |
| date: ( | |
| print $"enter DATE \(or enter for (date now | format date "%Y-%m-%d")\):"; | |
| input --default (date now | format date "%Y-%m-%d") | |
| ), | |
| } | |
| # append to the kdl file | |
| post-kdl $ktable | save --append ($data_dir ++ "posts.kdl") | |
| # copy files to local data dir | |
| $files | path expand | each {|i| cp $i ($data_dir ++ "posts/") } | |
| # the scary phase (uploading) | |
| $files | path basename | each {|i| "posts/" ++ $i } | append "posts.kdl" | |
| | each {|file| | |
| (scp -i $msm_key -P 2022 | |
| ($data_dir ++ $file) | |
| ($msm_ip ++ ":" ++ $remote_data_dir ++ $file)) | |
| } | |
| } | |
| def multiline [] { | |
| let str = input | |
| if $str == "" { | |
| [] | |
| } else { | |
| [$str] | append (multiline) | |
| } | |
| } | |
| def art-kdl [input: record] { | |
| # returns .kdl formatted string based on input record | |
| let goofy_ass_delimiter = "\" \\\n \""; | |
| $" | |
| art ($input.id) ($input.title | str kebab-case) date=\"($input.date)\" { | |
| art ($input.art | str join ' ') | |
| tags ($input.tags) | |
| title \"($input.title)\" | |
| description \\ | |
| \"($input.description | str join $goofy_ass_delimiter)\" | |
| }" | |
| } | |
| def post-kdl [input: record] { | |
| $" | |
| art ($input.id) ($input.file | path parse | get stem) date=\"($input.date)\" \\ | |
| title=\"($input.title)\" mood=\"($input.mood)\" { | |
| tags ($input.tags) | |
| }" | |
| } | |
| def fairy-magick [file] { | |
| # generate midsize and thumbnail size image from full resolution png | |
| cd ($data_dir ++ "art/") | |
| if $file not-in (ls -s ./mids/ | get name) { | |
| print $"generating midsize for ($file)" | |
| (magick ("./full/" ++ $file) | |
| -colorspace rgb | |
| -filter mitchell | |
| -resize 850> | |
| -unsharp 0x0.7071+0.5+0 | |
| -colorspace srgb | |
| ("./mids/" ++ $file)) | |
| } else { | |
| print $"midsize already exists for ($file)" | |
| } | |
| if $file not-in (ls -s ./thumbs/ | get name) { | |
| print $"generating thumbnail for ($file)" | |
| (magick ("./full/" ++ $file) | |
| -colorspace rgb | |
| -filter mitchell | |
| -resize x100 | |
| -unsharp 0x0.7071+0.5+0 | |
| -colorspace srgb | |
| -colors 64 | |
| ("./thumbs/" ++ $file)) | |
| } else { | |
| print $"thumbnail already exists for ($file)" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment