Skip to content

Instantly share code, notes, and snippets.

@calebsmith
Created August 21, 2015 02:57
Show Gist options
  • Save calebsmith/4a00dad7b92d38299ac7 to your computer and use it in GitHub Desktop.
Save calebsmith/4a00dad7b92d38299ac7 to your computer and use it in GitHub Desktop.
(ns xkcd-helper
(:require [clojure.string :as s]
[net.cgrand.enlive-html :as enl]))
(def base-url "https://xkcd.com/")
(def build-url #(str base-url % "/"))
(def fetch-url (comp enl/html-resource #(new java.net.URL %)))
(def fetch-url-on-num (comp fetch-url build-url))
(defn get-transcript [cont]
(first (:content (first (enl/select cont [:#transcript])))))
(defn get-comic [cont]
(:src (:attrs (second (:content (first (enl/select cont [:#comic])))))))
(defn xkcd [comic-num]
(let [html (fetch-url-on-num comic-num)]
((juxt (comp #(str "https:" %) get-comic) (comp s/split-lines get-transcript)) html)))
(map xkcd [224 297])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment