aboutsummaryrefslogtreecommitdiff
path: root/main.scm
blob: ef968b684e6c279ea497becca1c2f20854f2535a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/guile \
-q -e main -s
!#

(add-to-load-path (dirname (current-filename)))
(add-to-load-path (string-append (dirname (current-filename)) "/monad/"))

(add-to-load-path "/home/hugo/code/calp/module")  ; For (util), move that to a library

(use-modules (sxml match)

             (srfi srfi-1)
             (srfi srfi-43)             ; Vector iteration
             ((srfi srfi-60) :select (list->integer))
             (srfi srfi-71)

             (ice-9 regex)
             (ice-9 popen)
             (ice-9 match)
             (ice-9 format)

             (hnh util)
             (hnh util path)

             (monad)
             (monad state)
             (monad stack)

             (fmt-stack)
             (img-parse)


             (sxml gumbo)
             (json parser)

             ((xdg basedir) :prefix xdg-))

(define-syntax regex-case
  (syntax-rules (else)
    ((_ str var (else body ...))
     (begin body ...))

    ((_ str var (pat body ...) rest ...)
     (cond ((string-match pat str)
            => (lambda (var) body ...))
           (else (regex-case str var rest ...))))))

(define (class-handlers class)
  (fold (lambda (cl obj)
          (regex-case
           cl m
           ("^DH$"    (set-style obj 'bold))
           ("^bgImg$" obj)
           ("^bg([BCGMRWY]|Bl)$" (->> (match:substring m 1)
                                      string-upcase
                                      string->symbol
                                      (set-bg obj)))
           ("^([BCGMRWY]|bl)$"   (->> (match:substring m 1)
                                      string-upcase
                                      string->symbol
                                      (set-fg obj)))
           (else obj)))
        (empty-fmt-frame)
        (string-split class #\space)))

;;; TODO every push and pop should emit current ANSI-escape after it has run.

;;; TODO Every clause should return a string, in the state context of a state.

;; "<id>.gif": #\x<num>
(define bg-cache (make-hash-table))

(define (save-bg-cache)
  (with-output-to-file (cache-file "bg.scm")
    (lambda ()
      (write (hash-map->list list bg-cache))
      (newline))))

(define (load-bg-cache)
  (define file (cache-file "bg.scm"))
  (when (file-exists? file)
    (let ((forms (call-with-input-file file read)))
      (when (list? forms)
        (for-each (lambda (form) (apply hash-set! bg-cache form))
                  forms)))))


(define bg-img-url "https://l.texttv.nu/storage/chars")

(define (get-bg-image-char frame img)
  (or (and=> (hash-ref bg-cache img) string)
      (let ((img-file (cache-file (path-append "img" img))))
        (unless (file-exists? img-file)
          (download-file (format #f "~a/~a" bg-img-url img)
                         img-file))

        (let ((char
               (->> (map (lambda (x)
                           (cond ((eq? x (get-fg frame)) #t)
                                 ((eq? x (get-bg frame)) #f)
                                 (else (scm-error 'misc-error "get-bg-image-char"
                                                  "Color neither foreground or background. fg: ~s, bg: ~s, file: ~s"
                                                  (list (get-fg frame) (get-bg frame)
                                                        img-file)
                                                  #f))))
                         (parse-image-file img-file))
                    list->integer
                    (vector-ref lookup-table))))
          (hash-set! bg-cache img char)
          (return-state (string char))))))


(define bg-rx (make-regexp (format #f "background: url\\(~a/([^)]*)\\)" bg-img-url)))

(define (handle-h1-or-span class style nodes)
  (define (err fmt . args)
    (scm-error 'misc-error "handle-h1-or-span"
               fmt args
               #f))

  (do fmt-before <- (get-attr)
      (push (class-handlers class))
      fmt-with <- (get-attr)
      frame <- (get-frame)
      ret <- (<$> (lambda (s) (string-append fmt-with s fmt-before))
                  (if (member "bgImg" (string-split class #\space))
                      (cond ((regexp-exec bg-rx style)
                             => (lambda (m) (get-bg-image-char frame (match:substring m 1))))
                            (else (err "bgImg without background image style")))
                      (<$> string-concatenate (sequence (map fmt-tag nodes)))))

      (pop)
      (return-state ret)))

;;                  +-- State storage, "mutable"
;;                  |          +- Return value, static
;;                  V          V
;; sxml → State <fmt-stack> <string>
(define (fmt-tag tag)
  (sxml-match
   tag
   [(a ,body ...)
    (do fmt-before <- (get-attr)
        (push (make-fmt-frame 'underline #f #f))
        fmt-with <- (get-attr)
        (pop)
        (return-state
         (string-append fmt-with (car body) fmt-before)))]

   [(h1 (@ (class ,class) (style (,style ""))) ,nodes ...)
    (handle-h1-or-span class style nodes)]

   [(span (@ (class ,class) (style (,style ""))) ,nodes ...)
    (handle-h1-or-span class style nodes)]


   [,str (guard (string? str)) (return-state str)]

   [,default
    (match default
      ((tag) (return-state ""))
      ((tag ('@ args ...)) (return-state ""))
      ((tag ('@ args ...) nodes ...)
       (<$> string-concatenate (sequence (map fmt-tag nodes))))
      ((tag nodes ...)
       (<$> string-concatenate (sequence (map fmt-tag nodes))))
      (default (return-state (format #f "[|~a|]" default))))]))

(define (text-for-num j int)
  (let* ((table (vector-ref j (- int 100)))
         (arr (hashq-ref table 'content))
         (text (array-ref arr 0)))
    text))

(define (max-date vect)
  (-> (find-max (vector->list vect)
                (lambda (el) (assoc-ref el "date_updated_unix")))
      (assoc-ref "date_updated_unix")))

(define (cache-path)
  (path-append (xdg-cache-home) "texttv"))

(define (cache-file path)
  "Gives path to file in cache directory."
  (path-append (cache-path) 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 (build-cache-dir)
  (unless (file-exists? (cache-path))
    (mkdir (cache-path)))

  (let ((last-updated (cache-file "last-updated")))
    (unless (file-exists? last-updated)
      (with-output-to-file last-updated
        (lambda () (display 0)))))

  (unless (file-exists? (cache-file "img"))
    (mkdir (cache-file "img")))

  (unless (file-exists? (cache-file "rendered"))
    (mkdir (cache-file "rendered")))

  )

(define (render-page file el)
  (with-output-to-file file
    (lambda ()
      (-> el
          (assoc-ref "content")
          (vector-ref 0)
          (html->sxml #:trim-whitespace? #f
                      #:full-document? #f)
          fmt-tag
          (run-state (list (make-fmt-frame "" "" "")))
          car
          display))))


;; Render all pages in vector
(define (render-pages vect)
  (define (status i)
    (display-status-bar i (vector-length vect) (current-error-port)))

  (format (current-error-port) "~%Rendering HTML~%")

  (vector-for-each
   (lambda (i el)
     (status i)
     (render-page (cache-file (path-append
                               "rendered"
                               (format #f "~a.ansi" (assoc-ref el "num"))))
                  el))
   vect)

  (display-status-bar 1 1 (current-error-port))
  (newline (current-error-port)))

(define (download-file url destination)
  (system* "curl"
           "-s" url
           "-o" destination))

(define (main args)
  (let* ((self base-filename pagestr (apply values args))
         (json-cache (cache-file base-filename))
         (page (string->number pagestr)))

    (build-cache-dir)
    (load-bg-cache)

    (let ((last-updated
           (call-with-input-file (cache-file "last-updated") read)))
      (when (> (- (current-time) last-updated) 10000)
        (display "Downloading new data, please stand by...")
        (download-file "http://api.texttv.nu/api/get/100-999?app=hugonikanor"
                       json-cache)
        (newline)

        (let ((page-data (call-with-input-file json-cache json->scm)))
          (with-output-to-file (cache-file "last-updated")
            (lambda () (write (max-date page-data)) (newline)))
          (render-pages page-data))))

    (let ((p1 (cache-file (path-append "rendered" (format #f "~a.ansi" page))))
          (p2 (cache-file (path-append "rendered" (format #f "~a.ansi" (1+ page))))))
      (system* "paste" p1 p2))

    (save-bg-cache)))