aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--project.clj9
-rw-r--r--src/lemmold/core.clj87
2 files changed, 96 insertions, 0 deletions
diff --git a/project.clj b/project.clj
new file mode 100644
index 0000000..4a864ff
--- /dev/null
+++ b/project.clj
@@ -0,0 +1,9 @@
+(defproject lemmold "0.1.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
+ :url "https://www.eclipse.org/legal/epl-2.0/"}
+ :dependencies [[org.clojure/clojure "1.11.1"]
+ [clj-http "3.12.3"]
+ [org.clojure/data.json "2.4.0"]]
+ :main ^:skip-aot lemmold.core)
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)))))
+