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

Commit

Permalink
Putting together new data structures for display
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopch committed Jun 2, 2012
1 parent d29c1d1 commit c46e3d7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 79 deletions.
7 changes: 3 additions & 4 deletions src/isbnnetinclj/models/info.clj
Expand Up @@ -4,7 +4,6 @@
[net.cgrand.enlive-html :as html]
[monger.collection :as mc]
[isbnnetinclj.models.stores :as stores]
[isbnnetinclj.models.infolog :as infolog]
[isbnnetinclj.utils :as utils]))


Expand Down Expand Up @@ -35,7 +34,7 @@

(defn flipkart-page-content
[isbn]
(utils/fetch-url (format (get-in stores/sites [:flipkart :url]) isbn)))
(utils/fetch-page (format (get-in stores/sites [:flipkart :url]) isbn)))


(defn flipkart-image
Expand Down Expand Up @@ -66,7 +65,7 @@
:publisher (book-row 7)}}))


(defn flipkart-book-info
(defn fetch-book-info
[isbn]
(try
(let [info (flipkart-details isbn (flipkart-page-content isbn))]
Expand All @@ -79,4 +78,4 @@
(defn book-info
[isbn]
(or (get-stored-book-info isbn)
(flipkart-book-info isbn)))
(fetch-book-info isbn)))
24 changes: 0 additions & 24 deletions src/isbnnetinclj/models/infolog.clj

This file was deleted.

24 changes: 0 additions & 24 deletions src/isbnnetinclj/models/priceslog.clj

This file was deleted.

14 changes: 0 additions & 14 deletions src/isbnnetinclj/models/requestlog.clj

This file was deleted.

6 changes: 3 additions & 3 deletions src/isbnnetinclj/models/stores.clj
Expand Up @@ -72,8 +72,8 @@

(defn fetch-book-data-from-one-store
[isbn [site-name {:keys [url price-path]}]]
(let [url (format url isbn)
content (utils/fetch-url url)
(let [address (format url isbn)
content (utils/fetch-page address)
price-data (parse-price-from-content content price-path)]
(try (swap! book-data-cache
assoc-in [isbn :price site-name] price-data)
Expand All @@ -97,4 +97,4 @@
(defn book-data
[isbn]
(or (get-stored-book-data isbn)
(do (future (fetch-book-data isbn)) {})))
(do (future (fetch-book-data isbn)) {:when (java.util.Date.)})))
13 changes: 7 additions & 6 deletions src/isbnnetinclj/utils.clj
Expand Up @@ -4,9 +4,10 @@
[monger.query :as mq]
[monger.joda-time]
[clj-time.core :as time]
[clj-time.format :as timeformat]))
[clj-time.format :as timeformat]
[clj-time.coerce :as timecoerce]))

(defn fetch-url
(defn fetch-page
[url]
(html/html-resource (java.net.URL. url)))

Expand All @@ -15,13 +16,13 @@
(time/minus (time/now) (time/days 1)))

(defn format-timestamp
[timestamp]
(timeformat/unparse (timeformat/formatters :rfc822) timestamp))
[when]
(timeformat/unparse (timeformat/formatters :rfc822) (timecoerce/from-date when)))

(defn get-fresh-db-data
[db_collection isbn]
[collection-name isbn]
(first (mq/with-collection
db_collection
collection-name
(mq/find {:isbn isbn :when {"$gt" (twenty-four-hours-ago)}})
(mq/sort {:when -1})
(mq/limit 1))))
17 changes: 13 additions & 4 deletions src/isbnnetinclj/views/book.clj
Expand Up @@ -6,7 +6,8 @@
[isbnnetinclj.utils :as utils])
(:use [noir.core]
[isbnnetinclj.models.info :only [book-info]]
[isbnnetinclj.models.stores :only [book-data]]))
[isbnnetinclj.models.stores :only [book-data sites]]
[clojure.pprint :only [pprint]]))


(def request-collection "request")
Expand All @@ -19,12 +20,20 @@
:when (java.util.Date.)})


(defn convert-prices-for-display
[isbn prices]
(map (fn [[store-name price]]
{:name (name store-name)
:price price
:url (format (get-in sites [store-name :url]) isbn)}) prices))


(defpage "/:isbn" {:keys [isbn]}
(let [prices-log (book-data isbn)
(let [data (book-data isbn)
info (book-info isbn)]
(mc/insert request-collection (core-details-of-request (noir.request/ring-request)))
(mus/render-file "book.mustache" {:prices (:prices prices-log)
:when-prices (utils/format-timestamp (:timestamp prices-log))
(mus/render-file "book.mustache" {:prices (convert-prices-for-display isbn (:price data))
:when-prices (utils/format-timestamp (:when data))
:isbn isbn
:info (:info info)
:title (or (get-in book-info [:info :title]) "isbn.net.in")})))

0 comments on commit c46e3d7

Please sign in to comment.