12 (struct msg (ts_epoch ts_rfc3339 nick uri text))
13 (struct feed (nick uri))
15 (define (concurrent-filter-map num_workers f xs)
16 ; TODO preserve order of elements OR communicate that reorder is expected
17 ; TODO switch from mailboxes to channels
18 (define (make-worker id f)
19 (define parent (current-thread))
21 (define self (current-thread))
23 (thread-send parent (cons 'next self))
24 (match (thread-receive)
25 ['done (thread-send parent (cons 'exit id))]
26 [(cons 'unit x) (begin
28 (when y (thread-send parent (cons 'result y)))
31 (define (dispatch ws xs ys)
34 (match (thread-receive)
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
39 (thread-send thd 'done)
42 (thread-send thd (cons 'unit x))
43 (dispatch ws xs ys))])])))
44 (define workers (range num_workers))
45 (define threads (map (λ (id) (thread (make-worker id f))) workers))
46 (define results (dispatch workers xs '()))
47 (for-each thread-wait threads)
51 (let* ([f (λ (x) (if (even? x) x #f))]
53 [actual (sort (concurrent-filter-map 10 f xs) <)]
54 [expected (sort ( filter-map f xs) <)])
55 (check-equal? actual expected "concurrent-filter-map")))
58 (let* ([colors (vector 36 33)]
59 [n (vector-length colors)])
60 (λ (out-format color-i msg)
61 (let ([color (vector-ref colors (modulo color-i n))]
64 [text (msg-text msg)])
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))
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)])))))
77 ; TODO Implement rfc3339->epoch and remove dependency on rfc3339-old
80 (let ([re (pregexp "^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(:[0-9]{2})?)(\\.[0-9]+)?([^\\s\t]*)[\\s\t]+(.*)$")])
85 (log-error "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v" str nick uri e)
87 (match (regexp-match re str)
88 [(list _wholething ts s _fractional tz text)
90 ([ts_rfc3339 (string-append ts (if s "" ":00") (if tz tz ""))]
91 [t (string->rfc3339-record ts_rfc3339)]
92 [s (rfc3339-record:second t)]
93 ; TODO handle tz offset
94 [ts_epoch (find-seconds [if s s 0]
95 [rfc3339-record:minute t]
96 [rfc3339-record:hour t]
97 [rfc3339-record:mday t]
98 [rfc3339-record:month t]
99 [rfc3339-record:year t])])
100 (msg ts_epoch ts_rfc3339 nick uri text))]
102 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
106 (let* ([tzs (for*/list ([d '("-" "+")]
108 [m '("00" ":00" "57" ":57")])
109 (string-append d h m))]
110 [tzs (list* "" "Z" tzs)])
111 (for* ([n '("fake-nick")]
116 [sep (list "\t" " ")]
117 [txt '("foo bar baz" "'jaz poop bear giraffe / tea" "@*\"``")])
118 (let* ([ts (string-append "2020-11-18T22:22"
119 (if (non-empty-string? s) s ":00")
121 [m (str->msg n u (string-append ts sep txt))])
123 (check-equal? (msg-nick m) n)
124 (check-equal? (msg-uri m) u)
125 (check-equal? (msg-text m) txt)
126 (check-equal? (msg-ts_rfc3339 m) ts (format "Given: ~v" ts))
129 (let* ([ts "2020-11-18T22:22:09-0500"]
134 [actual (str->msg nick uri (string-append ts tab text))]
135 [expected (msg 1605756129 ts nick uri text)])
136 ; FIXME re-enable after handling tz offset
138 ; (msg-ts_epoch actual)
139 ; (msg-ts_epoch expected)
140 ; "str->msg ts_epoch")
142 (msg-ts_rfc3339 actual)
143 (msg-ts_rfc3339 expected)
144 "str->msg ts_rfc3339")
158 (define (str->lines str)
159 (string-split str (regexp "[\r\n]+")))
162 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
164 (define (str->msgs nick uri str)
165 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
167 (define (hash-sha1 str)
168 (define in (open-input-string str))
169 (define digest (sha1 in))
170 (close-input-port in)
173 (define (uri-read-cached uri)
174 (define cache-file-path
175 (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
176 (if (file-exists? cache-file-path)
177 (file->string cache-file-path)
179 (log-warning "Cache file not found for URI: ~a" uri)
182 ; uri-download : String -> Void
183 (define (uri-download uri)
184 (define cache-file-path
185 (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
186 (log-info "uri-download ~a" uri)
187 ; TODO Timeout. Currently hangs on slow connections.
188 (let* ([resp (http-get uri)]
189 [status (http-response-code resp)]
190 [body (http-response-body resp)])
191 (log-debug "finished GET ~a status:~a body length:~a"
192 uri status (string-length body))
193 ; TODO Handle redirects
195 (display-to-file body cache-file-path #:exists 'replace)
196 ; TODO A more-informative exception
199 (define (timeline-print out-format timeline)
200 (void (foldl (match-lambda**
201 [((and m (msg _ _ nick _ _)) (cons prev-nick i))
202 (let ([i (if (string=? prev-nick nick) i (+ 1 i))])
203 (msg-print out-format i m)
208 ; feed->msgs : Feed -> (Listof Msg)
209 (define (feed->msgs feed)
210 (log-info "Reading feed nick:~a uri:~a"
213 (define uri (feed-uri feed))
214 (str->msgs (feed-nick feed) uri (uri-read-cached uri)))
216 ; feed-download : Feed -> Void
217 (define (feed-download feed)
218 (log-info "Downloading feed nick:~a uri:~a"
224 (log-error "Network error nick:~a uri:~a exn:~a"
231 (log-error "HTTP error nick:~a uri:~a status:~a"
236 (uri-download (feed-uri feed))))
238 ; timeline-download : Integer -> (Listof Feed) -> Void
239 (define (timeline-download num_workers feeds)
240 ; TODO No need for map - can just iter
241 (void (concurrent-filter-map num_workers feed-download feeds)))
243 ; TODO timeline contract : time-sorted list of messages
244 (define (timeline-read feeds)
245 (sort (append* (filter-map feed->msgs feeds))
246 (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
248 (define (str->feed str)
250 (define toks (string-split str))
253 (define (filter-comments lines)
254 (filter-not (λ (line) (string-prefix? line "#")) lines))
256 (define (str->feeds str)
257 (map str->feed (filter-comments (str->lines str))))
259 (define (file->feeds filename)
260 (str->feeds (file->string filename)))
262 (define (user-agent prog-name prog-version)
264 ([prog-uri "https://github.com/xandkar/tt"]
265 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
267 (if (file-exists? user-feed-file)
268 (let ([user (first (file->feeds user-feed-file))])
269 (format "+~a; @~a" (feed-uri user) (feed-nick user)))
270 (format "+~a" prog-uri))])
271 (format "~a/~a (~a)" prog-name prog-version user)))
273 (define (start-logger level)
274 (let* ([logger (make-logger #f #f level #f)]
275 [log-receiver (make-log-receiver logger level)])
278 ([date-display-format 'iso-8601])
280 (define data (sync log-receiver))
281 (define level (vector-ref data 0))
282 (define msg (vector-ref data 1))
283 (define ts (date->string (current-date) #t))
284 (eprintf "~a [~a] ~a~n" ts level msg)
286 (current-logger logger)))
289 (require (prefix-in info: setup/getinfo))
291 (let ([log-level 'info])
297 "Enable debug log level."
298 (set! log-level 'debug)]
301 "and <command> is one of"
302 "r, read i : Read the timeline."
303 "d, download : Download the timeline."
305 #:args (command . args)
306 (start-logger log-level)
307 (current-command-line-arguments (list->vector args))
310 (let ([num_workers 15]) ; 15 was fastest out of the tried: 1, 5, 10, 20.
316 njobs "Number of concurrent jobs."
317 (set! num_workers (string->number njobs))]
321 (current-http-client/response-auto #f)
322 (let* ([prog-name "tt"]
323 [prog-version ((info:get-info (list prog-name)) 'version)]
324 [user-agent (user-agent prog-name prog-version)])
325 (current-http-client/user-agent user-agent))
326 (timeline-download num_workers (file->feeds filename))
329 (let ([out-format 'multi-line])
335 "Short output format"
336 (set! out-format 'single-line)]
339 (set! out-format 'multi-line)]
341 (timeline-print out-format (timeline-read (file->feeds filename)))))]