Fix variables for latest http-client update
[tt.git] / tt.rkt
CommitLineData
4764ff89
SK
1#lang racket
2
1d753430 3(require openssl/sha1)
4764ff89
SK
4(require racket/date)
5
6(require http-client)
7(require rfc3339-old)
8
78398948 9(module+ test
de3ff448 10 (require rackunit))
78398948 11
87d0eb92 12(struct msg (ts_epoch ts_rfc3339 nick uri text))
4764ff89
SK
13(struct feed (nick uri))
14
895a32cf 15(define (concurrent-filter-map num_workers f xs)
dad4504d 16 ; TODO preserve order of elements OR communicate that reorder is expected
a239a233 17 ; TODO switch from mailboxes to channels
895a32cf
SK
18 (define (make-worker id f)
19 (define parent (current-thread))
20 (λ ()
21 (define self (current-thread))
22 (define (work)
23 (thread-send parent (cons 'next self))
24 (match (thread-receive)
c562bea3
SK
25 ['done (thread-send parent (cons 'exit id))]
26 [(cons 'unit x) (begin
27 (define y (f x))
28 (when y (thread-send parent (cons 'result y)))
29 (work))]))
895a32cf
SK
30 (work)))
31 (define (dispatch ws xs ys)
32 (if (empty? ws)
f1493e49
SK
33 ys
34 (match (thread-receive)
c562bea3
SK
35 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
36 [(cons 'result y) (dispatch ws xs (cons y ys))]
37 [(cons 'next thd) (match xs
38 ['() (begin
39 (thread-send thd 'done)
40 (dispatch ws xs ys))]
41 [(cons x xs) (begin
42 (thread-send thd (cons 'unit x))
43 (dispatch ws xs ys))])])))
9926c9a9
SK
44 (define workers (range num_workers))
45 (define threads (map (λ (id) (thread (make-worker id f))) workers))
46 (define results (dispatch workers xs '()))
895a32cf
SK
47 (for-each thread-wait threads)
48 results)
49
dad4504d 50(module+ test
de3ff448
SK
51 (let* ([f (λ (x) (if (even? x) x #f))]
52 [xs (range 11)]
53 [actual (sort (concurrent-filter-map 10 f xs) <)]
54 [expected (sort ( filter-map f xs) <)])
c562bea3 55 (check-equal? actual expected "concurrent-filter-map")))
dad4504d 56
3d042e75
SK
57(define msg-print
58 (let* ([colors (vector 36 33)]
59 [n (vector-length colors)])
60 (λ (out-format color-i msg)
01e4c499
SK
61 (let ([color (vector-ref colors (modulo color-i n))]
62 [nick (msg-nick msg)]
63 [uri (msg-uri msg)]
64 [text (msg-text msg)])
3d042e75 65 (match out-format
01e4c499
SK
66 ['single-line
67 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
68 (parameterize ([date-display-format 'iso-8601])
69 (date->string (seconds->date [msg-ts_epoch msg]) #t))
70 nick color text)]
71 ['multi-line
72 (printf "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
73 (parameterize ([date-display-format 'rfc2822])
74 (date->string (seconds->date [msg-ts_epoch msg]) #t))
75 nick uri color text)])))))
e96264cc
SK
76
77(define re-msg-begin
78 ; TODO Zulu offset. Maybe in several formats. Which ones?
79 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
88d50b3e 80
b201e854 81(define (str->msg nick uri str)
88d50b3e 82 (if (not (regexp-match? re-msg-begin str))
f1493e49
SK
83 (begin
84 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
85 #f)
86 (let ([toks (string-split str (regexp "\t+"))])
c562bea3
SK
87 (if (not (= 2 (length toks)))
88 (begin
89 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
90 #f)
91 (let*
92 ([ts_rfc3339 (first toks)]
93 [text (second toks)]
94 [t (string->rfc3339-record ts_rfc3339)]
95 ; TODO handle tz offset
96 [ts_epoch (find-seconds [rfc3339-record:second t]
97 [rfc3339-record:minute t]
98 [rfc3339-record:hour t]
99 [rfc3339-record:mday t]
100 [rfc3339-record:month t]
101 [rfc3339-record:year t])])
102 (msg ts_epoch ts_rfc3339 nick uri text))))))
88d50b3e 103
63afa259 104(module+ test
de3ff448
SK
105 (let* ([ts "2020-11-18T22:22:09-0500"]
106 [tab " "]
107 [text "Lorem ipsum"]
108 [nick "foo"]
109 [uri "bar"]
110 [actual (str->msg nick uri (string-append ts tab text))]
111 [expected (msg 1605756129 ts nick uri text)])
c562bea3
SK
112 ; FIXME re-enable after handling tz offset
113 ;(check-equal?
114 ; (msg-ts_epoch actual)
115 ; (msg-ts_epoch expected)
116 ; "str->msg ts_epoch")
117 (check-equal?
118 (msg-ts_rfc3339 actual)
119 (msg-ts_rfc3339 expected)
120 "str->msg ts_rfc3339")
121 (check-equal?
122 (msg-nick actual)
123 (msg-nick expected)
124 "str->msg nick")
125 (check-equal?
126 (msg-uri actual)
127 (msg-uri expected)
128 "str->msg uri")
129 (check-equal?
130 (msg-text actual)
131 (msg-text expected)
132 "str->msg text")))
63afa259 133
e96264cc
SK
134(define (str->lines str)
135 (string-split str (regexp "[\r\n]+")))
136
63afa259 137(module+ test
de3ff448 138 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
63afa259 139
b201e854
SK
140(define (str->msgs nick uri str)
141 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
4764ff89 142
1d753430
SK
143(define (hash-sha1 str)
144 (define in (open-input-string str))
145 (define digest (sha1 in))
146 (close-input-port in)
147 digest)
148
0e16a46c
SK
149(define (uri-fetch use-cache uri)
150 (define cache-file-path
151 (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
152 (if (and use-cache (file-exists? cache-file-path))
153 (begin
154 (log-info "uri-fetch cached ~a" uri)
155 (file->string cache-file-path))
156 (begin
157 (log-info "uri-fetch new ~a" uri)
01e4c499 158 ; TODO Timeout. Currently hangs on slow connections.
0e16a46c
SK
159 (let* ([resp (http-get uri)]
160 [status (http-response-code resp)]
161 [body (http-response-body resp)])
c562bea3
SK
162 (log-debug "finished GET ~a status:~a body length:~a"
163 uri status (string-length body))
164 ; TODO Handle redirects
165 (if (= status 200)
166 (begin
167 (display-to-file body cache-file-path #:exists 'replace)
168 body)
169 ; TODO A more-informative exception
170 (raise status))))))
4764ff89 171
b201e854 172(define (timeline-print out-format timeline)
3d042e75
SK
173 (void (foldl (match-lambda**
174 [((and m (msg _ _ nick _ _)) (cons prev-nick i))
175 (let ([i (if (string=? prev-nick nick) i (+ 1 i))])
176 (msg-print out-format i m)
177 (cons nick i))])
178 (cons "" 0)
179 timeline)))
4764ff89 180
0e16a46c 181(define (feed->msgs use-cache feed)
9a6a9f9a
SK
182 (log-info "downloading feed nick:~a uri:~a"
183 (feed-nick feed)
184 (feed-uri feed))
185 (with-handlers
186 ([exn:fail:network?
187 (λ (e)
188 (log-error "network error nick:~a uri:~a exn:~a"
189 (feed-nick feed)
190 (feed-uri feed)
191 e)
192 #f)]
193 [integer?
194 (λ (status)
195 (log-error "http error nick:~a uri:~a status:~a"
196 (feed-nick feed)
197 (feed-uri feed)
198 status)
199 #f)])
b201e854 200 (define uri (feed-uri feed))
0e16a46c 201 (str->msgs [feed-nick feed] uri [uri-fetch use-cache uri])))
9a6a9f9a
SK
202
203; TODO timeline contract : time-sorted list of messages
0e16a46c
SK
204(define (timeline use-cache num_workers feeds)
205 (sort (append* (concurrent-filter-map num_workers (curry feed->msgs use-cache) feeds))
87d0eb92 206 (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
4764ff89 207
be96956c
SK
208(define (str->feed str)
209 ; TODO validation
210 (define toks (string-split str))
87d0eb92 211 (apply feed toks))
be96956c
SK
212
213(define (str->feeds str)
214 (map str->feed (str->lines str)))
215
216(define (file->feeds filename)
217 (str->feeds (file->string filename)))
218
4f3dbb24 219(define (user-agent prog-name prog-version)
24c6a76b 220 (let*
4f3dbb24 221 ([prog-uri "https://github.com/xandkar/tt"]
24c6a76b
SK
222 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
223 [user
224 (if (file-exists? user-feed-file)
f1493e49 225 (let ([user (first (file->feeds user-feed-file))])
c562bea3 226 (format "+~a; @~a" (feed-uri user) (feed-nick user)))
f1493e49 227 (format "+~a" prog-uri))]
24c6a76b
SK
228 )
229 (format "~a/~a (~a)" prog-name prog-version user)))
230
01e4c499
SK
231(define (start-logger level)
232 (let* ([logger (make-logger #f #f level #f)]
233 [log-receiver (make-log-receiver logger level)])
234 (void (thread (λ ()
235 (parameterize
236 ([date-display-format 'iso-8601])
237 (let loop ()
238 (define data (sync log-receiver))
239 (define level (vector-ref data 0))
240 (define msg (vector-ref data 1))
241 (define ts (date->string (current-date) #t))
242 (eprintf "~a [~a] ~a~n" ts level msg)
243 (loop))))))
244 (current-logger logger)))
245
24c6a76b 246(module+ main
de3ff448 247 (require setup/getinfo)
26d172a9 248
dd3fa56a 249 (current-http-client/response-auto #f)
de3ff448
SK
250 (let* ([prog-name "tt"]
251 [prog-version ((get-info (list prog-name)) 'version)]
252 [user-agent (user-agent prog-name prog-version)])
dd3fa56a 253 (current-http-client/user-agent user-agent))
de3ff448
SK
254 (let* ([use-cache
255 #f]
01e4c499
SK
256 [log-level
257 'info]
de3ff448
SK
258 [out-format
259 'multi-line]
260 [num_workers
261 15]) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
c562bea3
SK
262 (command-line
263 #:once-each
264 [("-c" "--cached")
265 "Read cached data instead of downloading."
266 (set! use-cache #t)]
0e16a46c 267
01e4c499
SK
268 [("-d" "--debug")
269 "Enable debug log level."
270 (set! log-level 'debug)]
271
c562bea3
SK
272 [("-j" "--jobs")
273 njobs "Number of concurrent jobs."
274 (set! num_workers (string->number njobs))]
0e16a46c 275
01e4c499
SK
276 #:once-any
277 [("-s" "--short")
278 "Short output format"
279 (set! out-format 'single-line)]
0e16a46c 280
01e4c499
SK
281 [("-l" "--long")
282 "Long output format"
283 (set! out-format 'multi-line)]
284
285 #:args (filename)
286 (start-logger log-level)
c562bea3
SK
287 (timeline-print out-format
288 (timeline use-cache
289 num_workers
290 (file->feeds filename))))))
This page took 0.08022 seconds and 4 git commands to generate.