factoring out shaarli stuff, and now also reading from folders

This commit is contained in:
Anish Lakhwara 2022-06-19 22:44:50 +10:00
parent afa54c2e5a
commit 431ee04053
2 changed files with 41 additions and 9 deletions

View file

@ -14,6 +14,9 @@
buildInputs = with pkgs; [ buildInputs = with pkgs; [
babashka babashka
]; ];
shellHook = ''
[[ -z $token ]] && echo "::dhyan:: Necessary Env Var missing: token"
'';
}; };
}; };
} }

View file

@ -1,6 +1,7 @@
(ns dhynan.main (ns dhynan.main
(:require [clojure.java.shell :refer [sh]] (:require [clojure.java.shell :refer [sh]]
[cheshire.core :as json] [cheshire.core :as json]
[clojure.string :as string]
[babashka.curl :as curl]) [babashka.curl :as curl])
(:import (javax.crypto Mac) (:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec))) (javax.crypto.spec SecretKeySpec)))
@ -28,20 +29,47 @@
(.encodeToString encoder)) (.encodeToString encoder))
signature (-> sig signature (-> sig
; TODO should do url encode here ; TODO should do url encode here
(clojure.string/replace "/" "_") (string/replace "/" "_")
(clojure.string/replace "=" "") (string/replace "=" "")
(clojure.string/replace "+" "-"))] (string/replace "+" "-"))]
(str header "." payload "." signature)))) (str header "." payload "." signature))))
(let [token (generate-token (System/getenv "token")) (defn filterer
data (-> (curl/get "http://links.mossnet.lan/api/v1/links?searchtags=music" {:headers {"Authorization" (str "Bearer " token)}}) [m]
(:body) (filter (fn [x] (.contains (get x :url) "bandcamp")) m))
(json/parse-string true))
(def token (generate-token (System/getenv "token")))
(defn shaarli-request [offset]
(-> (curl/get (str "http://links.mossnet.lan/api/v1/links?searchtags=music&limit=" offset) {:headers {"Authorization" (str "Bearer " token)}})
(:body)
(json/parse-string true)))
(defn get-all-shaarli-data
([] (get-all-shaarli-data {} 0))
([data offset]
(let [new-data (shaarli-request offset)
total-data (concat data new-data)
old-last (last data)
new-last (last new-data)]
(if (= old-last new-last)
total-data
(get-all-shaarli-data total-data (+ offset 100))))))
(let [shaarli-data (get-all-shaarli-data)
; filter to bandcamp (and maybe others?) ; filter to bandcamp (and maybe others?)
filtered-data (mapv (filter (fn [x] (contains? "bandcamp" (get x :url)))) data)] filtered-shaarli-data (filterv #(.contains (get % :url) "bandcamp") shaarli-data)
filtered-data) file-data (sh "ls" (System/getenv "folder"))
; remove anything in () [] {}
filtered-file-data (filterv #(string/replace % #"(.*)" "") file-data)]
(->> filtered-shaarli-data
(mapv #(select-keys % [:url :title]))))
(comment (comment
(shaarli-request 0)
(count (get-all-shaarli-data))
(filterer {:url "bandcamp"})
(filter (fn [x] (.contains (get x :url) "bandcamp")) [{:url "bandcamp"}])
(println "somestring") (println "somestring")
(.getBytes (str {"iat" (sh "date" "-u" "+%s")})) (.getBytes (str {"iat" (sh "date" "-u" "+%s")}))
(def signature (-> (Mac/getInstance "HmacSHA256") (def signature (-> (Mac/getInstance "HmacSHA256")
@ -53,4 +81,5 @@
(.init mac (SecretKeySpec. (.getBytes "test secret") algo)) (.init mac (SecretKeySpec. (.getBytes "test secret") algo))
(str (.doFinal mac (.getBytes "header.payload" "UTF-8"))))) (str (.doFinal mac (.getBytes "header.payload" "UTF-8")))))
(println sig) (println sig)
(.contains "https://somesite.bandcamp.com.au" "bandcamp")
(str {"alg" "HS512" "typ" "JWT"})) (str {"alg" "HS512" "typ" "JWT"}))