Skip to content

Instantly share code, notes, and snippets.

@nathanic
Created January 29, 2013 19:39
Show Gist options
  • Save nathanic/4667040 to your computer and use it in GitHub Desktop.
Save nathanic/4667040 to your computer and use it in GitHub Desktop.
Lazybot hacker news plugin
(ns lazybot.plugins.hackernews
(:use [lazybot registry]
[clojure.data.json :only [read-json]])
(:require [clj-http.client :as client])
(:import org.apache.commons.lang.StringEscapeUtils)
)
; we've got a couple choices for hacker news APIs
; both APIs return similar JSON
; but ihackernews seems faster but unreliable (500s a lot)
; and returns the score as :points
; and hndroidapi seems to require some html entity decoding
; and returns the score as :score
;(def HN-URL "http://api.ihackernews.com/page")
(def HN-URL "https://hndroidapi.appspot.com/news/format/json/page/?appid=&callback=")
(defn build-item-url
[item]
(assoc item :comment-url
(str "https://news.ycombinator.com/item?id=" (:item-id item))) )
(defn random-hn-item
"pull a random story from the front page"
[]
(-> (client/get HN-URL)
:body
read-json
:items
rand-nth
; uncomment for html entity decoding
; (update-in [:title] (memfn StringEscapeUtils/unescapeHtml))
))
(defplugin
(:cmd
"show a random story from the front page of Hacker News"
#{"hackernews" "hn"}
(fn [com-m]
(try
(let [{:keys [title points score url]} (random-hn-item) ]
(send-message com-m (str title " (score: " (or points score) ") " url)))
(catch Exception e
(send-message com-m (str "sorry, got an exception trying to reach HN: " e)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment