(import-macros {: map : reduce : filter} :lib.macro) (tset package :path (.. package.path ";./vendor/lpeglj/?.lua")) (local io (require :io)) (local math (require :math)) (local fennel (require :vendor.fennel)) (local html (require :vendor.html)) (local json (require :vendor.json)) (local array (require :lib.array)) (local str (require :lib.string)) (local texts (require :texts)) (local fs (require :lib.fs)) (local spellfix (require :lib.spellfix)) (local cache (require :lib.cache)) (local {: must : measure} (require :lib.utils)) (when _G.unpack (tset table :unpack _G.unpack)) (local db (must (luna.db.open "file:var/db.sqlite?_journal=WAL&_sync=NORMAL"))) (local query-synonyms { "шэн" "шен" "шен" "шэн" "дянь" "дань" "дань" "дянь" "чжень" "чжэнь" "чжэнь" "чжень" "хун" "цун" "цун" "хун" "доска" "чабань" "лунцзин" "лун цзин" "лун цзин" "лунцзин" "чахай" "сливник" "сливник" "чахай" "чабань" "доска" "термос" "бутылка" "бутылка" "термос"}) (fn unescape [s] (assert (= (type s) :string) "s must be string") (pick-values 1 (-> s (string.gsub "<" "<") (string.gsub ">" ">") (string.gsub """ "\"") (string.gsub "&" "&")))) (fn category-menu-path [category] (. {"Красный чай" "red-tea" "Шен пуэр" "sheng-puer" "Шу пуэр" "shou-puer" "Улун" "oolong" "Зеленый чай" "green-tea" "Белый чай" "white-tea" "Желтый чай" "yellow-tea" "Посуда" "teaware"} category)) (fn get-query-string [query key] (if (and query (. query key) (. query key 1) (< 0 (# (. query key 1)))) (. query key 1) nil)) (fn get-query-number [query key] (if (and query (. query key) (. query key 1) (< 0 (# (. query key 1)))) (tonumber (. query key 1)) nil)) (fn serialize-query [query] (local flattened-object (collect [k v (pairs query)] (values k (array.join (map (fn [_ vv] (.. k "=" vv)) v) "&")))) (array.join (array.list flattened-object) "&")) (fn collect-form [params category] {:query (str.trim (or (get-query-string params "query") "")) :tags (filter #(~= "" $2) (or params.tags [category])) :site (str.trim (or (get-query-string params "site") "")) :min-price (get-query-number params "min-price") :max-price (get-query-number params "max-price") :price-per (= "on" (get-query-string params "price-per"))}) (fn form-empty? [form] (and (= "" form.query) (= (# form.tags) 0) (= "" form.site) (not form.min-price) (not form.max-price) ;; price-per is intentionally left out since it must not trigger search ;; by itself )) (fn form->path [page form] (.. "?page=" (tostring page) (if (not (str.empty? form.query)) (.. "&query=" form.query) "") (if (< 0 (# form.tags)) (.. "&" (array.join (array.flatten (map (fn [_ tag] (.. "tags=" tag)) form.tags)) "&")) "") (if (not (str.empty? form.site)) (.. "&site=" form.site) "") (if (and form.min-price (< 0 form.min-price)) (.. "&min-price=" (tostring form.min-price)) "") (if (and form.max-price (< 0 form.max-price)) (.. "&max-price=" (tostring form.max-price)) "") (if form.price-per "&price-per=on" ""))) (fn random-products [limit] (assert (< 0 limit) "limit must be > 0") (must (luna.db.query* db "SELECT site, title, description, image, url, price, weight, price_per AS \"price-per\", year FROM products WHERE archived = false ORDER BY ROW_NUMBER() over (PARTITION BY site ORDER BY ROWID) LIMIT ?" [limit]))) (fn all-tags [] (map (fn [_ v] (. v 1)) (must (luna.db.query db "SELECT title FROM tags ORDER BY creation_time" [])))) (fn query-products [{: query : tags : min-price : max-price : price-per : site} page] (local tags (or tags [])) (var where-conds []) (var where-vars []) (when (< 0 (# tags)) (each [_ tag (pairs tags)] (table.insert where-conds "product_tags.tag = ?") (table.insert where-vars tag))) (when (and query (< 0 (# query))) (table.insert where-conds "search.title MATCH ?") (table.insert where-vars (array.join (map (fn [_ q] (local lower-q (must (luna.utf8.lower q))) (if (. query-synonyms lower-q) (.. "(" q "* OR " (. query-synonyms lower-q) "*)") (.. q "*"))) (str.split query " ")) " AND "))) (when (not (str.empty? site)) (table.insert where-conds "products.site = ?") (table.insert where-vars site)) (when (and min-price (< 0 min-price)) (if price-per (table.insert where-conds "products.price_per >= ?") (table.insert where-conds "products.price >= ?")) (table.insert where-vars min-price)) (when (and max-price (< 0 max-price)) (if price-per (table.insert where-conds "products.price_per <= ?") (table.insert where-conds "products.price <= ?")) (table.insert where-vars max-price)) (local where-sql (if (< 0 (# where-conds)) (.. "AND " (array.join where-conds "\nAND ")) "")) (local total (must (luna.db.query db (string.format "SELECT count(*) FROM search INNER JOIN products ON search.fid = products.url LEFT JOIN product_tags ON product_tags.product = search.fid WHERE search.`table` = 'products' AND products.archived = false %s" where-sql) where-vars))) {:results (must (luna.db.query* db (string.format "SELECT products.title, products.site, products.description, products.image, products.url, products.price, products.weight, products.price_per AS \"price-per\", products.year, products.archived, products.creation_time AS \"creation-time\" FROM search INNER JOIN products ON search.fid = products.url LEFT JOIN product_tags ON product_tags.product = products.url WHERE search.`table` = 'products' AND products.archived = false %s ORDER BY ROW_NUMBER() over (PARTITION BY products.site ORDER BY products.ROWID), rank LIMIT 48 OFFSET ?" where-sql) (array.concat where-vars [(* (- page 1) 48)]))) :total (if (< 0 (# total)) (. total 1 1) 0)}) (fn site-name-template [name] (local module (require (.. "parser." name))) [:a {:class "site-icon" :href (.. module.url "?from=everytea.ru")} [:img {:src (.. "/static/" name ".png") :alt (.. "Логотип " module.title)}] module.title]) (fn item-template [product] (local link (.. product.url "?from=everytea.ru")) [:div {:class "tile"} [:a {:href link :class "img-link" :rel "nofollow"} [:img {:class "img" :src product.image :title product.title :alt product.title} ""]] (site-name-template product.site) [:a {:href link :style "text-decoration: none;" :rel "nofollow"} [:NO-ESCAPE (.. "