Commit | Line | Data |
---|---|---|
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 | |
be96956c | 8 | ; TODO feed set operations (perhaps better done externally?) |
d6a187b7 SK |
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 | |
895a32cf SK |
13 | ; TODO parse "following" from feed |
14 | ; - following = <nick> <uri> | |
93ebe03e SK |
15 | ; TODO parse mentions: |
16 | ; - @<source.nick source.url> | @<source.url> | |
d6a187b7 SK |
17 | ; TODO highlight mentions |
18 | ; TODO filter on mentions | |
19 | ; TODO highlight hashtags | |
20 | ; TODO filter on hashtags | |
e0e0fb20 | 21 | ; TODO hashtags as channels? initial hashtag special? |
d6a187b7 | 22 | ; TODO query language |
e0e0fb20 SK |
23 | ; TODO console logger colors by level ('error) |
24 | ; TODO file logger ('debug) | |
dbccadf9 SK |
25 | ; TODO commands: |
26 | ; - r | read | |
27 | ; - see timeline ops above | |
28 | ; - w | write | |
93ebe03e | 29 | ; - arg or stdin |
dbccadf9 SK |
30 | ; - nick expand to URI |
31 | ; - q | query | |
32 | ; - see timeline ops above | |
33 | ; - see hashtag and channels above | |
93ebe03e SK |
34 | ; - d | download |
35 | ; - u | upload | |
36 | ; - calls user-configured command to upload user's own feed file to their server | |
84152e25 SK |
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 | |
d6a187b7 | 40 | |
4764ff89 SK |
41 | #lang racket |
42 | ||
43 | (require racket/date) | |
44 | ||
45 | (require http-client) | |
46 | (require rfc3339-old) | |
47 | ||
b201e854 | 48 | (struct msg (tm_epoch tm_rfc3339 nick uri text)) |
4764ff89 SK |
49 | (struct feed (nick uri)) |
50 | ||
895a32cf SK |
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 | ||
b201e854 SK |
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))) | |
e96264cc SK |
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}")) | |
88d50b3e | 102 | |
b201e854 | 103 | (define (str->msg nick uri str) |
88d50b3e SK |
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])]) | |
b201e854 | 124 | (msg tm_epoch tm_rfc3339 nick uri tok_text)))))) |
88d50b3e | 125 | |
e96264cc SK |
126 | (define (str->lines str) |
127 | (string-split str (regexp "[\r\n]+"))) | |
128 | ||
b201e854 SK |
129 | (define (str->msgs nick uri str) |
130 | (filter-map (λ (line) (str->msg nick uri line)) (str->lines str))) | |
4764ff89 SK |
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 | ||
b201e854 | 142 | (define (timeline-print out-format timeline) |
4764ff89 SK |
143 | (for ([msg timeline] |
144 | [i (in-naturals)]) | |
b201e854 | 145 | (msg-print out-format (odd? i) msg))) |
4764ff89 | 146 | |
9a6a9f9a SK |
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)]) | |
b201e854 SK |
166 | (define uri (feed-uri feed)) |
167 | (str->msgs [feed-nick feed] uri [uri-fetch uri]))) | |
9a6a9f9a SK |
168 | |
169 | ; TODO timeline contract : time-sorted list of messages | |
895a32cf SK |
170 | (define (timeline num_workers feeds) |
171 | (sort (append* (concurrent-filter-map num_workers feed->msgs feeds)) | |
9a6a9f9a | 172 | (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)]))) |
4764ff89 | 173 | |
be96956c SK |
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 | ||
4764ff89 | 187 | (define (we-are-twtxt) |
be96956c SK |
188 | (define uri |
189 | "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt") | |
190 | (str->feeds (uri-fetch uri))) | |
4764ff89 | 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) |
be96956c | 209 | (current-http-user-agent "xandkar/tt 0.2.0") |
4764ff89 | 210 | (date-display-format 'rfc2822) |
e96264cc | 211 | |
be96956c SK |
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)))) | |
b201e854 | 217 | (define out-format 'multi-line) |
895a32cf SK |
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))) | |
4764ff89 SK |
220 | |
221 | (main) |