first basic "working" version of battery and photo

master
nixo 2019-01-25 14:06:28 +01:00
parent acabf69a93
commit da78f7c68e
9 changed files with 288 additions and 113 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use guix --ad-hoc perl-image-exiftool

32
bin/mytest.org Normal file
View File

@ -0,0 +1,32 @@
* <2019-01-25 Fri 12:52> Battery AC0
:PROPERTIES:
:STATUS: discharging
:CAPACITY: 87.7843%
:PERCENTAGE: 35%
:LID-CLOSED: no
:CONSUMPTION: 12.8211 W
:END:
* <2019-01-25 Fri 12:52> Battery AC0
:PROPERTIES:
:STATUS: discharging
:CAPACITY: 87.7843%
:PERCENTAGE: 35%
:LID-CLOSED: yes
:CONSUMPTION: 12.8211 W
:END:
* <2019-01-25 Fri 13:29> Battery AC0
:PROPERTIES:
:STATUS: charging
:CAPACITY: 87.7843%
:PERCENTAGE: 42%
:LID-CLOSED: no
:CONSUMPTION: 11.4094 W
:END:
* <2019-01-25 Fri 13:29> Battery AC0
:PROPERTIES:
:STATUS: charging
:CAPACITY: 87.7843%
:PERCENTAGE: 42%
:LID-CLOSED: no
:CONSUMPTION: 11.4094 W
:END:

19
bin/test Executable file
View File

@ -0,0 +1,19 @@
#!/run/current-system/profile/bin/guile
!#
(add-to-load-path "../")
(use-modules (org helpers)
(org format)
(importers photo)
(importers battery))
(use-modules (ice-9 textual-ports)) ; put-string
(set! *battery-path* "/org/freedesktop/UPower/devices/battery_BAT1")
(define (battery-to-org file)
(let ((output (open-file file "a+")))
(put-string output (battery->org *battery-path*))
(close-port output)))
(battery-to-org "mytest.org")

56
importers/battery.scm Normal file
View File

@ -0,0 +1,56 @@
(define-module (importers battery)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 textual-ports)
;; #:use-module (ice-9 format)
#:use-module (srfi srfi-19) ; string/date
#:use-module (org format)
#:use-module (srfi srfi-1)
#:export (*battery-path*
get-battery
battery->org))
;; Depends on ubattery
;; use "ubattery --enumerate" to list batteries
(define *battery-path* "")
(define (get-battery battery-path)
(let* ((port
(open-input-pipe
(string-join `("upower" "--dump" ,battery-path) " ")))
(str (get-string-all port)))
(close-pipe port)
str))
(define (battery-props battery-path)
(filter!
(lambda (x) (not (unspecified? x)))
(map
(lambda (x) (when (= 2 (length x))
`(,(string-trim-both (car x)) .
,(string-trim-both (cadr x)))))
(map
(lambda (x) (string-split x #\:))
(string-split (get-battery battery-path) #\nl)))))
(define (get-battery-prop battery-path prop)
(cdr (assoc prop (battery-props battery-path))))
(define (battery->org-props btr)
(org-properties
`(("STATUS" ,(get-battery-prop btr "state"))
("CAPACITY" ,(get-battery-prop btr "capacity"))
("PERCENTAGE" ,(get-battery-prop btr "percentage"))
("LID-CLOSED" ,(get-battery-prop btr "lid-is-closed"))
("CONSUMPTION" ,(get-battery-prop btr "energy-rate"))
)))
(define (battery->org battery)
(string-concatenate
`("* "
,(date->org (current-date) #t #t) " "
"Battery "
,(get-battery-prop *battery-path* "native-path")
"\n"
,(battery->org-props battery)
"\n")))

37
importers/photo.scm Normal file
View File

@ -0,0 +1,37 @@
(define-module (importers photo)
#:use-module (ice-9 popen)
#:use-module (ice-9 format)
#:use-module (ice-9 rdelim)
#:use-module (org format)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-19)
#:export (image->org
get-creation
*move-files*))
(define *move-files* #f)
;; implementation
(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"))))
(define (image->org filename)
(string-concatenate `("* "
,(date->org (get-creation filename) #t #t) " "
,(link->org (canonicalize-path filename)
;; TODO: an option could remove
;; the file extension
(basename filename) #f)
"\n"
,(org-properties `(("FILENAME" ,filename))))))
;; timestamp = OrgFormat.datetime(datetime)
;; output = OrgFormat.link(filename, photo_file)
;; properties = OrgProperties(photo_file + timestamp)

68
org/format.scm Normal file
View File

@ -0,0 +1,68 @@
(define-module (org format)
#:use-module (srfi srfi-19) ; string/date
#:use-module (ice-9 regex)
#:export (date->org
link->org
org-properties))
;; <YYYY-MM-DD Sun HH:MM>
(define (date->org date show-time active)
"Converts a date object to an Org Mode date"
(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))))))
;; (date->org (current-date) #t #t) -> <2019-01-23 Wed 15:30>
;; (date->org (current-date) #t #f) -> [2019-01-23 Wed 15:30]
;; (date->org (current-date) #f #t) -> <2019-01-23 Wed>
;; (date->org (current-date) #f #f) -> [2019-01-23 Wed]
(define (link->org lnk description replace-spaces)
"Create an Org Mode link.
`lnk' is what the link is pointing to
`description' is the visible text
`replace-spaces' replace spaces url-encodes the spaces inside the link
"
(let* ((lnk (if replace-spaces
(regexp-substitute/global #f "[ \t]+" lnk
'pre "%20" 'post)
lnk))
(output (string-concatenate `("[[" ,lnk
,(if description
(string-concatenate `("][" ,description))
"")
"]]"
))))
output))
;; (link->org "http://blog.nixo.xyz/example page" "My blog" #t)
;; -> [[http://blog.nixo.xyz/example%20page][My blog]]
;; (link->org "http://blog.nixo.xyz/example page" "My blog" #f)
;; -> [[http://blog.nixo.xyz/example page][My blog]]
;; (link->org "http://blog.nixo.xyz/example page" #f #f)
;; -> [[http://blog.nixo.xyz/example page]]
(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 (org-properties props)
(string-concatenate
`(
":PROPERTIES:\n"
,(format-properties props)
"\n:END:")))
;; (org-properties `(("NAME" "value")
;; ("PROP2" ,(number->string 2))))
;; ":PROPERTIES:
;; :NAME: value
;; :PROP2: 2
;; :END:"

35
org/helpers.scm Normal file
View File

@ -0,0 +1,35 @@
(define-module (org helpers)
#:autoload (ice-9 match) (list-pictures list-files-by-ext)
#:use-module (ice-9 regex)
#:use-module (ice-9 ftw)
#:use-module (ice-9 match)
#:use-module (srfi srfi-19) ; string/date
#:export (list-pictures
list-files-by-ext
date->path ; do not export later
*path-format*))
;; Customize this! (setq! *path-format* ...)
(define *path-format* "~Y/~m/")
;; use lowercase extensions!
(define picture-exts '("png" "jpg"))
(define (join-exts exts)
(string-join exts "|"))
(define (make-ext-regex exts)
(string-concatenate `("\\." "(" ,(join-exts exts) ")" "$")) )
(define (list-files-by-ext folder exts)
(scandir folder
(lambda (x) (string-match (make-ext-regex exts)
(string-downcase x)))))
(define (list-pictures dir)
(map (lambda (x) (string-concatenate `(,dir "/" ,x)))
(list-files-by-ext dir picture-exts)))
(define (date->path date)
"Create a path structure based on the filename.
Customize this with the variable *path-format*"
(date->string (current-date) *path-format*))

113
photo.scm
View File

@ -1,113 +0,0 @@
(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)

40
test.scm Normal file
View File

@ -0,0 +1,40 @@
(add-to-load-path ".")
(use-modules (org helpers)
(org format)
(importers photo)
(importers battery))
(use-modules (srfi srfi-19)) ;date
(use-modules (ice-9 textual-ports)) ; put-string
(define photo-dir "/home/nixo/memories/inbox/photos/moto/Camera")
(map
(lambda (x) (get-creation x))
(list-pictures photo-dir))
(set! *path-format* "~Y/~m/~d")
(set! *move-files* #t)
(date->path (current-date))
(for-each
(lambda (file)
(let ((output (open-file "example.org" "a+")))
(put-string output (string-concatenate
`(,(image->org file) "\n")))
(close-port output)))
(list-head (list-pictures photo-dir) 3))
(set! *battery-path* "/org/freedesktop/UPower/devices/battery_BAT1")
(define (battery-to-org file)
(let ((output (open-file file "a+")))
(put-string output (battery->org *battery-path*))
(close-port output)))
(battery-to-org "battery2.org")