streamlet-examples/exiftool
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 71 f8fe085c1c9f
permissions -rwxr-xr-x
portable order of (i++) parameters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, version 3 of the License.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# GNU General Public License for more details.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
41
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    19
# This streamlet provides various file metadata like EXIF or PDF. It calls the tool exiftool.
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    20
# With no options it returns "File:MIMEType" and "exiftool_xml" attributes.
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    21
# Specific attributes can be selected using options – e.g. --option 'attribute' '…'
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    22
# List of available attributes can be obtained by directly calling the "exiftool -X" command on given file or from the "available_attributes" attribute.
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    23
# Two additional attributes are provided by this streamlet:
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    24
#  - "exiftool_xml" – all attributes provided by exiftool in form of XML
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    25
#  - "available_attributes" – list of available attributes (each file may have different) separated by line-breaks (TODO: return as an array of strings, when this data type is implemented)
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
50
22ed5647b235 streamlets: include streamlet-common.sh from the directory where the actual streamlet resides (not where is the symlink, if any)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    28
. "$(dirname "$(realpath "$0")")/streamlet-common.sh"
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    31
	streamletFields=()
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    32
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    33
	for (( i=0; i<${#optionNames[@]}; i++)); do
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    34
		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    35
			streamletFields+=("${optionValues[$i]}");
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    36
		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    37
			pdfPrefix="${optionValues[$i]}";
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    38
		else
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    39
			echo "Unsupported option: ${optionNames[$i]}" >&2
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    40
		fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    41
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    42
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    43
	if [[ -z "$streamletFields" ]]; then
41
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    44
		streamletFields=( "File:MIMEType" "exiftool_xml" );
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    45
	fi
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    46
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    47
	for (( i=0; i<${#streamletFields[@]}; i++)); do
71
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    48
		local exifIntegerFields="^(PNG:ImageWidth|PNG:ImageHeight|PNG:BitDepth|File:ImageWidth|File:ImageHeight|File:BitsPerSample|File:ColorComponents)$";
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    49
		if [[ "${streamletFields[$i]}" =~ "$exifIntegerFields" ]]; then type="integer";
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    50
		else type="string";
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    51
		fi
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    52
		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}"    "$type"
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    53
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    54
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	send WAITING_FOR_INPUT_ATTRIBUTES
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    59
	local streamletInfo streamletValid value isNull;
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    60
41
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    61
	[[ -d "$currentFile" ]] || streamletInfo="$(exiftool -X "$currentFile")";
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    62
	streamletValid="$?";
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    63
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    64
	for (( i=0; i<${#streamletFields[@]}; i++)); do
41
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    65
		if   [[ "x${streamletFields[$i]}" == "xexiftool_xml"  ]]; then value="$streamletInfo";
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    66
		elif [[ "x${streamletFields[$i]}" == "xavailable_attributes"  ]]; then
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    67
			value=$'available_attributes\nexiftool_xml\n'"$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records '/*/*/*' --attribute 'name' string 'name()' | relpipe-out-nullbyte | tr \\0 \\n)";
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    68
		else
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    69
			value="$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records "/*/*/*[name() = '${streamletFields[$i]}']" --attribute 'value' string '.' | relpipe-out-nullbyte | tr -d \\0)";
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    70
			# TODO: parse the XML only once
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    71
			# TODO: validate parameter or use parametrized XPath
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    72
			# TODO: use real namespaces
40
a41423ac9e94 streamlet examples: exiv2
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    73
		fi
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    74
41
afbce5b8aabf streamlet examples: exiftool
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
    75
		# n.b. for some files exiftools returns exit code, however it provides some basic properties like file timestamps and <ExifTool:Error>Unknown file type</ExifTool:Error> which is also valid XML and might be useful
71
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    76
		if   [[ ! "x$streamletValid" == "x0" ]] || [[ "x$value" == "x" ]]; then isNull="true";
f8fe085c1c9f streamlet examples: exiftool: integer data type for certain attributes
František Kučera <franta-hg@frantovo.cz>
parents: 50
diff changeset
    77
		else                                                                    isNull="false";
39
225da9aa9c94 streamlet examples: pdfinfo
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    78
		fi
37
3dbe113637ef streamlet: cloc – count lines of code
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    79
34
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    80
		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    81
	done
0b9e4af08cc8 streamlet examples: hash
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
    82
	
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	send WAITING_FOR_INPUT_ATTRIBUTES;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
initialize
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
processMessages