init photo

This commit is contained in:
nixo 2019-01-12 22:55:18 +01:00
commit acabf69a93
1 changed files with 113 additions and 0 deletions

113
photo.scm Normal file
View File

@ -0,0 +1,113 @@
(use-modules (srfi srfi-19)) ; string/date
(use-modules (ice-9 ftw))
(use-modules (ice-9 format))
(use-modules (ice-9 popen))
(use-modules (ice-9 rdelim))
(use-modules (ice-9 textual-ports)) ; put-string
(getcwd)
(chdir "/home/nixo/memories/inbox/photos/moto/Camera")
(define (get-creation filename)
(if (access? filename R_OK)
(let* ((port (open-input-pipe
;; FIXME: add "datetimeoriginal" as another option
(string-join `("exiftool" "-T" "-createdate" ,filename) " ")))
(str (read-line port)))
(close-pipe port)
(string->date str "~Y:~m:~d ~H:~M:~S")
)))
(system "ls")
;; <YYYY-MM-DD Sun HH:MM>
(define (date->org date show-time active)
(let* ((brackets (if active '("<" ">")
'( "[" "]")))
(format (string-append (car brackets) "~Y-~m-~d ~a"))
(format (if show-time
(string-append format " ~H:~M")
format)))
(date->string date (string-append format (car (cdr brackets))))))
(define (link->org lnk description replacespaces)
(let* ((lnk (if replacespaces
(regexp-substitute/global #f "[ \t]+" lnk
'pre "%20" 'post)
lnk))
(output (if description
(string-concatenate `("[[" ,lnk
"][" ,description
"]]"))
(string-concatenate `("[[" ,lnk
"]]")))))
output))
(define (make-org-pair p)
(string-concatenate `(,(car p) ":" ,(cadr p))))
(define (format-properties props)
(string-join
(map (lambda (x) (make-org-pair x)) props)
"\n"))
(define (create-uuid props) "UUID")
(define (org-properties props)
(let ((id (create-uuid props)))
(string-concatenate
`(
":PROPERTIES:\n"
,(format-properties props)
":ID:" ,id "\n"
":END:\n"))))
(define (image->org filename)
(string-concatenate `("* "
,(date->org (get-creation filename) #t #t) " "
,(link->org (canonicalize-path filename)
filename #t)
"\n"
,(org-properties `(("FILENAME" ,filename))))))
(use-modules (ice-9 match))
(define remove-stat
(match-lambda
((name stat) ; flat file
name)
((name stat children ...) ; directory
(list name (map remove-stat children)))))
(define image-exts '("jpg" "jpeg" "png"))
;; (make-regexp "\.png" regexp/icase)
;; (map image-exts)
;; (map (lambda (ext) (string-match filename)
;; (string-contains-ci "test.png" ext)) image-exts)
;; (define (image? filename)
;; (map (lambda (ext) (string-contains-ci filename ext)) image-exts)
;; )
;; (filter! image? (cadr (remove-stat (file-system-tree "."))))
;; (let ((dir "."))
;; (remove-stat ))
(let ((output (open-file "example.org" "a")))
(put-string output (image->org "IMG_20180520_230823.jpg"))
(close-port output))
;; (image-dir->org ".")
;; timestamp = OrgFormat.datetime(datetime)
;; output = OrgFormat.link(filename, photo_file)
;; properties = OrgProperties(photo_file + timestamp)