Created
December 18, 2020 05:56
-
-
Save netj/0a0155f6259f96f87fb04e73e1b8a094 to your computer and use it in GitHub Desktop.
This file contains 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 bash | |
# a Day One CLI for macOS script generator for importing diary entries from Momento3 text exports | |
# | |
# Prerequisites: | |
# | |
# - install dayone2 CLI | |
# - brew install coreutils | |
# | |
# | |
# Usage: run the script inside a Momento Export (one text file per day format) | |
# | |
# $ cd "Momento Export "*/ | |
# $ export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH" | |
# $ export journal=Momento | |
# $ dayone_import_script=../"$(basename "$PWD")".tool | |
# $ ./dayone2-script-from-momento3-export.sh >"$dayone_import_script" | |
# $ chmod +x "$dayone_import_script" | |
# | |
# $ # Now, remember from the Day One app to create a journal named "Momento" (or the custom $journal name you chose) | |
# $ less -S "$dayone_import_script" # take one last good look at the script before running | |
# $ open "$dayone_import_script" # run the script to observe each entry showing up in the Day One.app | |
# | |
## | |
# Author: Jaeho Shin <[email protected]> | |
# Created: 2020-12-17 | |
set -eu | |
: ${journal:=Momento} | |
: ${tz:=} | |
[[ $# -gt 0 ]] || set -- ????.??.??" - "*.txt | |
echo "#!/usr/bin/env bash | |
# Day One import script generated from Momento export: $PWD | |
set -euo pipefail | |
type dayone2 || { echo 'install Day One CLI, following: https://help.dayoneapp.com/en/articles/435871-command-line-interface-cli'; false; } >&2 | |
set -x | |
" | |
for file_day; do | |
echo >&2 "# $file_day" | |
date=${file_day%% - *} | |
rm -rf "$date".tmp | |
mkdir -p "$date".tmp | |
cat "$file_day" | tr -d '\r' | | |
csplit --quiet --prefix="$date".tmp/x - '%====*%2' '/[0-9][0-9]:[0-9][0-9]/' '{*}' >&2 | |
for file_entry in "$date".tmp/x*; do | |
# parse metadata from Momento3's export text | |
has_field() { grep "^$1: " "$file_entry" -q; } | |
value_for() { grep "^$1: " "$file_entry" | sed 's/^.*: //'; } | |
# detect timezone changes | |
first_line=$(tail -n +2 "$file_entry"); | |
[[ $(wc -l <<<"$first_line") -gt 1 ]] || | |
case $first_line in | |
"Changed timezone to "*) | |
tz=${first_line#* to } | |
case $tz in # XXX cope with non-IANA DST names unrecognized by dayone2 | |
PDT) tz=GMT-7 ;; | |
MDT) tz=GMT-6 ;; | |
CDT) tz=GMT-5 ;; | |
EDT) tz=GMT-4 ;; | |
esac | |
echo >&2 "# tz=$tz" | |
continue | |
;; | |
esac | |
time=$(head -1 "$file_entry") | |
args=() | |
args+=(-j "$journal") | |
args+=(-d "$date $time") | |
[[ -z "$tz" ]] || args+=(-z "$tz") | |
tags=() | |
! has_field Feed || tags+=("$(value_for Feed)") | |
! has_field Tags || while read t; do tags+=("$t"); done < <(value_for Tags | sed 's/, /\n/g') | |
! has_field Media || while read f; do args+=(-p "Attachments/$f"); done < <(value_for Media) | |
! has_field At || { coords=$(value_for At | grep -o '(.*, .*)' | tr -d '(,)'); [[ -z "$coords" ]] || args+=(--coordinate $coords); } | |
[[ ${#tags[@]} -eq 0 ]] || args+=(-t "${tags[@]}") | |
# emit a dayone2 CLI command | |
echo "# $file_entry" | |
printf '%q ' dayone2 "${args[@]}" -- new | |
echo "<<'EOF'" | |
tail -n +2 "$file_entry" | grep -v -f <( # | |
echo ^Feed: | |
echo ^Tags: | |
echo ^Media: | |
) || true | |
echo "EOF" | |
echo | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment