This repository has been archived by the owner on Dec 26, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) |