read a random file that was created on this day and add it to the

message of the day
This commit is contained in:
Anish Lakhwara 2022-09-11 11:24:44 +10:00
parent e9b785dcb2
commit a03ca0b389
2 changed files with 52 additions and 4 deletions

View file

@ -0,0 +1,47 @@
(ns dhyan.connections.files
(:require [clojure.java.shell :refer [sh]]
[clojure.string :as string]))
(defn get-birth
"Using stat, get the birth of the full file path passed"
[file]
(->> file
(sh "stat" "%w")
:out
(string/split-lines)
(last)
(re-find #"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])")
(first)))
(defn get-files
"Using find, Get a list of all the files in the folder passed"
[folder]
(->> (sh "find" folder "-maxdepth" "1" "-type" "f")
:out
(string/split-lines)))
(def folder "/home/anish/kitaab/vimwiki")
; Alternative:
; we could search through the git log to get the initial commit, but that only goes back 2 years
(defn on-this-day
"Find files also created on the current day/month
Expects filenames to be in format *YYMMDD* based on creation"
[]
(let [month (-> (sh "date" "+%m")
:out
(string/trim-newline))
day (-> (sh "date" "+%d")
:out
(string/trim-newline))
regex (re-pattern (str ".*\\d{2}" month day ".*"))]
(->> (get-files folder)
(filter #(some? (re-matches regex %))))))
(defn slurp-if-file
"If there was a file that was created on this day read a random one and return the text"
[]
(let [files (on-this-day)]
(if (> (count files) 0)
(slurp (rand-nth files))
"No file created on this day, a great day to journal!")))

View file

@ -1,6 +1,7 @@
(ns dhyan.main
(:require [dhyan.connections.shaarli :as shaarli]
[dhyan.connections.wallabag :as wallabag]
[dhyan.connections.files :as files]
[clojure.string :as string]
[dhyan.connections.matrix :as matrix]))
@ -15,16 +16,16 @@
rand-nth)
shaarli-message (str "Album of the day is: " (get album-of-the-day :title)
"\n available at: " (get album-of-the-day :url))
wallabag-data (wallabag/get-archived-annotated-entries)
wallabag-entry-of-the-day (rand-nth wallabag-data)
wallabag-entry-of-the-day (rand-nth (wallabag/get-archived-annotated-entries))
wallabag-annotation-of-the-day (rand-nth (get wallabag-entry-of-the-day :annotations))
wallabag-message (str "Annotation of the day: " wallabag-annotation-of-the-day
"\n from the article: " (get wallabag-entry-of-the-day :title)
"\n available at: " (get wallabag-entry-of-the-day :url)
"\n tagged as: " (string/join ", " (get wallabag-entry-of-the-day :tags)))
message (string/join "\n\n" [shaarli-message wallabag-message])
on-this-day-file (files/slurp-if-file)
message (string/join "\n\n" [shaarli-message wallabag-message on-this-day-file])]
;file-data (sh "ls" (System/getenv "folder"))
; remove anything in () [] {}
;filtered-file-data (filterv #(string/replace % #"(.*)" "") file-data)
]
(matrix/post-message message)))