Implement concurrency
[tt.git] / tt
CommitLineData
4764ff89
SK
1#! /usr/bin/env racket
2; vim: filetype=racket
3
e5c3ae92 4; TODO optional text wrap
d6a187b7
SK
5; TODO write
6; TODO caching (use cache by default, unless explicitly asked for update)
7; TODO timeline limits
8; TODO user-defined feed sets (a la twitter lists)
9; TODO feed set operations
10; TODO timeline as a result of a query (feed set op + filter expressions)
11; TODO named timelines
12; TODO CLI params
13; TODO config files
895a32cf
SK
14; TODO parse "following" from feed
15; - following = <nick> <uri>
93ebe03e
SK
16; TODO parse mentions:
17; - @<source.nick source.url> | @<source.url>
d6a187b7
SK
18; TODO highlight mentions
19; TODO filter on mentions
20; TODO highlight hashtags
21; TODO filter on hashtags
e0e0fb20 22; TODO hashtags as channels? initial hashtag special?
d6a187b7 23; TODO query language
e0e0fb20
SK
24; TODO console logger colors by level ('error)
25; TODO file logger ('debug)
dbccadf9
SK
26; TODO commands:
27; - r | read
28; - see timeline ops above
29; - w | write
93ebe03e 30; - arg or stdin
dbccadf9
SK
31; - nick expand to URI
32; - q | query
33; - see timeline ops above
34; - see hashtag and channels above
93ebe03e
SK
35; - d | download
36; - u | upload
37; - calls user-configured command to upload user's own feed file to their server
84152e25
SK
38; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
39; - requires configurability
40; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
d6a187b7 41
4764ff89
SK
42#lang racket
43
44(require racket/date)
45
46(require http-client)
47(require rfc3339-old)
48
b201e854 49(struct msg (tm_epoch tm_rfc3339 nick uri text))
4764ff89
SK
50(struct feed (nick uri))
51
895a32cf
SK
52(define (concurrent-filter-map num_workers f xs)
53 (define (make-worker id f)
54 (define parent (current-thread))
55 (λ ()
56 (define self (current-thread))
57 (define (work)
58 (thread-send parent (cons 'next self))
59 (match (thread-receive)
60 ['done (thread-send parent (cons 'exit id))]
61 [(cons 'unit x) (begin
62 (define y (f x))
63 (when y (thread-send parent (cons 'result y)))
64 (work))]))
65 (work)))
66 (define (dispatch ws xs ys)
67 (if (empty? ws)
68 ys
69 (match (thread-receive)
70 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
71 [(cons 'result y) (dispatch ws xs (cons y ys))]
72 [(cons 'next thd) (match xs
73 ['() (begin
74 (thread-send thd 'done)
75 (dispatch ws xs ys))]
76 [(cons x xs) (begin
77 (thread-send thd (cons 'unit x))
78 (dispatch ws xs ys))])])))
79 (define workers
80 (range 1 (add1 num_workers)))
81 (define threads
82 (map (λ (id) (thread (make-worker id f))) workers))
83 (define results
84 (dispatch workers xs '()))
85 (for-each thread-wait threads)
86 results)
87
b201e854
SK
88(define (msg-print out-format odd msg)
89 (printf
90 (match out-format
91 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
92 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
93 [_ (raise (format "Invalid output format: ~a" out-format))])
94 (date->string (seconds->date [msg-tm_epoch msg]) #t)
95 (msg-nick msg)
96 (msg-uri msg)
97 (if odd 36 33)
98 (msg-text msg)))
e96264cc
SK
99
100(define re-msg-begin
101 ; TODO Zulu offset. Maybe in several formats. Which ones?
102 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
88d50b3e 103
b201e854 104(define (str->msg nick uri str)
88d50b3e
SK
105 (if (not (regexp-match? re-msg-begin str))
106 (begin
107 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
108 #f)
109 (let ([toks (string-split str (regexp "\t+"))])
110 (if (not (= 2 (length toks)))
111 (begin
112 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
113 #f)
114 (let*
115 ([tm_rfc3339 (list-ref toks 0)]
116 [tok_text (list-ref toks 1)]
117 [t (string->rfc3339-record tm_rfc3339)]
118 ; TODO handle tz offset
119 [tm_epoch (find-seconds [rfc3339-record:second t]
120 [rfc3339-record:minute t]
121 [rfc3339-record:hour t]
122 [rfc3339-record:mday t]
123 [rfc3339-record:month t]
124 [rfc3339-record:year t])])
b201e854 125 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
88d50b3e 126
e96264cc
SK
127(define (str->lines str)
128 (string-split str (regexp "[\r\n]+")))
129
b201e854
SK
130(define (str->msgs nick uri str)
131 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
4764ff89
SK
132
133(define (uri-fetch uri)
134 (log-info "GET ~a" uri)
135 (define resp (http-get uri))
136 (define status (http-response-code resp))
137 (define body (http-response-body resp))
138 (log-debug "finished GET ~a status:~a body length:~a"
139 uri status (string-length body))
140 ; TODO Handle redirects
141 (if (= status 200) body (raise status)))
142
b201e854 143(define (timeline-print out-format timeline)
4764ff89
SK
144 (for ([msg timeline]
145 [i (in-naturals)])
b201e854 146 (msg-print out-format (odd? i) msg)))
4764ff89 147
9a6a9f9a
SK
148(define (feed->msgs feed)
149 (log-info "downloading feed nick:~a uri:~a"
150 (feed-nick feed)
151 (feed-uri feed))
152 (with-handlers
153 ([exn:fail:network?
154 (λ (e)
155 (log-error "network error nick:~a uri:~a exn:~a"
156 (feed-nick feed)
157 (feed-uri feed)
158 e)
159 #f)]
160 [integer?
161 (λ (status)
162 (log-error "http error nick:~a uri:~a status:~a"
163 (feed-nick feed)
164 (feed-uri feed)
165 status)
166 #f)])
b201e854
SK
167 (define uri (feed-uri feed))
168 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
9a6a9f9a
SK
169
170; TODO timeline contract : time-sorted list of messages
895a32cf
SK
171(define (timeline num_workers feeds)
172 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
9a6a9f9a 173 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
4764ff89
SK
174
175(define (we-are-twtxt)
176 (let* ([uri
177 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
178 [payload
179 (uri-fetch uri)]
180 [lines
181 (str->lines payload)]
182 [feeds
183 (map (λ (line)
9e09e799 184 ; TODO validation
4764ff89
SK
185 (define toks (string-split line))
186 (feed
187 [list-ref toks 0]
188 [list-ref toks 1]))
189 lines)])
190 feeds))
191
e96264cc 192(define (setup-logging)
4764ff89
SK
193 (define logger (make-logger #f #f 'debug #f))
194 (define log-chan (make-log-receiver logger 'debug))
195 (void (thread (λ ()
196 [date-display-format 'iso-8601]
197 [let loop ()
198 (define data (sync log-chan))
199 (define level (vector-ref data 0))
200 (define msg (vector-ref data 1))
201 (define ts (date->string (current-date) #t))
202 (eprintf "~a [~a] ~a~n" ts level msg)
203 (loop)])))
204 (current-logger logger))
205
206(define (main)
e96264cc 207 (setup-logging)
4764ff89 208 (current-http-response-auto #f)
895a32cf 209 (current-http-user-agent "xandkar/tt 0.1.0")
4764ff89 210 (date-display-format 'rfc2822)
e96264cc
SK
211
212 (define feeds (we-are-twtxt))
b201e854 213 (define out-format 'multi-line)
895a32cf
SK
214 (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
215 (timeline-print out-format (timeline num_workers feeds)))
4764ff89
SK
216
217(main)
This page took 0.050382 seconds and 4 git commands to generate.