Compare commits
2 Commits
0a84d68e85
...
794a702aaa
Author | SHA1 | Date |
---|---|---|
Ryan Rix | 794a702aaa | |
Ryan Rix | f359172059 |
30
command.fnl
30
command.fnl
|
@ -5,6 +5,8 @@
|
|||
(local stringx (require :pl.stringx))
|
||||
(local tablex (require :pl.tablex))
|
||||
(local text (require :pl.text))
|
||||
(local wallabag (require :wallabag))
|
||||
(local api-prefix wallabag.api-prefix)
|
||||
|
||||
(local sha256 (require :hashings.sha256))
|
||||
|
||||
|
@ -71,6 +73,17 @@
|
|||
(print "parsed" sum "highlights")
|
||||
output))
|
||||
|
||||
(local wallabag-token (->> ".wallabag"
|
||||
(wallabag.load-client-credentials)
|
||||
(wallabag.get-token (.. api-prefix "/oauth/v2/token"))))
|
||||
|
||||
(fn get-single-entry-from-wallabag [id]
|
||||
(let [(headers body) (wallabag.api-req wallabag-token "GET" (.. api-prefix "/api/entries/" id ".json"))]
|
||||
body))
|
||||
|
||||
(fn get-wallabag-url [id]
|
||||
(. (get-single-entry-from-wallabag id) :url))
|
||||
|
||||
(local template (. text :Template))
|
||||
|
||||
(local highlight-tmpl (template
|
||||
|
@ -119,15 +132,28 @@
|
|||
(local book-tmpl (template
|
||||
":PROPERTIES:
|
||||
:ID: koreader-${md5}
|
||||
:ROAM_REFS: \"${path}\"
|
||||
:END:
|
||||
#+TITLE: Notes from ${title}
|
||||
#+AUTHORS: ${authors}
|
||||
[[file:${path}][${path}]]
|
||||
[[${path}][${path}]]
|
||||
"))
|
||||
|
||||
(fn munge-book-path [book-md]
|
||||
(let [path (. book-md :path)
|
||||
(_ _ bag-id) (string.find path "%[w%-id_(%d+)%]")]
|
||||
(if bag-id
|
||||
(do
|
||||
(print "bag id" bag-id)
|
||||
(set book-md.path (get-wallabag-url bag-id)))
|
||||
;; sickos.jpg
|
||||
(set book-md.path (.. "file:"
|
||||
(string.gsub path "sdr/metadata.([^.]+).lua" "%1"))))))
|
||||
|
||||
(fn render-one-book [book]
|
||||
(let [authors (?. book "authors")
|
||||
title (?. book "title")]
|
||||
(munge-book-path book)
|
||||
(.. (: book-tmpl :substitute book)
|
||||
(stringx.join "\n"
|
||||
(icollect [_i1 chapter-hls (pairs (?. book "highlights"))]
|
||||
|
@ -176,7 +202,7 @@
|
|||
(lambda [book book-path]
|
||||
(write-one-book-from-md book book-path out-path))))
|
||||
|
||||
(let [default-book-dir "~/mobile-library/books"
|
||||
(let [default-book-dir "~/mobile-library/"
|
||||
default-note-dir "~/org/highlights/"
|
||||
args (lapp (stringx.join
|
||||
"\n"
|
||||
|
|
|
@ -4,7 +4,7 @@ let
|
|||
lua = pkgs.lua5_3;
|
||||
myLuaPkgs = import ./pkgs.nix { inherit pkgs; inherit lua; };
|
||||
myHashings = myLuaPkgs.hashings;
|
||||
myLua = lua.withPackages (luapkgs: with luapkgs; [penlight myHashings]);
|
||||
myLua = lua.withPackages (luapkgs: with luapkgs; [penlight http myHashings rapidjson]);
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
(local hr (require :http.request))
|
||||
(local tablex (require :pl.tablex))
|
||||
(local pretty (require :pl.pretty))
|
||||
(local rj (require :rapidjson))
|
||||
|
||||
(var api-prefix "https://bag.fontkeming.fail")
|
||||
|
||||
(fn load-client-credentials [path]
|
||||
((loadfile path)))
|
||||
|
||||
(fn get-token [token-endpoint credentials]
|
||||
(let [req (hr.new_from_uri token-endpoint)
|
||||
body (tablex.copy credentials)]
|
||||
(set body.grant_type "password")
|
||||
(req:set_body (rj.encode body))
|
||||
(: (. req :headers) :upsert ":method" "POST")
|
||||
(: (. req :headers) :append "content-type" "application/json")
|
||||
(match (req:go)
|
||||
(headers stream) (let [{ :access_token at }
|
||||
(rj.decode (stream:get_body_as_string 2))]
|
||||
at))))
|
||||
|
||||
(fn api-req [cred method uri body]
|
||||
(let [req (hr.new_from_uri uri)]
|
||||
(req:set_body (rj.encode body))
|
||||
(: (. req :headers) :upsert ":method" method)
|
||||
(: (. req :headers) :append "content-type" "application/json")
|
||||
(: (. req :headers) :append "authorization" (.. "Bearer " cred))
|
||||
(match (req:go)
|
||||
{:_data [{:name ":status" :value "404"}]} (print 404)
|
||||
{:_data [{:name ":status" :value "503"}]} (print 404)
|
||||
(headers stream) (values headers (rj.decode (stream:get_body_as_string 2)))
|
||||
_ (print "req failed"))))
|
||||
|
||||
{ :api-req api-req :get-token get-token :api-prefix api-prefix :load-client-credentials load-client-credentials }
|
Loading…
Reference in New Issue