diff --git a/src/micro_blog/main.clj b/src/micro_blog/main.clj index cb84b05..c4ec6b9 100644 --- a/src/micro_blog/main.clj +++ b/src/micro_blog/main.clj @@ -5,6 +5,7 @@ [taoensso.telemere :as tel] [micro-blog.logging.main :as logging] micro-blog.api + [micro-blog.nostr :as nostr] [micro-blog.blue-sky :as blue-sky] [micro-blog.mastodon :as masto])) @@ -15,6 +16,8 @@ (logging/setup-logging) (tel/log! :info "Setting up API") (micro-blog.api/start) + (tel/log! :info "Setting up nostr scraper") + (nostr/start) (tel/log! :info "Setting up crons") (doseq [[i cron] (map-indexed vector crons)] (let [start (.plus (Instant/now) (Duration/ofMinutes (* i 5)))] diff --git a/src/micro_blog/nostr.clj b/src/micro_blog/nostr.clj index ed9adbb..35afe68 100644 --- a/src/micro_blog/nostr.clj +++ b/src/micro_blog/nostr.clj @@ -1,9 +1,12 @@ (ns micro-blog.nostr (:require [micro-blog.pocket-base :as pb] + [micro-blog.is-tech] + [taoensso.telemere :as tel] [hato.websocket :as ws] [cheshire.core :as json] [clojure.string :as str] + [clojure.pprint :refer [pprint]] [micro-blog.config :refer [config]]) (:import [java.time Instant OffsetDateTime ZoneOffset] @@ -15,22 +18,49 @@ (Instant/parse) (.getEpochSecond))) +(defn nostr-date-to-pb [ts] + (let [instant (java.time.Instant/ofEpochSecond ts) + formatter (java.time.format.DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss.SSSX") + zoned (.atZone instant java.time.ZoneOffset/UTC)] + (.format formatter zoned))) + (defn last-post-timestamp [] (pb-date-to-unix-timestamp-seconds (:posted (pb/get-latest-post-by-source :nostr)))) -;; :nostr-fetcher-npub "NOSTR_FETCHER_NPUB" -;; :nostr-id "NOSTR_ID" -;; :nostr-relay "NOSTR_RELAY" +(defn transform-post [post] + (tel/log! {:level :info :data {:post post}} "Transforming nostr post") + (hash-map :source :nostr + :fullPost post + :remoteId (get post "id") + :isTech (micro-blog.is-tech/is-tech? (get post "content")) + :authorId (get post "pubkey") + :tags (reduce (fn [acc tag] + (let [tag-type (first tag) + tag-value (second tag)] + (if (= tag-type "t") + (conj acc tag-value) + acc))) [] (get post "tags")) + :images [] + :posted (nostr-date-to-pb (get post "created_at")))) + +(defn process-msg [raw-msg] + (let [msg (json/parse-string (.toString raw-msg))] + (tel/log! {:level :info :data {:msg msg}} "Processing nostr message") + (let [msg-type (first msg) ;; ex: EVENT + event (nth msg 2)] + (when (and (= (get event "kind") 1) (= msg-type "EVENT")) + (-> event + transform-post + pb/save-post))))) + (def socket (atom nil)) (defn connect [] (reset! socket @(ws/websocket (@config :nostr-relay) {:on-message (fn [_ws msg _last?] - (println "Received message:" msg)) + (process-msg msg)) :on-close (fn [_ws _status _reason] (println "WebSocket closed!"))}))) -;; (last-post-timestamp []) - (defn subscribe-to-author [pubkey since] (let [sub-id (@config :nostr-fetcher-id) filter {:kinds [1] :authors [pubkey] :since since} @@ -39,3 +69,7 @@ (defn close [] (ws/close! @socket)) + +(defn start [] + (connect) + (subscribe-to-author (@config :nostr-id) (last-post-timestamp))) diff --git a/src/micro_blog/pocket_base.clj b/src/micro_blog/pocket_base.clj index f74e8c4..bd43e02 100644 --- a/src/micro_blog/pocket_base.clj +++ b/src/micro_blog/pocket_base.clj @@ -1,6 +1,5 @@ (ns micro-blog.pocket-base (:require - [clojure.pprint :refer [pprint]] [clojure.string :as str] [clj-http.client :as http-client] [malli.core :as m]