Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
Adding prices fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopch committed May 19, 2012
1 parent 8dd8136 commit 7c96254
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -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)
52 changes: 52 additions & 0 deletions 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)))

0 comments on commit 7c96254

Please sign in to comment.