From 418b5dad205c70ff232d084d170853f1db3081ce Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 2 Jun 2022 10:57:12 +0100 Subject: initial random journal page --- README.md | 26 ++++++++++++++++++++++++++ randomjournalpage.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 README.md create mode 100755 randomjournalpage.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..65be27d --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Read a random journal + +This is a quick and dirty script which I use personally to grab a random PDF file (a scanned version of one of my bullet journals)and to extract a random set of pages from it in order to reflect/read what was happening in the past. This also includes various notes of books I have read and random ideas I wrote down and my want to reconsider. + +## Requirements + +For Fedora Linux: + +``` +# for (obviously) qpdf +sudo dnf install qpdf +# for pdfinfo +sudo dnf install poppler-utils +``` + +## Usage + +Simply configure the journal path and the PDF vewer in the script and run: `./randomjournalpage.sh` + +## Testing + +There is no real testing done. It's a pretty simple script. However, the script passes the following ShellCheck command: + +``` +shellcheck randomjournalpage.sh --exclude SC2155 +``` diff --git a/randomjournalpage.sh b/randomjournalpage.sh new file mode 100755 index 0000000..d8d3e32 --- /dev/null +++ b/randomjournalpage.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +declare -r JOURNAL_DIR="$HOME/Nextcloud/Documents/Notebooks" +declare -i NUM_PAGES_TO_EXTRACT=42 # This is the answear! +declare -r PDF_VIEWER_COMMAND=/usr/bin/evince +declare -r OUT_PDF=$HOME/.random_journal.pdf + +get_random_journal () { + find "$1" -name \*.pdf | sort -R | head -n 1 +} + +extract () { + local -r in_pdf="$1"; shift + local -i from="$1"; shift + local -i to="$1"; shift + local -r out_pdf="$1"; shift + + echo "Extracting pages ${from}-${to} from $in_pdf" + qpdf --empty --pages "$in_pdf" "${from}-${to}" -- "$out_pdf" + chmod 600 "$out_pdf" +} + +main () { + local -r random_journal=$(get_random_journal "$JOURNAL_DIR") + local -i num_pages=$(pdfinfo "$random_journal" | awk '/Pages/ { print $2 }') + + local -i extract_from=$(( 1 + (RANDOM % num_pages) - NUM_PAGES_TO_EXTRACT )) + if [ $extract_from -lt 1 ]; then + extract_from=1 + fi + + local -i extract_to=$(( extract_from + NUM_PAGES_TO_EXTRACT -1 )) + if [ $extract_to -gt "$num_pages" ]; then + extract_to=$num_pages + fi + + extract "$random_journal" "$extract_from" "$extract_to" "$OUT_PDF" + $PDF_VIEWER_COMMAND "$OUT_PDF" +} + +main + -- cgit v1.2.3