Adds Cheetah

This commit is contained in:
2022-01-11 11:11:36 -08:00
parent 318ae3024c
commit 55450a3c32
3 changed files with 82 additions and 80 deletions

View File

@@ -1,32 +1,31 @@
(ns auto-ap.handler
(:require [amazonica.core :refer [defcredential]]
[auto-ap.routes.auth :as auth]
[auto-ap.routes.events :as events]
[auto-ap.routes.exports :as exports]
[auto-ap.routes.queries :as queries]
[auto-ap.routes.graphql :as graphql]
[auto-ap.routes.invoices :as invoices]
[auto-ap.routes.yodlee :as yodlee]
[auto-ap.routes.yodlee2 :as yodlee2]
[buddy.auth.backends.token :refer [jws-backend]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[clojure.tools.logging :as log]
[clojure.string :as str]
[compojure.core :refer :all]
[compojure.route :as route]
[config.core :refer [env]]
[mount.core :as mount]
[ring.middleware.edn :refer [wrap-edn-params]]
[ring.middleware.gzip :refer [wrap-gzip]]
[ring.middleware.multipart-params :as mp]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.reload :refer [wrap-reload]]
[ring.util.response :as response]
[unilog.context :as lc]
[auto-ap.client-routes :as client-routes]
[bidi.bidi :as bidi]))
(:require
[amazonica.core :refer [defcredential]]
[auto-ap.client-routes :as client-routes]
[auto-ap.routes.auth :as auth]
[auto-ap.routes.exports :as exports]
[auto-ap.routes.graphql :as graphql]
[auto-ap.routes.invoices :as invoices]
[auto-ap.routes.queries :as queries]
[auto-ap.routes.yodlee :as yodlee]
[bidi.bidi :as bidi]
[buddy.auth.backends.token :refer [jws-backend]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[clojure.string :as str]
[clojure.tools.logging :as log]
#_{:clj-kondo/ignore [:refer-all]}
[compojure.core :refer :all]
[compojure.route :as route]
[config.core :refer [env]]
[mount.core :as mount]
[ring.middleware.edn :refer [wrap-edn-params]]
[ring.middleware.multipart-params :as mp]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.reload :refer [wrap-reload]]
[ring.util.response :as response]
[unilog.context :as lc]))
(when (:aws-access-key-id env)
(when (:aws-access-key-id env)
(defcredential (:aws-access-key-id env) (:aws-secret-access-key env) (:aws-region env)))
(def running? (atom false))
@@ -36,33 +35,31 @@
:stop (reset! running? false))
(defroutes static-routes
(GET "/" [] (response/resource-response "index.html" {:root "public"}))
(GET "/" [] (response/resource-response "index.html" {:root "public"}))
(route/resources "/")
(routes (ANY "*" {:keys [path] :as r}
(if (bidi/match-route client-routes/routes (:uri r))
(response/resource-response "index.html" {:root "public"})
{:status 404
:body "Not found"}))))
(routes (ANY "*" {:keys [uri]}
(if (bidi/match-route client-routes/routes uri)
(response/resource-response "index.html" {:root "public"})
{:status 404
:body "Not found"}))))
(defroutes health-check
(GET "/health-check" []
(if @running?
{:status 200
:body "Ok"}
{:status 503
:body "Application shut down"})))
(if @running?
{:status 200
:body "Ok"}
{:status 503
:body "Application shut down"})))
(defroutes api-routes
(context "/api" []
exports/export-routes
yodlee/routes
yodlee2/routes
queries/query2-routes
invoices/routes
graphql/routes
auth/routes
health-check))
exports/export-routes
yodlee/routes
queries/query2-routes
invoices/routes
graphql/routes
auth/routes
health-check))
(def auth-backend (jws-backend {:secret (:jwt-secret env) :options {:alg :hs512}}))
@@ -71,7 +68,7 @@
(handler request)))
(def app-routes
(routes
(routes
(wrap-transaction api-routes)
static-routes))
@@ -86,15 +83,12 @@
(log/info "Beginning request" (:uri request)))
(handler request))))
(def app
(-> #'app-routes
(wrap-logging)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend)
(wrap-reload)
(wrap-params)
(mp/wrap-multipart-params)
(wrap-edn-params)
#_(wrap-gzip)))
(wrap-authorization auth-backend)
(wrap-authentication auth-backend)
(wrap-reload)
(wrap-params)
(mp/wrap-multipart-params)
(wrap-edn-params)))

View File

@@ -1,16 +1,15 @@
(ns auto-ap.parse
(:require [auto-ap.parse.csv :as csv]
[auto-ap.parse.excel :as excel]
[auto-ap.parse.templates :as t]
[auto-ap.parse.util :as u]
[clj-fuzzy.metrics :as m]
[clj-time.core :as time]
[clj-time.format :as f]
[clojure.java.shell :as sh]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[auto-ap.logging :refer [info-event]]))
(:require
[auto-ap.logging :refer [info-event]]
[auto-ap.parse.csv :as csv]
[auto-ap.parse.excel :as excel]
[auto-ap.parse.templates :as t]
[auto-ap.parse.util :as u]
[clj-fuzzy.metrics :as m]
[clojure.java.shell :as sh]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]))
(def last-text (atom nil))
@@ -57,11 +56,11 @@
(extract-template text)))
(defmulti parse-file (fn [file filename] (.toLowerCase (last (str/split filename #"\." )))))
(defmulti parse-file (fn [_ filename] (.toLowerCase (last (str/split filename #"\." )))))
(defmethod parse-file
"pdf"
[file filename]
[file _]
(-> (sh/sh "pdftotext" "-layout" file "-")
:out
parse))
@@ -87,7 +86,7 @@
(best-match clients invoice-client-name 0.25))
([clients invoice-client-name threshold]
(let [fuzzy-match (->> clients
(mapcat (fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
(mapcat (fn [{:keys [:client/matches :client/name] :as client :or {matches []}}]
(map (fn [m]
[client (m/jaccard (.toLowerCase invoice-client-name) (.toLowerCase m))])
(conj matches name))))
@@ -98,7 +97,7 @@
word-set (set (filter (complement str/blank?) (str/split (.toLowerCase invoice-client-name) #"[\s:\-]" )))
client-word-match (->> clients
(map
(fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}]
(fn [{:keys [:client/matches :client/name] :as client :or {matches []}}]
(let [client-words (-> #{}
(into
(mapcat
@@ -119,7 +118,7 @@
(mapcat (fn [{:keys [:location-match/location :location-match/matches]}]
(map (fn [match] [location match]) matches)))
(filter (fn [[location match]]
(filter (fn [[_ match]]
(re-find (re-pattern (str "(?i)" match)) text)) )
first
first)
@@ -127,15 +126,14 @@
:client/location-matches
(mapcat (fn [{:keys [:location-match/location :location-match/matches]}]
(map (fn [match] [location match]) matches)))
(filter (fn [[location match]] (re-find (re-pattern (str "(?i)" match)) full-text)) )
(filter (fn [[_ match]] (re-find (re-pattern (str "(?i)" match)) full-text)) )
first
first)
(:client/default-location client)
(first (:client/locations client))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn dbg-parse [v]
(doto
(map
(fn [x] (dissoc x :full-text :text))
(parse v))
clojure.pprint/pprint ))
(map
(fn [x] (dissoc x :full-text :text))
(parse v)))

View File

@@ -182,6 +182,16 @@
:parser {:date [:clj-time "MM/dd/yyyy"]
:total [:trim-commas nil]}}
;; Cheetah
{:vendor "Cheetah"
:keywords [#"Truck name" #"Stop number"]
:extract {:date #"Delivery date: ([0-9\-]+)"
:customer-identifier #"Shipping.*\n(.*)"
:invoice-number #"Invoice #: (\d+)"
:total #"TOTAL:.*?\$(.*)"}
:parser {:date [:clj-time "yyyy-MM-dd"]
:total [:trim-commas nil]}}
;; Classic Wines
{:vendor "Classic Wines"
:keywords [#"585-9463"]