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