Skip to content

Instantly share code, notes, and snippets.

@h8rt3rmin8r
Last active February 23, 2024 14:13
Show Gist options
  • Save h8rt3rmin8r/5ae75c352b9a48d64bb0514c686b70f3 to your computer and use it in GitHub Desktop.
Save h8rt3rmin8r/5ae75c352b9a48d64bb0514c686b70f3 to your computer and use it in GitHub Desktop.
Convert an .ics calendar file into JSON syntax
#! /usr/bin/env bash
#>------------------------------------------------------------------------------
#>
#> [ ics2json ]
#>
#> ABOUT:
#>
#> Convert an .ics calendar file into JSON syntax
#>
#> If input data is passed as a file reference, output data is written to
#> an identically named file as the input file with a new file extension
#> of ".json".
#>
#> If input data is passed as an incoming pipe, then output data will be
#> printed directly the standard output.
#>
#> Created on 20201209 by h8rt3rmin8r (161803398@email.tg)
#>
#> USAGE:
#>
#> ics2json <INPUT>
#> ics2json <OPTION>
#>
#> where "INPUT" is a valid file reference which includes the file extension
#> ".ics"; and where "OPTION" is one of the following:
#>
#> |
#> -h, --help | Print this help text to the terminal
#> |
#>
#> SOURCE:
#>
#> # Pastebin
#> https://bit.ly/2W6PVox
#>
#> # Github
#> https://bit.ly/377V323
#>
#>------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Declare functions
function ics2json_help() {
# Script help text printing function
cat "${0}" \
| grep -E '^#>' \
| sed 's/^..//'
return $?
}
function ics2json_vbs() {
# General script verbosity function
local message="$@"
local timestamp="$(date '+%s%N')"
echo "${timestamp}|${sh_name}|${message}" &>${vb_x}
return $?
}
function _main() {
dos2unix \
| sed 's/:/█/;s/$/░/' \
| tr -d '\n' \
| tr '\t' ' ' \
| tr -s ' ' ' ' \
| sed -E 's/BEGIN█/\nBEGIN█/g' \
| tail -n +2 \
| sed 's/\"/\\\"/g;s/█/\":\"/g;s/░/\",\"/g;s/^/\{\"/;s/..$/\},/' \
| tr -d '\n' \
| sed 's/^/\[/;s/,$//;s/$/\]/' \
| jq -c '.' 2>/dev/null
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
echo '[]'
fi
return ${e_c}
}
#-------------------------------------------------------------------------------
# Declare variables and execute operations
vb_x="/dev/stderr"
sh_name="ics2json"
## catch help text requests
if [[ "${1}" =~ ^[-][hH]$ || "${1}" =~ ^[-]+help$ ]]; then
ics2json_help
exit $?
fi
## execute operations based upon the form of input detected
if [ -t 0 ]; then
## declare input-specific variables
i_n=$(readlink -f "${1}")
out_file="${i_n%.ics}.json"
is_present=$(egrep --quiet '^.' <<<"${1}"; echo $?)
is_file=$(if [[ -f "${i_n}" ]]; then echo "0"; else echo "1"; fi)
is_ics=$(if [[ "${i_n}" =~ .ics$ ]]; then echo "0"; else echo "1"; fi)
## test the input string
if [[ "${is_present}" -ne 0 ]]; then
ics2json_vbs "ERROR: No input detected"
ics2json_vbs "Requires: <FILE>.ics"
ics2json_vbs "Use '--help' for more information"
exit 1
fi
if [[ "${is_file}" -ne 0 ]]; then
ics2json_vbs "ERROR: Input is not a valid file reference: ${1}"
ics2json_vbs "Use '--help' for more information"
exit 1
fi
if [[ "${is_ics}" -ne 0 ]]; then
ics2json_vbs "ERROR: Input is not a valid '.ics' file: ${1}"
ics2json_vbs "Use '--help' for more information"
exit 1
fi
_main <"${i_n}" > "${out_file}"
e_c="$?"
echo "${out_file}"
else
_main
e_c="$?"
fi
exit ${e_c}
@PSchileffski
Copy link

Why do you use
if [ -t 0 ]; then?
Do you see any issues using
if [ -n "${1}" ]; then
to be able to call the script from an automation script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment