From fdea10a6481920b3bd097dd920269754c3a72dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 18 Mar 2019 18:24:24 +0100 Subject: Better cache. --- main.scm | 71 ++++++++++++++++++++++++++++++++++++++++++++++------------------ ttv | 4 +--- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/main.scm b/main.scm index fd4489e..320df14 100755 --- a/main.scm +++ b/main.scm @@ -114,34 +114,65 @@ (vector-fold (lambda (i accum el) (max accum (hashq-ref el 'date_updated_unix))) 0 vect)) +(define (cache-dir) + (string-append + (or (getenv "XDG_CACHE_HOME") + (and=> (getenv "HOME") (cut string-append <> "/.cache")) + "/tmp") + "/texttv/")) + +(define (cfile path) + "Gives path to file in cache directory." + (string-append (cache-dir) path)) + +(define* (display-status-bar n max #:optional (port #t)) + (let* ((progress (/ n max)) + (trnc (truncate (* 60 progress)))) + (format port + "\rProgress [~a~a] ~3d%" + (make-string trnc #\#) + (make-string (- 60 trnc) #\-) + (truncate (* 100 progress))))) + (define (main args) (let* (((self filename pagestr) args) (page (string->number pagestr))) - (let ((last-updated (call-with-input-file ".last-updated" read))) + (unless (file-exists? (cache-dir)) + (mkdir (cache-dir)) + (with-output-to-file (cfile "last-updated") + (lambda () (display 0)))) + + (let ((last-updated (call-with-input-file (cfile "last-updated") read))) (when (> (- (current-time) last-updated) 10000) (display "Downloading new data, please stand by...") (system* "curl" "-s" "http://api.texttv.nu/api/get/100-999?app=hugonikanor" - "-o" filename) + "-o" (cfile filename)) - (let* ((json (get-json filename)) + (let* ((json (get-json (cfile filename))) (update (max-date json))) - (with-output-to-file ".last-updated" (lambda () (display update))) - - (vector-for-each - (lambda (i el) - (let ((num (hashq-ref el 'num)) - (text (vector-ref (hashq-ref el 'content) 0))) - (with-output-to-file (format #f "cache/~a.ansi" num) - (lambda () (-> (parse-html-string text) - fmt-tag - (run-state (list (make-fmt-frame "" "" ""))) - car display))))) - json))) - - (let ((p1 (format #f "cache/~a.ansi" page)) - (p2 (format #f "cache/~a.ansi" (1+ page)))) - (system* "paste" p1 p2)) - ))) + (with-output-to-file (cfile "last-updated") (lambda () (display update))) + + (let ((vlen (vector-length json))) + (format (current-error-port) + "~%Rendering HTML~%") + (vector-for-each + (lambda (i el) + (let ((num (hashq-ref el 'num)) + (text (vector-ref (hashq-ref el 'content) 0))) + (with-output-to-file (cfile (format #f "~a.ansi" num)) + (lambda () + (display-status-bar i vlen (current-error-port)) + (-> (parse-html-string text) + fmt-tag + (run-state (list (make-fmt-frame "" "" ""))) + car display))))) + json)) + (display-status-bar 1 1 (current-error-port)) + (newline (current-error-port)))) + + (let ((p1 (cfile (format #f "~a.ansi" page))) + (p2 (cfile (format #f "~a.ansi" (1+ page))))) + (system* "paste" p1 p2))))) diff --git a/ttv b/ttv index 6e527c6..2bcbbb4 100755 --- a/ttv +++ b/ttv @@ -2,9 +2,7 @@ pushd $(dirname $(realpath $0)) > /dev/null -LD_LIBRARY_PATH=$PWD - file=texttv.json page=${1:- 100} -./main.scm $file $page +env LD_LIBRARY_PATH=$PWD ./main.scm $file $page -- cgit v1.2.3