1 ; TODO optional text wrap
3 ; TODO caching (use cache by default, unless explicitly asked for update)
4 ; - [x] value --> cache
5 ; - [x] value <-- cache
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
12 ; TODO parse "following" from feed
13 ; - following = <nick> <uri>
14 ; TODO parse mentions:
15 ; - @<source.nick source.url> | @<source.url>
16 ; TODO highlight mentions
17 ; TODO filter on mentions
18 ; TODO highlight hashtags
19 ; TODO filter on hashtags
20 ; TODO hashtags as channels? initial hashtag special?
22 ; TODO console logger colors by level ('error)
23 ; TODO file logger ('debug)
26 ; - see timeline ops above
29 ; - nick expand to URI
31 ; - see timeline ops above
32 ; - see hashtag and channels above
35 ; - calls user-configured command to upload user's own feed file to their server
37 ; Looks like a better CLI parser than "racket/cmdline":
38 ; https://docs.racket-lang.org/natural-cli/
42 (require openssl/sha1)
51 (struct msg (ts_epoch ts_rfc3339 nick uri text))
52 (struct feed (nick uri))
54 (define (concurrent-filter-map num_workers f xs)
55 ; TODO preserve order of elements OR communicate that reorder is expected
56 ; TODO switch from mailboxes to channels
57 (define (make-worker id f)
58 (define parent (current-thread))
60 (define self (current-thread))
62 (thread-send parent (cons 'next self))
63 (match (thread-receive)
64 ['done (thread-send parent (cons 'exit id))]
65 [(cons 'unit x) (begin
67 (when y (thread-send parent (cons 'result y)))
70 (define (dispatch ws xs ys)
73 (match (thread-receive)
74 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
75 [(cons 'result y) (dispatch ws xs (cons y ys))]
76 [(cons 'next thd) (match xs
78 (thread-send thd 'done)
81 (thread-send thd (cons 'unit x))
82 (dispatch ws xs ys))])])))
83 (define workers (range num_workers))
84 (define threads (map (λ (id) (thread (make-worker id f))) workers))
85 (define results (dispatch workers xs '()))
86 (for-each thread-wait threads)
90 (let* ([f (λ (x) (if (even? x) x #f))]
92 [actual (sort (concurrent-filter-map 10 f xs) <)]
93 [expected (sort ( filter-map f xs) <)])
94 (check-equal? actual expected "concurrent-filter-map")))
97 (let* ([colors (vector 36 33)]
98 [n (vector-length colors)])
99 (λ (out-format color-i msg)
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))])
105 (date->string (seconds->date [msg-ts_epoch msg]) #t)
108 (vector-ref colors (modulo color-i n))
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}"))
115 (define (str->msg nick uri str)
116 (if (not (regexp-match? re-msg-begin str))
118 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
120 (let ([toks (string-split str (regexp "\t+"))])
121 (if (not (= 2 (length toks)))
123 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
126 ([ts_rfc3339 (first toks)]
128 [t (string->rfc3339-record ts_rfc3339)]
129 ; TODO handle tz offset
130 [ts_epoch (find-seconds [rfc3339-record:second t]
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])])
136 (msg ts_epoch ts_rfc3339 nick uri text))))))
139 (let* ([ts "2020-11-18T22:22:09-0500"]
144 [actual (str->msg nick uri (string-append ts tab text))]
145 [expected (msg 1605756129 ts nick uri text)])
146 ; FIXME re-enable after handling tz offset
148 ; (msg-ts_epoch actual)
149 ; (msg-ts_epoch expected)
150 ; "str->msg ts_epoch")
152 (msg-ts_rfc3339 actual)
153 (msg-ts_rfc3339 expected)
154 "str->msg ts_rfc3339")
168 (define (str->lines str)
169 (string-split str (regexp "[\r\n]+")))
172 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
174 (define (str->msgs nick uri str)
175 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
177 (define (hash-sha1 str)
178 (define in (open-input-string str))
179 (define digest (sha1 in))
180 (close-input-port in)
183 (define (uri-fetch use-cache uri)
184 (define cache-file-path
185 (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
186 (if (and use-cache (file-exists? cache-file-path))
188 (log-info "uri-fetch cached ~a" uri)
189 (file->string cache-file-path))
191 (log-info "uri-fetch new ~a" uri)
192 (let* ([resp (http-get uri)]
193 [status (http-response-code resp)]
194 [body (http-response-body resp)])
195 (log-debug "finished GET ~a status:~a body length:~a"
196 uri status (string-length body))
197 ; TODO Handle redirects
200 (display-to-file body cache-file-path #:exists 'replace)
202 ; TODO A more-informative exception
205 (define (timeline-print out-format timeline)
206 (void (foldl (match-lambda**
207 [((and m (msg _ _ nick _ _)) (cons prev-nick i))
208 (let ([i (if (string=? prev-nick nick) i (+ 1 i))])
209 (msg-print out-format i m)
214 (define (feed->msgs use-cache feed)
215 (log-info "downloading feed nick:~a uri:~a"
221 (log-error "network error nick:~a uri:~a exn:~a"
228 (log-error "http error nick:~a uri:~a status:~a"
233 (define uri (feed-uri feed))
234 (str->msgs [feed-nick feed] uri [uri-fetch use-cache uri])))
236 ; TODO timeline contract : time-sorted list of messages
237 (define (timeline use-cache num_workers feeds)
238 (sort (append* (concurrent-filter-map num_workers (curry feed->msgs use-cache) feeds))
239 (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
241 (define (str->feed str)
243 (define toks (string-split str))
246 (define (str->feeds str)
247 (map str->feed (str->lines str)))
249 (define (file->feeds filename)
250 (str->feeds (file->string filename)))
252 (define (user-agent prog-name prog-version)
254 ([prog-uri "https://github.com/xandkar/tt"]
255 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
257 (if (file-exists? user-feed-file)
258 (let ([user (first (file->feeds user-feed-file))])
259 (format "+~a; @~a" (feed-uri user) (feed-nick user)))
260 (format "+~a" prog-uri))]
262 (format "~a/~a (~a)" prog-name prog-version user)))
265 (require setup/getinfo)
268 [logger (make-logger #f #f level #f)]
269 [log-receiver (make-log-receiver logger level)])
271 [date-display-format 'iso-8601]
273 (define data (sync log-receiver))
274 (define level (vector-ref data 0))
275 (define msg (vector-ref data 1))
276 (define ts (date->string (current-date) #t))
277 (eprintf "~a [~a] ~a~n" ts level msg)
279 (current-logger logger))
280 (current-http-response-auto #f)
281 (let* ([prog-name "tt"]
282 [prog-version ((get-info (list prog-name)) 'version)]
283 [user-agent (user-agent prog-name prog-version)])
284 (current-http-user-agent user-agent))
285 (date-display-format 'rfc2822)
291 15]) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
295 "Read cached data instead of downloading."
299 njobs "Number of concurrent jobs."
300 (set! num_workers (string->number njobs))]
304 (timeline-print out-format
307 (file->feeds filename))))))