Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require 'request-deferred)
- (defconst user-id 33)
- (defconst api-token "asdf")
- (defconst aika-url "http://localhost:4000/api")
- (defun org-aika ()
- (interactive)
- "HTTP POSTs a very specific sort of org-clocktable to the aikasheet API"
- (save-excursion
- (goto-char (point-max))
- (insert "\n#+TIMESHEET:\n"))
- (if (post-timesheet (collect-timesheet))
- (progn
- (goto-char (point-max))
- (message "GREAT SUCCESS"))
- (save-excursion
- (goto-char (point-max))
- (insert ":(╯°□°)╯︵ ┻━┻)"))))
- (defun post-timesheet (entries)
- (if entries
- (let ((entry (car entries)))
- (request
- (format "%s/entries/%s" aika-url (plist-get entry ':date))
- :type "POST"
- :data (json-encode-plist (plist-get entry ':data))
- :headers '(("Content-Type" . "application/json"))
- :parser 'json-read
- :sync t
- :success
- (cl-function
- (lambda (&key data &allow-other-keys)
- (save-excursion
- (goto-char (point-max))
- (insert (format ": OK %s\n" data)))
- (post-timesheet (cdr entries))))
- :error
- (cl-function
- (lambda (&rest args &key error-thrown &allow-other-keys)
- (save-excursion
- (goto-char (point-max))
- (insert (format ": ERR %s \n" error-thrown)))
- (post-timesheet (cdr entries))))))))
- (defun collect-timesheet ()
- (org-element-map (org-element-parse-buffer) 'headline
- (lambda (el)
- (if (eq 2 (level el))
- (list
- :date (date el)
- :data
- (list
- :user_id user-id
- :api_token api-token
- :entry
- (list
- :description (format "[%s] %s"
- (tag el)
- (description el))
- :time (duration el))))))))
- (defun level (el)
- (org-element-property :level el))
- (defun date (el)
- (car
- (org-element-map el 'clock
- (lambda (clock)
- (let ((timestamp (org-element-property :value clock)))
- (format
- "%02d-%02d-%02d"
- (org-element-property :year-start timestamp)
- (org-element-property :month-start timestamp)
- (org-element-property :day-start timestamp)))))))
- (defun tag (el)
- (car (org-element-property :tags el)))
- (defun description (el)
- (or (car
- (org-element-map el 'link
- (lambda (headline)
- (substring-no-properties (nth 2 headline)))))
- (org-element-property :raw-value el)))
- (defun duration (el)
- (apply '+ (all-durations el)))
- (defun all-durations (el)
- (org-element-map el 'clock
- (lambda (clock)
- (timestamp-to-hours
- (or (org-element-property :duration clock)
- "00:00")))))
- (defun timestamp-to-hours (str)
- (setq parts (split-string str ":"))
- (setq hours (string-to-number (car parts)))
- (setq mins (string-to-number (cadr parts)))
- (+ hours (/ mins (float 60))))
Advertisement
Add Comment
Please, Sign In to add comment