mirror of
https://github.com/foo-dogsquared/dotfiles.git
synced 2025-01-31 04:57:57 +00:00
Port the update metadata script to Oil
This commit is contained in:
parent
5017a6725c
commit
85603bd00a
@ -1,57 +1,138 @@
|
|||||||
#!/usr/bin/env perl
|
#!/usr/bin/env oil
|
||||||
|
|
||||||
# A quick script on moving a document with the embedded metadata.
|
# A quick script on moving a file with embedded data.
|
||||||
# It uses exiftool for embedding so be sure to have it installed.
|
# It can only detect certain files and write the metadata with a certain program.
|
||||||
# Only needs an argument of a filename to be converted.
|
# For the argument, it needs the path of the files to be moved.
|
||||||
|
|
||||||
# TODO: Create --title (-t) option that automatically skips the title prompt.
|
shopt --set strict:all
|
||||||
# TODO: Create --author (-a) option that automatically skips the author prompt.
|
|
||||||
# TODO: Create --date (-d) option that automatically skips the date prompt.
|
|
||||||
|
|
||||||
use File::Basename;
|
var help_section = "A script for embedding metadata and renaming them files.
|
||||||
use File::Copy;
|
|
||||||
use Modern::Perl;
|
|
||||||
|
|
||||||
# Create a simple prompt returning with the given answer.
|
edit-file-metadata [options...] FILE
|
||||||
sub prompt {
|
|
||||||
my ($msg) = @_;
|
Options:
|
||||||
say $msg;
|
-h, --help Show the help section.
|
||||||
print ">> ";
|
-a, --author [AUTHOR] Set the author.
|
||||||
return <STDIN>;
|
-d, --date [DATE] Set the date.
|
||||||
|
-t, --title [TITLE] Set the title.
|
||||||
|
--skip Skip the prompt for missing metadata
|
||||||
|
and only change the needed metadata.
|
||||||
|
This only works if you've set any
|
||||||
|
of the metadata in the command line.
|
||||||
|
--json Return a JSON object on stdout.
|
||||||
|
--move Move the filename from the kebab-case
|
||||||
|
of the title.
|
||||||
|
"
|
||||||
|
|
||||||
|
var path = ''
|
||||||
|
var author = ''
|
||||||
|
var title = ''
|
||||||
|
var pub_date = ''
|
||||||
|
var skip = false
|
||||||
|
var json_data = false
|
||||||
|
var move = false
|
||||||
|
|
||||||
|
while test $# -gt 0 {
|
||||||
|
case $1 {
|
||||||
|
-h|--help)
|
||||||
|
echo $help_section
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-a|--author)
|
||||||
|
set author = $2
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-t|--title)
|
||||||
|
set title = $2
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-d|--date)
|
||||||
|
set pub_date = $2
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--skip)
|
||||||
|
set skip = true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--json)
|
||||||
|
set json_data = true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--move)
|
||||||
|
set move = true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
set path = $1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# This implementation of kebab-case conversion is opinionated.
|
proc kebab-case(word) {
|
||||||
# It converts into lowercase and removes all non-alphanumeric characters.
|
|
||||||
sub kebab_case {
|
|
||||||
my ($string) = @_;
|
|
||||||
|
|
||||||
# Convert into lower case.
|
# Convert into lower case.
|
||||||
$string = lc($string);
|
set word = $(echo $word | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
# Substitute all spaces and extra dashes as one dash.
|
# What happens to this line:
|
||||||
$string =~ s/\s+|-+/-/g;
|
# * Convert all whitespace and dashes into a single dash.
|
||||||
|
# * Remove all invalid characters (all that are not alphanumeric characters and dashes).
|
||||||
|
# * Remove leading and trailing dashes.
|
||||||
|
set word = $(echo $word | sed --regexp-extended --expression 's/\s+|-+/-/g' --expression 's/[^.a-z0-9-]//g' --expression 's/^-+|-+\$//g')
|
||||||
|
|
||||||
# Remove all invalid characters.
|
echo $word
|
||||||
$string =~ s/[^a-z0-9-]//g;
|
|
||||||
|
|
||||||
# Remove all leading and trailing dashes.
|
|
||||||
$string =~ s/^-+|-+$//g;
|
|
||||||
|
|
||||||
return $string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
my $file = $ARGV[0] ? $ARGV[0] : exit 1;
|
proc prompt(string, :out, prefix = ">> ") {
|
||||||
|
>&2 printf "%s\\n%s" $string $prefix
|
||||||
|
read --line
|
||||||
|
setref out = $_line
|
||||||
|
}
|
||||||
|
|
||||||
# Prompt for all the required information.
|
proc file_parse(path, :out) {
|
||||||
my $title = prompt "Title of the document?";
|
var extension_regex = / '.' ![ '.' ]+ %end /
|
||||||
my $author = prompt "Author?";
|
var file = {}
|
||||||
my $publication_date = prompt "Publication date?";
|
set file['dir'] = $(dirname $path)
|
||||||
|
set file['name'] = $(basename $path | sed --regexp-extended "s|(.+)${extension_regex}|\\1|")
|
||||||
|
set file['ext'] = $(basename $path | sed --regexp-extended "s|.+(${extension_regex})|\\1|")
|
||||||
|
|
||||||
# Overwrite the file metadata.
|
setref out = file
|
||||||
system "exiftool -title=\"${title}\" -author=\"${author}\" -date=\"${publication_date}\" \"$file\"";
|
}
|
||||||
|
|
||||||
# Once the file metadata has been written, it is time to move the file into its kebab-case equivalent.
|
test -f $path || {
|
||||||
my ($filename, $dirs, $suffix) = fileparse($file, '\.[^.]+$');
|
>&2 echo "${path} is not a regular file"
|
||||||
my $file_slug = $dirs . kebab_case($title) . $suffix;
|
exit 1
|
||||||
move($file, $file_slug);
|
}
|
||||||
|
|
||||||
|
# Prompt for the missing metadata (if it's configured to not skip, anyways).
|
||||||
|
if (not skip) {
|
||||||
|
test $title || prompt "What is the title?" :title
|
||||||
|
test $author || prompt "Who are the authors?" :author
|
||||||
|
test $pub_date || prompt "When this file published?" :pub_date
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writing up the metadata time!
|
||||||
|
# Each file type has a different way of embedding metadata so it needs different tools.
|
||||||
|
# We'll use exiftool as a catch-all since it supports the most out of... 15 file types I considered.
|
||||||
|
case $(file --mime-type --brief $path) {
|
||||||
|
"audio/ogg")
|
||||||
|
vorbiscomment --write $path --tag "TITLE=${title}" --tag "ARTIST=${author}" --tag "DATE=${pub_date}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
exiftool -title="${title}" -author="${author}" -date="${pub_date}" ${path}
|
||||||
|
;;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_parse $path :file_info
|
||||||
|
|
||||||
|
if (json_data) {
|
||||||
|
var metadata = {}
|
||||||
|
set metadata['file'] = file_info
|
||||||
|
set metadata['title'] = $title
|
||||||
|
set metadata['author'] = $author
|
||||||
|
set metadata['date'] = $pub_date
|
||||||
|
json write :metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move) {
|
||||||
|
mv $path "${file_info['dir']}/$(kebab-case $title).${file_info['ext']}"
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user