Skip to content

Commit

Permalink
better way to find single-file and chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
qnkhuat committed Jan 23, 2023
1 parent 847805e commit ebffc31
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions PROJECT_MANAGEMENT_TM.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The so called "Project Management" of Ubinote

# Phase 2
- can add/edit/delete comments
- Finetune:
- clean way to install single-file-cli, maybe provide an env to set chrome-bin

# Phase 3
- PDF
Expand Down
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
cheshire/cheshire {:mvn/version "5.10.2"} ; fast JSON encoding (used by Ring JSON middleware)
potemkin/potemkin {:mvn/version "0.4.5"} ; utility macros & fns
org.mindrot/jbcrypt {:mvn/version "0.4"} ; Crypto library
org.clojure/core.memoize {:mvn/version "1.0.257"} ; Memoize functions

;; webserver+routing
compojure/compojure {:mvn/version "1.6.2"}
Expand Down
62 changes: 34 additions & 28 deletions src/ubinote/cmd.clj
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
(ns ubinote.cmd
(:require [ubinote.util.fs :as fs]
[clojure.string :as string]
[clojure.java.shell :refer [sh]]
[clojure.tools.logging :as log]))
(:require
[clojure.core.memoize :as memoize]
[clojure.java.shell :refer [sh]]
[clojure.string :as str]
[clojure.tools.logging :as log]
[ubinote.util.fs :as fs]))

(defn which
(def ^:private which
"like `which` command"
[exe]
(if (fs/absolute? exe)
(fs/executable? exe)
(fs/find-in (-> (fs/env-path)
(string/split (re-pattern fs/path-separator)))
exe fs/executable?)))
(memoize/ttl
(fn [exe]
(some-> (sh "which" exe)
:out
str/trim))
:ttl/threshold (* 1000 1)))

(defn find-chrome-binary
[]
(first (filter (fn [path] (which path))
["chromium-browser"
"chromium"
"/Applications/Chromium.app/Contents/MacOS/Chromium"
"chrome"
"google-chrome"
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
"google-chrome-stable"
"google-chrome-beta"
"google-chrome-canary"
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
"google-chrome-unstable"
"google-chrome-dev"])))
(def ^:private find-chrome-binary
(memoize/ttl
(fn []
(first (filter (fn [path]
(let [exe (which path)]
(and (not (str/blank? exe)) (fs/executable? exe))))
["chromium-browser"
"chromium"
"/Applications/Chromium.app/Contents/MacOS/Chromium"
"chrome"
"google-chrome"
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
"google-chrome-stable"
"google-chrome-beta"
"google-chrome-canary"
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
"google-chrome-unstable"
"google-chrome-dev"])))
:ttl/threshold (* 1000 1)))

(defn single-file
"download an url as a single fs with name is the page {title}-{time-locale}.html"
Expand All @@ -39,8 +45,8 @@
;; TODO maybe call an OPTIONs to the endpoint to check if it's reachable
(let [chrome-bin (find-chrome-binary)
single-file-bin (which "single-file")
;; https://github.com/gildas-lormeau/SingleFile/tree/master/cli
;; to install npm install -g "gildas-lormeau/single-file-cli"
;; https://github.com/gildas-lormeau/single-file-cli
;; to install npm install -g "single-file-cli"
args (filter some? [single-file-bin (format "--browser-executable-path=%s" chrome-bin)
(format "--filename-template={page-title}-{time-locale}.html")
url out-path])
Expand Down
2 changes: 1 addition & 1 deletion src/ubinote/models/page.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
(let [{:keys [relative absolute]} (out-path url)
domain (get-domain url)
{:keys [err]} (cmd/single-file url absolute)
_ (api/check-400 (= err "") {:url "Failed to download single-file"})
_ (api/check-400 (= err "") {:errors (format "Failed to archive %s" err)})
{:keys [title]} (extract-html absolute)]
;; TODO: move the file after download to name with title
(db/insert! Page (assoc new-page
Expand Down

0 comments on commit ebffc31

Please sign in to comment.