add silly little scripts
This commit is contained in:
parent
e616d19a15
commit
4a9e524f66
8 changed files with 397 additions and 1 deletions
67
scripts/niss-misc/galleryHelpers/make-info
Normal file
67
scripts/niss-misc/galleryHelpers/make-info
Normal file
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env raku
|
||||
|
||||
#| generate gallery info file
|
||||
unit sub MAIN(
|
||||
Str :o($output) = 'info.yaml', #= output file
|
||||
Bool :f($force) = False, #= overwrite an existing file
|
||||
Bool :N($no-edit) = False, #= don't open editor after
|
||||
*@files, #= files to list (default all images except thumb.*)
|
||||
);
|
||||
|
||||
my $cwd = $*CWD.basename;
|
||||
my token isodate {
|
||||
$<date> = [$<y>=[\d**4] '-' $<m>=[\d**2] '-' $<d>=[\d**2]]
|
||||
}
|
||||
|
||||
my $date = $cwd.match(&isodate)<date> // Date.today.yyyy-mm-dd;
|
||||
my $name = $*CWD.basename.subst(/^<isodate> <[\w]>*\-/, '').trans('-' => ' ');
|
||||
|
||||
sub image($path) {
|
||||
qq:to/END/.chomp
|
||||
- path: $path.basename()
|
||||
desc: >
|
||||
...
|
||||
nsfw: false
|
||||
warning: ""
|
||||
END
|
||||
}
|
||||
|
||||
sub is-image($_) { /\. [jpg | png | webp | gif | svg]$/ and not /^thumb\./ }
|
||||
|
||||
my @images = @files ?? @files».IO !! $*CWD.dir(test => &is-image).sort;
|
||||
|
||||
die "no images found in $*CWD" unless @images;
|
||||
|
||||
my $thumb-line = do given $*CWD.dir: test => /^thumb \./ {
|
||||
when ([*]) { "thumb: $_[0].basename()" }
|
||||
default { "" }
|
||||
};
|
||||
|
||||
# spurt 'info.yaml', qq:to/END/;
|
||||
my $yaml = qq:to/END/;
|
||||
date: $date
|
||||
title: $name
|
||||
|
||||
tags: null
|
||||
|
||||
images:
|
||||
{@images.map(&image).join: "\n"}
|
||||
$thumb-line
|
||||
bg: noborder
|
||||
|
||||
desc: |
|
||||
...
|
||||
END
|
||||
|
||||
if $output eq '-' {
|
||||
print $yaml;
|
||||
}
|
||||
elsif $output.IO.e and not $force {
|
||||
say "$output already exists, use -f if you're sure";
|
||||
exit 1;
|
||||
} else {
|
||||
spurt $output, $yaml;
|
||||
}
|
||||
|
||||
my $editor = %*ENV<VISUAL> // %*ENV<EDITOR> // 'nano';
|
||||
run $editor, $output unless $no-edit;
|
147
scripts/niss-misc/galleryHelpers/make-sizes
Normal file
147
scripts/niss-misc/galleryHelpers/make-sizes
Normal file
|
@ -0,0 +1,147 @@
|
|||
#!/usr/bin/env raku
|
||||
|
||||
#| resize images for uploading to gallery sites
|
||||
unit sub MAIN(
|
||||
#| number of concurrent jobs (default min{10, # cores})
|
||||
Int :j(:$jobs) = min(10, $*KERNEL.cpu-cores),
|
||||
#| formats to use for mastodon (png, webp, jpg, default, skip)
|
||||
:m(:$mastodon) = Nil,
|
||||
#| formats to use for furaffinity/weasyl
|
||||
:f(:$furaffinity) = Nil,
|
||||
#| formats to use for gallery.n.w
|
||||
:g(:$gallery) = Nil,
|
||||
#| formats to use for bluesky
|
||||
:b(:$bluesky) = Nil,
|
||||
#| webp/jpg quality setting (default 98)
|
||||
Int :Q(:$quality) = 98,
|
||||
#| only run conversions explicitly mentioned
|
||||
Bool :O(:$only) = False,
|
||||
#| don't actually do anything (implies -v)
|
||||
Bool :n(:$dry-run) = False,
|
||||
#| print every action
|
||||
Bool :v(:$verbose) = False,
|
||||
#| out dir (default ./out)
|
||||
IO(Str) :o(:$outdir) = 'out'.IO,
|
||||
#| output as e.g. thing.gallery.webp instead of gallery/thing.webp
|
||||
Bool :F(:$flat) = False,
|
||||
#| background colour to use for jpg
|
||||
Str :B(:$background) = 'white',
|
||||
#| files to resize (default *.jpg *.png *.webp)
|
||||
*@files,
|
||||
);
|
||||
|
||||
sub cmd(*@args) {
|
||||
say "> @args[]" if $dry-run || $verbose;
|
||||
run @args unless $dry-run;
|
||||
}
|
||||
|
||||
sub verbose(*@args) { say "> @args[]" if $verbose }
|
||||
|
||||
# fixup args
|
||||
|
||||
enum Format <png webp jpg>;
|
||||
|
||||
sub to-formats($_, *@default) {
|
||||
when 'skip' { Empty }
|
||||
when 'default' { @default }
|
||||
when Nil { $only ?? Empty !! @default }
|
||||
when Format { $_ }
|
||||
default {
|
||||
.lc.split(',').map: { Format::{$_} // die "unrecognised format '$_'" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
my @mastodon = to-formats $mastodon, webp;
|
||||
# my @itaku = to-formats $itaku, webp;
|
||||
my @furaffinity = to-formats $furaffinity, png;
|
||||
my @gallery = to-formats $gallery, png,webp;
|
||||
my @bluesky = to-formats $bluesky, jpg;
|
||||
|
||||
die "furaffinity/weasyl don't know what webp are" if webp ∈ @furaffinity;
|
||||
die "at the time of writing, bsky converts everything to jpg"
|
||||
if @bluesky and @bluesky !eqv [jpg];
|
||||
|
||||
if @files { @files .= map(*.IO) }
|
||||
else { @files = $*CWD.dir: test => /:i \.[jpe?g|png|webp]$/ && *.IO.f }
|
||||
for @files { die "$_ is not a regular file" unless .f; }
|
||||
|
||||
|
||||
# running imagemagick (and oxipng sometimes) 🪄
|
||||
|
||||
sub make-filename($in, $base, $format) {
|
||||
$flat ?? $outdir.child($in.basename).extension("$base.$format")
|
||||
!! $outdir.child($base).child($in.basename).extension("$format")
|
||||
}
|
||||
|
||||
|
||||
multi convert($in, @names where .elems >= 1, $size, $format) {
|
||||
my $name = @names[0];
|
||||
my $out = make-filename($in, $name, $format);
|
||||
my @bg-flags = «-background $background -alpha remove»
|
||||
if $format == jpg or $name eq 'bluesky';
|
||||
cmd <mkdir -p>, $out.dirname;
|
||||
cmd <magick>, $in, '-resize', $size, '-quality', $quality, @bg-flags, $out;
|
||||
cmd <oxipng -q>, $out if $format == png;
|
||||
|
||||
for @names[1..*] {
|
||||
my $link = make-filename($in, $_, $format);
|
||||
cmd <mkdir -p>, $link.dirname;
|
||||
cmd <cp -f>, $out, $link;
|
||||
}
|
||||
|
||||
$out
|
||||
}
|
||||
multi convert($in, $name, $size, $format) {
|
||||
convert($in, [$name], $size, $format);
|
||||
}
|
||||
|
||||
sub square($s) { "{$s}x{$s}>" }
|
||||
sub pixels($s) { "{$s}@" }
|
||||
|
||||
my @sites = [
|
||||
{:name«fedi itaku», formats => @mastodon, size => square 2800},
|
||||
{:name<furaffinity>, formats => @furaffinity, size => square 1920},
|
||||
{:name<weasyl>, formats => @furaffinity, size => square 2800},
|
||||
{:name<gallery>, formats => @gallery, size => square 3000},
|
||||
{:name<bluesky>, formats => @bluesky, size => square 2000},
|
||||
];
|
||||
|
||||
|
||||
# go go go
|
||||
|
||||
my @tasks;
|
||||
|
||||
sub task-serial(&body, :$if = True) { body if $if; }
|
||||
|
||||
sub task-parallel(&body, :$if = True) {
|
||||
state $sem = Semaphore.new: $jobs;
|
||||
return unless $if;
|
||||
my $task = start { ENTER $sem.acquire; LEAVE $sem.release; body }
|
||||
@tasks.push: $task;
|
||||
$task
|
||||
}
|
||||
|
||||
constant &task = $dry-run ?? &task-serial !! &task-parallel;
|
||||
|
||||
my @todo = (@files X @sites).map: { hash :file(.[0]), |.[1] }
|
||||
my $len = @todo.map(*.<formats>).sum;
|
||||
my $index;
|
||||
|
||||
for @todo -> (:$file, :$name, :@formats, :$size) {
|
||||
my @outputs = @formats.map: -> $format {
|
||||
my $i = ++$index; my $base = $file.basename;
|
||||
task {
|
||||
say "[$i/$len] $name $base ($format)";
|
||||
convert $file, $name, $size, $format;
|
||||
}
|
||||
}
|
||||
|
||||
task if => !$dry-run && @outputs > 1, {
|
||||
my $keep = @outputs».=result.map({$_ => .s}).min(*.value).key;
|
||||
verbose "# keeping $keep";
|
||||
for @outputs { next when $keep; cmd "rm", $_ }
|
||||
}
|
||||
}
|
||||
|
||||
@tasks».result;
|
Loading…
Add table
Add a link
Reference in a new issue