Implement concurrency
[tt.git] / tt
1 #! /usr/bin/env racket
2 ; vim: filetype=racket
3
4 ; TODO optional text wrap
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
14 ; TODO parse "following" from feed
15 ; - following = <nick> <uri>
16 ; TODO parse mentions:
17 ; - @<source.nick source.url> | @<source.url>
18 ; TODO highlight mentions
19 ; TODO filter on mentions
20 ; TODO highlight hashtags
21 ; TODO filter on hashtags
22 ; TODO hashtags as channels? initial hashtag special?
23 ; TODO query language
24 ; TODO console logger colors by level ('error)
25 ; TODO file logger ('debug)
26 ; TODO commands:
27 ; - r | read
28 ; - see timeline ops above
29 ; - w | write
30 ; - arg or stdin
31 ; - nick expand to URI
32 ; - q | query
33 ; - see timeline ops above
34 ; - see hashtag and channels above
35 ; - d | download
36 ; - u | upload
37 ; - calls user-configured command to upload user's own feed file to their server
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
41
42 #lang racket
43
44 (require racket/date)
45
46 (require http-client)
47 (require rfc3339-old)
48
49 (struct msg (tm_epoch tm_rfc3339 nick uri text))
50 (struct feed (nick uri))
51
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
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)))
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}"))
103
104 (define (str->msg nick uri str)
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])])
125 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
126
127 (define (str->lines str)
128 (string-split str (regexp "[\r\n]+")))
129
130 (define (str->msgs nick uri str)
131 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
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
143 (define (timeline-print out-format timeline)
144 (for ([msg timeline]
145 [i (in-naturals)])
146 (msg-print out-format (odd? i) msg)))
147
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)])
167 (define uri (feed-uri feed))
168 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
169
170 ; TODO timeline contract : time-sorted list of messages
171 (define (timeline num_workers feeds)
172 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
173 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
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)
184 ; TODO validation
185 (define toks (string-split line))
186 (feed
187 [list-ref toks 0]
188 [list-ref toks 1]))
189 lines)])
190 feeds))
191
192 (define (setup-logging)
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)
207 (setup-logging)
208 (current-http-response-auto #f)
209 (current-http-user-agent "xandkar/tt 0.1.0")
210 (date-display-format 'rfc2822)
211
212 (define feeds (we-are-twtxt))
213 (define out-format 'multi-line)
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)))
216
217 (main)
This page took 0.084582 seconds and 5 git commands to generate.