diff --git a/import/api/wpforms-mails/.gitignore b/import/api/wpforms-mails/.gitignore index aa5f3a9..3e64a0c 100644 --- a/import/api/wpforms-mails/.gitignore +++ b/import/api/wpforms-mails/.gitignore @@ -10,3 +10,4 @@ pom.xml.asc /.nrepl-port /.prepl-port *.xlsx +*.csv diff --git a/import/api/wpforms-mails/project.clj b/import/api/wpforms-mails/project.clj index 7678b36..78103fb 100644 --- a/import/api/wpforms-mails/project.clj +++ b/import/api/wpforms-mails/project.clj @@ -8,6 +8,7 @@ [io.forward/clojure-mail "1.0.8"] [clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure org.clojure/data.xml]] [org.clojure/data.xml "0.0.8"] + [semantic-csv "0.2.1-alpha1"] [dk.ative/docjure "1.14.0"]] :main ^:skip-aot wpforms-mails.core :target-path "target/%s" diff --git a/import/api/wpforms-mails/src/data/table/writer.clj b/import/api/wpforms-mails/src/data/table/writer.clj new file mode 100644 index 0000000..ab02a98 --- /dev/null +++ b/import/api/wpforms-mails/src/data/table/writer.clj @@ -0,0 +1,34 @@ +(ns data.table.writer + (:require [dk.ative.docjure.spreadsheet :as xls] + [semantic-csv.core :as sc] + [clojure.spec.alpha :as s] + [clojure.string :refer [ends-with?]])) + +;; TODO check that cells are literals, not compounds +(s/def ::table-map (s/coll-of map?)) +(s/def ::table-vec (s/coll-of vector?)) +(s/def ::table (s/or :map ::table-map + :vec ::table-vec)) + +(defn vectorize-if-needed [table] + (assert (s/valid? ::table table)) + (if (s/valid? ::table-map table) + (sc/vectorize table) + table)) + +(defn table2xls [filename args table] + (let [workbook-name (:workbook-name args filename) + wb (xls/create-workbook workbook-name (vectorize-if-needed table))] + (xls/save-workbook! filename wb))) + +(defn save-table! + ([filename table] + (save-table! filename {} table)) + ([filename args table] + (assert (or (ends-with? filename ".xlsx") + (ends-with? filename ".csv"))) + (if (ends-with? filename ".xlsx") + (table2xls filename args table) + (let [args+defaults (merge {:writer-opts {:delimiter ";"}} args)] + (sc/spit-csv filename args+defaults table))) + (println "Saved " filename))) diff --git a/import/api/wpforms-mails/src/wpforms_mails/core.clj b/import/api/wpforms-mails/src/wpforms_mails/core.clj index 0a52949..dfd9139 100644 --- a/import/api/wpforms-mails/src/wpforms_mails/core.clj +++ b/import/api/wpforms-mails/src/wpforms_mails/core.clj @@ -6,7 +6,7 @@ [clojure-mail.message :as cmm] [pl.danieljanus.tagsoup :refer [parse-string]] [wpforms-mails.parse-hickup :refer [wpforms_html->edn]] - [dk.ative.docjure.spreadsheet :refer [create-workbook save-workbook!]]) + [data.table.writer :refer [save-table!]]) (:import [java.util Properties] [javax.mail Session] [javax.mail.internet MimeMessage]) @@ -39,21 +39,16 @@ (->> msg:edn :body :body parse-string)))) -(defn save-spreadsheet! [filename sheet data] - (let [wb (create-workbook sheet - (concat [(keys (first data))] - (map vals data)))] - (save-workbook! filename wb))) - (defn -main - [& _args] + ([] (-main "host-offers.xlsx")) + ([filename & _args] (->> (file->messages (:wpforms-mails-file env)) (map (fn [message] (-> message message->html wpforms_html->edn))) rest ;; TODO filter valid entries - (save-spreadsheet! "host-offers.xlsx" "Host Offers"))) + (save-table! filename {:workbook-name "Host Offers"})))) (comment (count (mbox->emls (:wpforms-mails-file env)))