aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2023-07-10 09:27:51 -0400
committerClyne Sullivan <clyne@bitgloo.com>2023-07-10 09:27:51 -0400
commit4585b6dffb0b0cec6b445656955d51a20b2f9f3e (patch)
tree9eaf08c6e2bd5dc7d674fcf748be7f17362483a3 /src
parent15eae1ab2d73b1a844f27eb1588d9a13117e0e61 (diff)
rough draft
Diffstat (limited to 'src')
-rw-r--r--src/lemmold/core.clj87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/lemmold/core.clj b/src/lemmold/core.clj
new file mode 100644
index 0000000..6528e51
--- /dev/null
+++ b/src/lemmold/core.clj
@@ -0,0 +1,87 @@
+(ns lemmold.core
+ (:require [clj-http.client :as client])
+ (:require [clojure.data.json :as json]))
+
+(def INSTANCE "lemmy.ml")
+(def POST-LIST "/post/list")
+(def COMMENTS "/comment/list")
+
+(defn make-api-url [inst endpoint]
+ (str "https://" inst "/api/v3" endpoint))
+
+(defn api-call [api-url query-params]
+ (-> api-url
+ (client/get {:accept :json :query-params query-params})
+ (get :body)
+ (json/read-str)))
+
+(defn post-list [page]
+ (-> (make-api-url INSTANCE POST-LIST)
+ (api-call {:sort "Hot" :page (str page)})
+ (get "posts")))
+
+(defn comments-list [post]
+ (-> (make-api-url INSTANCE COMMENTS)
+ (api-call {:id (get-in post ["post" "id"])})
+ (get "comments")))
+
+(defn show-post-item [post]
+ (println (get-in post ["creator" "name"]) "on" (get-in post ["community" "name"]))
+ (println (get-in post ["post" "name"]))
+ (println "at" (get-in post ["post" "published"]) "(" (get-in post ["counts" "comments"]) "comments)")
+ (println)
+ )
+
+(defn show-comment-item [commnt]
+ (println (get-in commnt ["creator" "name"]) "says:")
+ (println (get-in commnt ["comment" "content"]))
+ (println)
+ )
+
+(defn show-posts [posts]
+ (doseq [post (take 5 posts)]
+ (show-post-item post)))
+
+(defn show-comments [comments]
+ (doseq [commnt (take 5 comments)]
+ (show-comment-item commnt)))
+
+(defn show-posts-prompt []
+ (print "#NPQ> ")
+ (flush)
+ (first (read-line)))
+
+(defn show-comments-prompt []
+ (print "BNPQ> ")
+ (flush)
+ (first (read-line)))
+
+(defn view-post [post]
+ (println "Viewing post id:" (get-in post ["post" "id"]))
+ (loop [offset 0 comments (comments-list (get-in post ["post" "id"]))]
+ (show-comments (if (pos? offset) (drop (* 5 offset) comments) comments))
+ (flush)
+ (case (show-comments-prompt)
+ \B (println)
+ \N (recur (inc offset) comments)
+ \P (recur (dec offset) comments)
+ \Q (println "Goodbye.")
+ (do (println "Unknown command.") (recur offset comments)))))
+
+(defn -main [& args]
+ (loop [top true page 1 posts (post-list page)]
+ (show-posts (if top posts (drop 5 posts)))
+ (flush)
+ (case (show-posts-prompt)
+ \N (if top (recur false page posts)
+ (recur true (inc page) (post-list (inc page))))
+ \P (if top (recur false (dec page) (post-list (dec page)))
+ (recur true page posts))
+ \Q (println "Goodbye.")
+ \1 (do (view-post (nth posts 0)) (recur top page posts))
+ \2 (do (view-post (nth posts 1)) (recur top page posts))
+ \3 (do (view-post (nth posts 2)) (recur top page posts))
+ \4 (do (view-post (nth posts 3)) (recur top page posts))
+ \5 (do (view-post (nth posts 4)) (recur top page posts))
+ (do (println "Unknown command.") (recur top page posts)))))
+