aboutsummaryrefslogtreecommitdiff
path: root/main.scm
diff options
context:
space:
mode:
Diffstat (limited to 'main.scm')
-rwxr-xr-xmain.scm71
1 files changed, 51 insertions, 20 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)))))