search heb for fooditems

This commit is contained in:
Bart Akeley 2020-04-12 14:06:37 -05:00
parent 1a8a8f80d8
commit 50de03fb66
5 changed files with 76 additions and 14 deletions

View file

@ -18,7 +18,8 @@
[com.layerware/hugsql "0.5.1"]
[buddy/buddy-auth "2.1.0"]
[clj-http "3.7.0"]
[amazonica "0.3.121"]]
[amazonica "0.3.121"]
[enlive "1.1.6"]]
:plugins [[lein-ring "0.9.7"] [lein-environ "1.1.0"]]
:ring {:handler aretherecookies.app/app}
:profiles

View file

@ -1,13 +1,13 @@
(ns aretherecookies.app
(:gen-class)
(:require [aretherecookies.handler :refer [food-items-handler
quantity-handler
(:require [aretherecookies.handler :refer [quantity-handler
add-food-item-handler
add-image-handler
get-images-handler
faves-get-handler
faves-put-handler
faves-delete-handler]]
faves-delete-handler
heb-search-handler]]
[aretherecookies.auth :refer [auth0-auth-backend]]
[environ.core :refer [env]]
[compojure.handler :refer [api]]
@ -22,14 +22,14 @@
(defroutes app-routes
(GET "/" [] (str {:csrf-token *anti-forgery-token*}))
(POST "/test" [] "ok")
(POST "/fooditems" [] food-items-handler)
(POST "/quantity" [] quantity-handler)
(POST "/addfooditem" [] add-food-item-handler)
(POST "/images/:foodItemId" [] add-image-handler)
(GET "/images/:foodItemId" [] get-images-handler)
(POST "/faves" [] faves-get-handler)
(PUT "/fave" [] faves-put-handler)
(DELETE "/fave" [] faves-delete-handler))
(DELETE "/fave" [] faves-delete-handler)
(GET "/fooditems/:search" [] heb-search-handler))
(def app-config (assoc-in api-defaults [:security :anti-forgery] false))

View file

@ -9,21 +9,18 @@
delete-faves]]
[aretherecookies.parsers :refer [parse-special-types
parse-location]]
[aretherecookies.helpers :refer [safe-json]]
[aretherecookies.search.heb :refer [memoized-search-heb]]
[clojure.data.json :as json]
[aretherecookies.aws :refer [put-s3
build-s3-url]]
[clojure.data.json :as json]
[clojure.string :as str]
[buddy.auth :refer [authenticated?
throw-unauthorized]])
throw-unauthorized]]
[net.cgrand.enlive-html :as html])
(:import (java.util UUID)))
(defn safe-json
"converts hashmaps into a JSON string suitable for a network response"
[data]
(json/write-str data :value-fn parse-special-types))
(defn bad-request
"returns a HTTP 404 error with the supplied body"
[body]
@ -145,3 +142,25 @@
foodItemIds (get-in req [:body :foodItemIds])]
(println "/faves/" email "----> DELETE" foodItemIds)
(->> (delete-faves email foodItemIds) safe-json)))
(defn heb-product-to-food-item
[html-node]
(let [image-url (:data-src (:attrs (first (html/select html-node [:img]))))
content-data (json/read-str (first (:content (first (html/select html-node [:script])))))]
{:id (str "HEB:" (get content-data "id"))
:name (get content-data "name")
:thumbimage image-url
:placeType "HEB"
:category (get content-data "category")
:variant (get content-data "variant")}))
(defn heb-search-handler
"passes a search text to heb and parses html response"
[req]
(let [search (get-in req [:params :search])]
(println (str "/heb/" search))
(if-not (empty? search)
(safe-json {:fooditems (memoized-search-heb search)})
(safe-json {:fooditems []}))))

View file

@ -0,0 +1,9 @@
(ns aretherecookies.helpers
(:require [aretherecookies.parsers :refer [parse-special-types]]
[clojure.data.json :as json]))
(defn safe-json
"converts hashmaps into a JSON string suitable for a network response"
[data]
(json/write-str data :value-fn parse-special-types))

View file

@ -0,0 +1,33 @@
(ns aretherecookies.search.heb
(:require [clojure.data.json :as json]
[net.cgrand.enlive-html :as html]
[clojure.core.memoize :as memoize]
[clojure.core.cache :as cache]))
(defn heb-product-to-food-item
[html-node]
(let [image-url (:data-src (:attrs (first (html/select html-node [:img]))))
content-data (json/read-str (first (:content (first (html/select html-node [:script])))))]
{:id (str "HEB:" (get content-data "id"))
:name (get content-data "name")
:thumbimage image-url
:placeType "HEB"
:category (get content-data "category")
:variant (get content-data "variant")}))
(defn search-heb
"passes a search text to heb and parses html response"
[search]
(if-not (empty? search)
(as-> (java.net.URLEncoder/encode search) $
(str "https://www.heb.com/search/?q=" $)
(java.net.URL. $)
(html/html-resource $)
(html/select $ [:div.gtm-product])
(map heb-product-to-food-item $))
[]))
(def memoized-search-heb
(memoize/fifo
search-heb
(cache/ttl-cache-factory {} :ttl (* 30 60 1000))))