dhyan/main.clj

86 lines
3.3 KiB
Clojure
Raw Normal View History

2022-06-18 01:26:53 -07:00
(ns dhynan.main
(:require [clojure.java.shell :refer [sh]]
[cheshire.core :as json]
[clojure.string :as string]
2022-06-18 01:26:53 -07:00
[babashka.curl :as curl])
(:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec)))
(defn generate-token
[api-secret]
(let [encoder (java.util.Base64/getEncoder)
header-raw (->> (json/encode {"alg" "HS512" "typ" "JWT"})
2022-06-18 01:44:29 -07:00
.getBytes
(.encodeToString encoder))
2022-06-18 01:26:53 -07:00
; TODO can't figure out how to apply this with an anon function
header (clojure.string/replace header-raw "=" "")
2022-06-18 01:44:29 -07:00
date (-> (sh "date" "-u" "+%s")
:out
(clojure.string/replace "\n" ""))
2022-06-18 01:26:53 -07:00
payload-raw (->> (json/encode {"iat" date})
2022-06-18 01:44:29 -07:00
.getBytes
(.encodeToString encoder))
2022-06-18 01:26:53 -07:00
; TODO can't figure out how to apply this with an anon function
payload (clojure.string/replace payload-raw "=" "")
algo "HmacSHA512"
mac (Mac/getInstance algo)]
(.init mac (SecretKeySpec. (.getBytes api-secret) algo))
(let [sig (->> (.doFinal mac (.getBytes (str header "." payload)))
(.encodeToString encoder))
2022-06-18 01:44:29 -07:00
signature (-> sig
2022-06-18 01:26:53 -07:00
; TODO should do url encode here
(string/replace "/" "_")
(string/replace "=" "")
(string/replace "+" "-"))]
2022-06-18 01:44:29 -07:00
(str header "." payload "." signature))))
2022-06-18 01:26:53 -07:00
(defn filterer
[m]
(filter (fn [x] (.contains (get x :url) "bandcamp")) m))
(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)
2022-06-18 01:26:53 -07:00
; filter to bandcamp (and maybe others?)
filtered-shaarli-data (filterv #(.contains (get % :url) "bandcamp") shaarli-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]))))
2022-06-18 01:26:53 -07:00
2022-06-18 01:44:29 -07:00
(comment
(shaarli-request 0)
(count (get-all-shaarli-data))
(filterer {:url "bandcamp"})
(filter (fn [x] (.contains (get x :url) "bandcamp")) [{:url "bandcamp"}])
2022-06-18 01:26:53 -07:00
(println "somestring")
(.getBytes (str {"iat" (sh "date" "-u" "+%s")}))
(def signature (-> (Mac/getInstance "HmacSHA256")
2022-06-18 01:44:29 -07:00
(.init (SecretKeySpec. (.getBytes "test secret") "HmacSHA256"))
(.doFinal (.getBytes (str "header.payload") "UTF-8"))))
(def sig
2022-06-18 01:26:53 -07:00
(let [algo "HmacSHA256"
mac (Mac/getInstance algo)]
(.init mac (SecretKeySpec. (.getBytes "test secret") algo))
(str (.doFinal mac (.getBytes "header.payload" "UTF-8")))))
(println sig)
(.contains "https://somesite.bandcamp.com.au" "bandcamp")
2022-06-18 01:26:53 -07:00
(str {"alg" "HS512" "typ" "JWT"}))