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