Skip to content

Commit

Permalink
got a little carried away with my refactoring. Added a def and functi…
Browse files Browse the repository at this point in the history
…on back to parser.clj.
  • Loading branch information
rippinrobr committed Apr 21, 2011
1 parent b48cba3 commit 22ac65e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 0 additions & 1 deletion hugo_awards_best_novels.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
The Algebraist by Iain M. Banks [Orbit, 2004]
Iron Sunrise by Charles Stross [Ace, 2004]
Iron Council by China Miéville [Del Rey, 2004; Macmillan UK, 2004]

2004 Hugo Awards - Best Novel
Paladin of Souls by Lois McMaster Bujold [Eos, 2003] (WINNER)
Ilium by Dan Simmons [Subterranean, 2003; Eos, 2003; Gollancz, 2003]
Expand Down
3 changes: 2 additions & 1 deletion src/cmdline.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns cmdline
(:gen-class)
(:use hugo.parser)
(:use hugo.text-formatting)
(:require [clojure.contrib.duck-streams :as duck]))

(defn prep-for-file
Expand All @@ -10,4 +11,4 @@

(defn -main [& args]
"Runs the parser and then writes the results to the output file"
(duck/spit "hugo_awards_best_novels.txt" (prep-for-file (take 11 (get-award-links)))))
(duck/spit "hugo_awards_best_novels.txt" (prep-for-file (take 11 (hugo.parser/get-award-links)))))
27 changes: 16 additions & 11 deletions src/hugo/parser.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns hugo.parser
(:require [net.cgrand.enlive-html :as html])
(:require [clojure.contrib.duck-streams :as duck])
(:require [clojure.contrib.string :as ccstring]))

(defn fetch-url
Expand Down Expand Up @@ -31,15 +30,21 @@
{:books (get-book-info (rest (second %)))}
{:year (first (:content (first (html/select page-content #{[:div#content :h2]}))))}) (parse-award-page page-content))))

(defn if-work-not-nil
"Formats the book's line like so: title author and WINNER if it won as long as work is not nil"
[work]
(str (if (not (nil? (:title work))) (str "\n\t" (:title work)) ) (if (not (nil? (:winner work))) (str " (WINNER)") ) ""))
(def *base-url* "http://www.thehugoawards.org/hugo-history/")

(defn format-nominees [works]
"formats all winners and nominees for a given award into one string"
(apply str (map if-work-not-nil works)))
(defn get-award-links []
(map #(:attrs %) (html/select (fetch-url *base-url*)
#{[:div#content :li.page_item :a] [:li.page.item.subtext html/first-child]})))

(defn format-output [novels]
"formats the award section including award title, winners and nominees"
(format "%s - Best Novel%s\n" (:year novels) (format-nominees (:books novels))))
;(defn if-work-not-nil
;"Formats the book's line like so: title author and WINNER if it won as long as work is not nil"
;[work]
;(str (if (not (nil? (:title work))) (str "\n\t" (:title work)) ) (if (not (nil? (:winner work))) (str " (WINNER)") ) ""))

;(defn format-nominees [works]
;"formats all winners and nominees for a given award into one string"
;(apply str (map if-work-not-nil works)))

;(defn format-output [novels]
;"formats the award section including award title, winners and nominees"
;(format "%s - Best Novel%s\n" (:year novels) (format-nominees (:books novels))))

0 comments on commit 22ac65e

Please sign in to comment.