move everything around so it actually does the things i want

This commit is contained in:
Anish Lakhwara 2022-07-09 20:17:31 +10:00
parent e5dc7d83b1
commit bab12b111b
5 changed files with 254 additions and 2 deletions

View file

@ -1,9 +1,21 @@
## Description
A babashka script that checks a list of sources, and randomly picks one to be album of the day
A collection of babashka scripts to remind me of things, by posting to my matrix channel
it (aspires to) check:
- Shaarli
- wallabag
- files
- kitaab
- the weather?
## Expectations
##### shaarli
Connect to Shaarli
Check incoming & downloaded?
Imports album through beets?
read *'d responses and do something with that?
##### wallabag
connect to wallabag
pick a random article
post

View file

@ -0,0 +1,83 @@
(ns dhyan.connections.matrix
(:require
[cheshire.core :as json]
[babashka.curl :as curl]))
(def creds (atom 1))
(def rooms (atom 1))
(def txid (atom 1))
(def url
{:scheme "https"
:host "chat.sealight.xyz"
:port 443
:path "/_matrix/client/v3/"
:query ""})
(defn append-to-path
[url endpoint]
(update url :path #(str % endpoint)))
(defn add-access-token
[url]
(assoc url :query (str "access_token=" (get @creds :access_token))))
(defn get-creds
[url data]
(let [url' (append-to-path url "login")]
(->> (curl/request {:url url'
:method "post"
:raw-args ["-d" data]})
:body
(json/decode)
(clojure.walk/keywordize-keys)
(reset! creds))))
(defn get-rooms
[url]
(let [url' (-> url
(add-access-token)
(append-to-path "joined_rooms"))]
(->> (curl/request {:url url'
:throw false})
:body
(json/parse-string)
(clojure.walk/keywordize-keys)
(reset! rooms))))
(def data (json/encode {:type "m.login.password"
:user "siren_bot"
:password (System/getenv "MATRIX_PASSWORD")}))
(defn setup
[]
(get-creds url data)
(get-rooms url))
(defn post-message
[m]
(let [message (json/encode {:body m
:msgtype "m.text"})
url' (-> url
(add-access-token)
(append-to-path (str "rooms/" (first (get @rooms :joined_rooms)) "/send/m.room.message/" @txid)))]
(-> (curl/request {:url url'
:method "put"
:raw-args ["-d" message]})
:body)
(swap! txid inc)))
(defn change-username
[username]
(let [message (json/encode {:displayname username})
url' (-> url
(add-access-token)
(append-to-path "profile/@siren_bot:sealight.xyz/displayname"))]
(-> (curl/request {:url url'
:method "put"
:raw-args ["-d" message]}))))
(comment
(setup)
(change-username "wail")
(post-message "test message"))

View file

@ -0,0 +1,76 @@
(ns dhyan.connections.shaarli
(:require [clojure.java.shell :refer [sh]]
[cheshire.core :as json]
[clojure.string :as string]
[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"})
.getBytes
(.encodeToString encoder))
; TODO can't figure out how to apply this with an anon function
header (clojure.string/replace header-raw "=" "")
date (-> (sh "date" "-u" "+%s")
:out
(clojure.string/replace "\n" ""))
payload-raw (->> (json/encode {"iat" date})
.getBytes
(.encodeToString encoder))
; 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))
signature (-> sig
; TODO should do url encode here
(string/replace "/" "_")
(string/replace "=" "")
(string/replace "+" "-"))]
(str header "." payload "." signature))))
(defn url-filterer
[m url]
(filter (fn [x] (.contains (get x :url) url)) m))
(def token (generate-token (System/getenv "SHAARLI_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))))))
(comment
(shaarli-request 0)
(count (get-all-shaarli-data))
(url-filterer {:url "bandcamp"} "bandcamp")
(filter (fn [x] (.contains (get x :url) "bandcamp")) [{:url "bandcamp"}])
(println "somestring")
(.getBytes (str {"iat" (sh "date" "-u" "+%s")}))
(def signature (-> (Mac/getInstance "HmacSHA256")
(.init (SecretKeySpec. (.getBytes "test secret") "HmacSHA256"))
(.doFinal (.getBytes (str "header.payload") "UTF-8"))))
(def sig
(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")
(str {"alg" "HS512" "typ" "JWT"}))

20
src/dhyan/main.clj Normal file
View file

@ -0,0 +1,20 @@
(ns dhyan.main
(:require [dhyan.connections.shaarli :as shaarli]
[clojure.java.shell :refer [sh]]
[dhyan.connections.matrix :as matrix]))
(defn -main
[& _args]
(matrix/setup)
(let [shaarli-data (shaarli/get-all-shaarli-data)
; filter to bandcamp (and maybe others?)
filtered-shaarli-data (shaarli/url-filterer shaarli-data "bandcamp")
album-of-the-day (->> filtered-shaarli-data
(mapv #(select-keys % [:url :title]))
rand-nth)
message (str "Album of the day is: " (get album-of-the-day :title) " available at: " (get album-of-the-day :url))
;file-data (sh "ls" (System/getenv "folder"))
; remove anything in () [] {}
;filtered-file-data (filterv #(string/replace % #"(.*)" "") file-data)
]
(matrix/post-message message)))

View file

@ -0,0 +1,61 @@
(ns server.socket
(:require
[clojure.java.io :refer [writer reader]])
(:import
[java.net ServerSocket]
[java.io BufferedWriter BufferedReader]))
(defn create-server
[port]
(ServerSocket. port))
(defn listen
"Waits for a connection from another socket to come through, then
returns the server's now-connected Socket."
[server-socket]
(.accept server-socket))
(defn get-reader
"Get the BufferedReader for a socket.
This is memoized so that we always get the same reader for a given socket.
If we didn't do this, every time we did e.g. read-char on a socket we'd
get back a new reader, and that last one would be thrown away despite having
loaded input into its buffer."
[socket]
(reader socket))
(defn read-line-from-socket
"Read a line from the given socket"
[socket]
(let [read-line-from-reader
(fn [reader]
(.readLine reader))]
(read-line-from-reader (get-reader socket))))
(defn read-lines
"Read all the lines currently loaded into the input stream of a socket."
[socket]
(line-seq (get-reader socket)))
(defn wait-for-message
[]
(let [server (create-server 3754)
socket (listen server)
message (read-lines (get-reader socket))]
message))
(def server (listen (create-server 4646)))
(comment
(.readLine (reader server))
(read-line-from-socket server)
; TODO i think this is why we need memoization?
; i can keep doing read-line-from-socket or .readLine
; and i'll keep getting lines until one comes through
; but the moment I do read-lines, i lose the ability to get any text back?
; seems like a memoization thing
; but i can't call any of the functions that use the memoized version?
; not allowed to call readLine on memoized?
(read-lines server))
#_(wait-for-message)