summaryrefslogtreecommitdiff
path: root/bin/fetch.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'bin/fetch.fnl')
-rw-r--r--bin/fetch.fnl76
1 files changed, 76 insertions, 0 deletions
diff --git a/bin/fetch.fnl b/bin/fetch.fnl
new file mode 100644
index 0000000..fe1a1a5
--- /dev/null
+++ b/bin/fetch.fnl
@@ -0,0 +1,76 @@
+(import-macros {: map : reduce} :lib.macro)
+
+(tset package :path (.. package.path ";./vendor/lpeglj/?.lua"))
+
+(local array (require :lib.array))
+(local ozchai (require :parser.ozchai))
+(local ipuer (require :parser.ipuer))
+(local artoftea (require :parser.artoftea))
+
+(local db (luna.db.open "file:var/db.sqlite?_journal=WAL&_sync=NORMAL"))
+(luna.db.exec db "
+ PRAGMA foreign_keys=ON;
+ PRAGMA journal_mode=WAL;
+ PRAGMA synchronous=NORMAL;
+
+ CREATE VIRTUAL TABLE IF NOT EXISTS search USING fts5(name, fid, `table`);
+
+ CREATE TABLE IF NOT EXISTS products (
+ id TEXT NOT NULL PRIMARY KEY,
+ site TEXT NOT NULL,
+ category TEXT NOT NULL,
+ title TEXT NOT NULL,
+ description TEXT NOT NULL,
+ year INT NOT NULL,
+ image TEXT NOT NULL,
+ url TEXT NOT NULL,
+ price REAL NOT NULL,
+ weight REAL NOT NULL,
+ price_per REAL NOT NULL,
+ misc TEXT NOT NULL,
+ creation_time DATETIME NOT NULL
+ );" [])
+
+(fn now []
+ (os.date "%Y-%m-%d %H:%M:%S"))
+
+(fn store-products [products]
+ (local sql
+ (.. "INSERT OR REPLACE INTO products VALUES "
+ (table.concat
+ (map (fn [_ _]
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
+ products)
+ ",")))
+ (local vars
+ (reduce
+ (fn [_ product rest]
+ (array.concat rest
+ [product.id
+ product.site
+ product.category
+ product.title
+ (or product.description "")
+ (or product.year 0)
+ (or product.image "")
+ (or product.url "")
+ (or product.price 0)
+ (or product.weight 0)
+ (or product.price-per 0)
+ (or product.misc "")
+ (now)]))
+ products []))
+ (luna.db.exec db sql vars))
+
+(fn populate-search-table []
+ (local tx (luna.db.begin db))
+ (luna.db.exec-tx tx "DELETE FROM search" [])
+ (luna.db.exec-tx tx "INSERT INTO search
+ SELECT title, id, 'products'
+ FROM products;" [])
+ (luna.db.commit tx))
+
+(store-products (artoftea.products))
+(store-products (ipuer.products))
+(store-products (ozchai.products))
+(populate-search-table)