Commit | Line | Data |
---|---|---|
e5c3ae92 | 1 | ; TODO optional text wrap |
d6a187b7 SK |
2 | ; TODO write |
3 | ; TODO caching (use cache by default, unless explicitly asked for update) | |
1d753430 SK |
4 | ; - [x] value --> cache |
5 | ; - [ ] value <-- cache | |
6 | ; requires: commands | |
d6a187b7 | 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 | |
812f0b04 SK |
37 | ; |
38 | ; Looks like a better CLI parser than "racket/cmdline": | |
39 | ; https://docs.racket-lang.org/natural-cli/ | |
d6a187b7 | 40 | |
4764ff89 SK |
41 | #lang racket |
42 | ||
1d753430 | 43 | (require openssl/sha1) |
4764ff89 SK |
44 | (require racket/date) |
45 | ||
46 | (require http-client) | |
47 | (require rfc3339-old) | |
48 | ||
78398948 SK |
49 | (module+ test |
50 | (require rackunit)) | |
51 | ||
87d0eb92 | 52 | (struct msg (ts_epoch ts_rfc3339 nick uri text)) |
4764ff89 SK |
53 | (struct feed (nick uri)) |
54 | ||
895a32cf | 55 | (define (concurrent-filter-map num_workers f xs) |
dad4504d | 56 | ; TODO preserve order of elements OR communicate that reorder is expected |
a239a233 | 57 | ; TODO switch from mailboxes to channels |
895a32cf SK |
58 | (define (make-worker id f) |
59 | (define parent (current-thread)) | |
60 | (λ () | |
61 | (define self (current-thread)) | |
62 | (define (work) | |
63 | (thread-send parent (cons 'next self)) | |
64 | (match (thread-receive) | |
65 | ['done (thread-send parent (cons 'exit id))] | |
66 | [(cons 'unit x) (begin | |
67 | (define y (f x)) | |
68 | (when y (thread-send parent (cons 'result y))) | |
69 | (work))])) | |
70 | (work))) | |
71 | (define (dispatch ws xs ys) | |
72 | (if (empty? ws) | |
73 | ys | |
74 | (match (thread-receive) | |
75 | [(cons 'exit w) (dispatch (remove w ws =) xs ys)] | |
76 | [(cons 'result y) (dispatch ws xs (cons y ys))] | |
77 | [(cons 'next thd) (match xs | |
78 | ['() (begin | |
79 | (thread-send thd 'done) | |
80 | (dispatch ws xs ys))] | |
81 | [(cons x xs) (begin | |
82 | (thread-send thd (cons 'unit x)) | |
83 | (dispatch ws xs ys))])]))) | |
9926c9a9 SK |
84 | (define workers (range num_workers)) |
85 | (define threads (map (λ (id) (thread (make-worker id f))) workers)) | |
86 | (define results (dispatch workers xs '())) | |
895a32cf SK |
87 | (for-each thread-wait threads) |
88 | results) | |
89 | ||
dad4504d SK |
90 | (module+ test |
91 | (define n-workers 10) | |
92 | (define given (list | |
93 | (λ (x) (if (even? x) x #f)) | |
94 | (range 11))) | |
95 | (check-equal? | |
96 | (sort (apply concurrent-filter-map (cons n-workers given)) <) | |
97 | (sort (apply filter-map given ) <))) | |
98 | ||
b201e854 SK |
99 | (define (msg-print out-format odd msg) |
100 | (printf | |
101 | (match out-format | |
102 | ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"] | |
103 | ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"] | |
104 | [_ (raise (format "Invalid output format: ~a" out-format))]) | |
87d0eb92 | 105 | (date->string (seconds->date [msg-ts_epoch msg]) #t) |
b201e854 SK |
106 | (msg-nick msg) |
107 | (msg-uri msg) | |
108 | (if odd 36 33) | |
109 | (msg-text msg))) | |
e96264cc SK |
110 | |
111 | (define re-msg-begin | |
112 | ; TODO Zulu offset. Maybe in several formats. Which ones? | |
113 | (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}")) | |
88d50b3e | 114 | |
b201e854 | 115 | (define (str->msg nick uri str) |
88d50b3e SK |
116 | (if (not (regexp-match? re-msg-begin str)) |
117 | (begin | |
118 | (log-debug "Non-msg line from nick:~a, line:~a" nick str) | |
119 | #f) | |
120 | (let ([toks (string-split str (regexp "\t+"))]) | |
121 | (if (not (= 2 (length toks))) | |
122 | (begin | |
123 | (log-warning "Invalid msg line from nick:~a, msg:~a" nick str) | |
124 | #f) | |
125 | (let* | |
87d0eb92 SK |
126 | ([ts_rfc3339 (first toks)] |
127 | [text (second toks)] | |
128 | [t (string->rfc3339-record ts_rfc3339)] | |
88d50b3e | 129 | ; TODO handle tz offset |
87d0eb92 | 130 | [ts_epoch (find-seconds [rfc3339-record:second t] |
88d50b3e SK |
131 | [rfc3339-record:minute t] |
132 | [rfc3339-record:hour t] | |
133 | [rfc3339-record:mday t] | |
134 | [rfc3339-record:month t] | |
135 | [rfc3339-record:year t])]) | |
87d0eb92 | 136 | (msg ts_epoch ts_rfc3339 nick uri text)))))) |
88d50b3e | 137 | |
e96264cc SK |
138 | (define (str->lines str) |
139 | (string-split str (regexp "[\r\n]+"))) | |
140 | ||
b201e854 SK |
141 | (define (str->msgs nick uri str) |
142 | (filter-map (λ (line) (str->msg nick uri line)) (str->lines str))) | |
4764ff89 | 143 | |
1d753430 SK |
144 | (define (hash-sha1 str) |
145 | (define in (open-input-string str)) | |
146 | (define digest (sha1 in)) | |
147 | (close-input-port in) | |
148 | digest) | |
149 | ||
4764ff89 SK |
150 | (define (uri-fetch uri) |
151 | (log-info "GET ~a" uri) | |
152 | (define resp (http-get uri)) | |
153 | (define status (http-response-code resp)) | |
154 | (define body (http-response-body resp)) | |
155 | (log-debug "finished GET ~a status:~a body length:~a" | |
156 | uri status (string-length body)) | |
157 | ; TODO Handle redirects | |
1d753430 SK |
158 | (if (= status 200) |
159 | (let* | |
160 | ([url-digest | |
161 | (hash-sha1 uri)] | |
162 | [cache-file-path | |
163 | (expand-user-path (string-append "~/.tt/cache/" url-digest))]) | |
164 | (display-to-file | |
165 | body cache-file-path | |
166 | #:exists 'replace) | |
167 | body) | |
168 | ; TODO A more-informative exception | |
169 | (raise status))) | |
4764ff89 | 170 | |
b201e854 | 171 | (define (timeline-print out-format timeline) |
4764ff89 SK |
172 | (for ([msg timeline] |
173 | [i (in-naturals)]) | |
b201e854 | 174 | (msg-print out-format (odd? i) msg))) |
4764ff89 | 175 | |
9a6a9f9a SK |
176 | (define (feed->msgs feed) |
177 | (log-info "downloading feed nick:~a uri:~a" | |
178 | (feed-nick feed) | |
179 | (feed-uri feed)) | |
180 | (with-handlers | |
181 | ([exn:fail:network? | |
182 | (λ (e) | |
183 | (log-error "network error nick:~a uri:~a exn:~a" | |
184 | (feed-nick feed) | |
185 | (feed-uri feed) | |
186 | e) | |
187 | #f)] | |
188 | [integer? | |
189 | (λ (status) | |
190 | (log-error "http error nick:~a uri:~a status:~a" | |
191 | (feed-nick feed) | |
192 | (feed-uri feed) | |
193 | status) | |
194 | #f)]) | |
b201e854 SK |
195 | (define uri (feed-uri feed)) |
196 | (str->msgs [feed-nick feed] uri [uri-fetch uri]))) | |
9a6a9f9a SK |
197 | |
198 | ; TODO timeline contract : time-sorted list of messages | |
895a32cf SK |
199 | (define (timeline num_workers feeds) |
200 | (sort (append* (concurrent-filter-map num_workers feed->msgs feeds)) | |
87d0eb92 | 201 | (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)]))) |
4764ff89 | 202 | |
be96956c SK |
203 | (define (str->feed str) |
204 | ; TODO validation | |
205 | (define toks (string-split str)) | |
87d0eb92 | 206 | (apply feed toks)) |
be96956c SK |
207 | |
208 | (define (str->feeds str) | |
209 | (map str->feed (str->lines str))) | |
210 | ||
211 | (define (file->feeds filename) | |
212 | (str->feeds (file->string filename))) | |
213 | ||
4764ff89 | 214 | (define (we-are-twtxt) |
be96956c SK |
215 | (define uri |
216 | "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt") | |
217 | (str->feeds (uri-fetch uri))) | |
4764ff89 | 218 | |
4f3dbb24 | 219 | (define (user-agent prog-name prog-version) |
24c6a76b | 220 | (let* |
4f3dbb24 | 221 | ([prog-uri "https://github.com/xandkar/tt"] |
24c6a76b SK |
222 | [user-feed-file (expand-user-path "~/twtxt-me.txt")] |
223 | [user | |
224 | (if (file-exists? user-feed-file) | |
225 | (let ([user (first (file->feeds user-feed-file))]) | |
226 | (format "+~a; @~a" (feed-uri user) (feed-nick user))) | |
227 | (format "+~a" prog-uri))] | |
228 | ) | |
229 | (format "~a/~a (~a)" prog-name prog-version user))) | |
230 | ||
231 | (module+ main | |
4f3dbb24 | 232 | (require setup/getinfo) |
24c6a76b SK |
233 | (define (setup-logging) |
234 | (define logger (make-logger #f #f 'debug #f)) | |
235 | (define log-chan (make-log-receiver logger 'debug)) | |
236 | (void (thread (λ () | |
237 | [date-display-format 'iso-8601] | |
238 | [let loop () | |
239 | (define data (sync log-chan)) | |
240 | (define level (vector-ref data 0)) | |
241 | (define msg (vector-ref data 1)) | |
242 | (define ts (date->string (current-date) #t)) | |
243 | (eprintf "~a [~a] ~a~n" ts level msg) | |
244 | (loop)]))) | |
245 | (current-logger logger)) | |
8fae9613 | 246 | |
e96264cc | 247 | (setup-logging) |
4764ff89 | 248 | (current-http-response-auto #f) |
4f3dbb24 SK |
249 | (let* ([prog-name "tt"] |
250 | [prog-version ((get-info (list prog-name)) 'version)] | |
251 | [user-agent (user-agent prog-name prog-version)]) | |
252 | (current-http-user-agent user-agent)) | |
4764ff89 | 253 | (date-display-format 'rfc2822) |
e96264cc | 254 | |
be96956c SK |
255 | (define args (current-command-line-arguments)) |
256 | (define feeds | |
5aaa408b | 257 | (if (= 0 (vector-length args)) |
be96956c SK |
258 | (we-are-twtxt) |
259 | (file->feeds (vector-ref args 0)))) | |
b201e854 | 260 | (define out-format 'multi-line) |
895a32cf SK |
261 | (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20. |
262 | (timeline-print out-format (timeline num_workers feeds))) |