From 7c96254e28a37ee97d5b40c05aa59a11078508e9 Mon Sep 17 00:00:00 2001 From: Swaroop C H Date: Sat, 19 May 2012 14:54:11 +0530 Subject: [PATCH] Adding prices fetching --- project.clj | 3 +- src/isbnnetinclj/models/stores.clj | 52 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/isbnnetinclj/models/stores.clj diff --git a/project.clj b/project.clj index 273f4ba..fc6bd70 100644 --- a/project.clj +++ b/project.clj @@ -5,5 +5,6 @@ [noir "1.3.0-beta7"] [hiccup "1.0.0"] [com.novemberain/monger "1.0.0-beta6"] - [joda-time "2.1"]] + [joda-time "2.1"] + [enlive "1.0.0"]] :main isbnnetinclj.server) diff --git a/src/isbnnetinclj/models/stores.clj b/src/isbnnetinclj/models/stores.clj new file mode 100644 index 0000000..e09e02e --- /dev/null +++ b/src/isbnnetinclj/models/stores.clj @@ -0,0 +1,52 @@ +(ns isbnnetinclj.models.stores + [:require [net.cgrand.enlive-html :as html] + [clojure.string :as string]]) + +(def sites + {:flipkart + {:url "http://www.flipkart.com/search.php?query=%s" + :price-path [:span#fk-mprod-our-id html/content]} + :infibeam + {:url "http://www.infibeam.com/Books/search?q=%s" + :price-path [:span.infiPrice html/text]} + :homeshop18 + {:url "http://www.homeshop18.com/search:%s/categoryid:10000" + :price-path [:span#productLayoutForm:OurPrice html/text]} + }) + +(defn fetch-url + [url] + (html/html-resource (java.net.URL. url))) + +(defn find-price-at-end + [text] + (if (empty? text) (str "not available") + (try (Float/valueOf (last (re-seq #"\d+(?:\.\d+)?" (string/trim (string/replace (str text) "," ""))))) + (catch Exception x (str x))))) + +(defn parse-page + [content path] + (let [nodes (html/select content path) + node (last nodes)] + (find-price-at-end node))) + +(defn search + [isbn {:keys [url price-path]}] + (let [url (format url isbn) + content (fetch-url url)] + (try (parse-page content price-path) + (catch Exception x (str x))))) + +(defn add-price + [isbn site] + (assoc site :price (search isbn site))) + +(defn search-all + [isbn] + (zipmap + (keys sites) + (map (partial search isbn) (vals sites)))) + +(defn sorted-search-all + [isbn] + (sort-by val (search-all isbn)))