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