4 ; TODO optional text wrap
6 ; TODO caching (use cache by default, unless explicitly asked for update)
8 ; TODO user-defined feed sets (a la twitter lists)
9 ; TODO feed set operations
10 ; TODO timeline as a result of a query (feed set op + filter expressions)
11 ; TODO named timelines
14 ; TODO parse "following" from feed
15 ; - following = <nick> <uri>
16 ; TODO parse mentions:
17 ; - @<source.nick source.url> | @<source.url>
18 ; TODO highlight mentions
19 ; TODO filter on mentions
20 ; TODO highlight hashtags
21 ; TODO filter on hashtags
22 ; TODO hashtags as channels? initial hashtag special?
24 ; TODO console logger colors by level ('error)
25 ; TODO file logger ('debug)
28 ; - see timeline ops above
31 ; - nick expand to URI
33 ; - see timeline ops above
34 ; - see hashtag and channels above
37 ; - calls user-configured command to upload user's own feed file to their server
38 ; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
39 ; - requires configurability
40 ; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
49 (struct msg (tm_epoch tm_rfc3339 nick uri text))
50 (struct feed (nick uri))
52 (define (concurrent-filter-map num_workers f xs)
53 (define (make-worker id f)
54 (define parent (current-thread))
56 (define self (current-thread))
58 (thread-send parent (cons 'next self))
59 (match (thread-receive)
60 ['done (thread-send parent (cons 'exit id))]
61 [(cons 'unit x) (begin
63 (when y (thread-send parent (cons 'result y)))
66 (define (dispatch ws xs ys)
69 (match (thread-receive)
70 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
71 [(cons 'result y) (dispatch ws xs (cons y ys))]
72 [(cons 'next thd) (match xs
74 (thread-send thd 'done)
77 (thread-send thd (cons 'unit x))
78 (dispatch ws xs ys))])])))
80 (range 1 (add1 num_workers)))
82 (map (λ (id) (thread (make-worker id f))) workers))
84 (dispatch workers xs '()))
85 (for-each thread-wait threads)
88 (define (msg-print out-format odd msg)
91 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
92 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
93 [_ (raise (format "Invalid output format: ~a" out-format))])
94 (date->string (seconds->date [msg-tm_epoch msg]) #t)
101 ; TODO Zulu offset. Maybe in several formats. Which ones?
102 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
104 (define (str->msg nick uri str)
105 (if (not (regexp-match? re-msg-begin str))
107 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
109 (let ([toks (string-split str (regexp "\t+"))])
110 (if (not (= 2 (length toks)))
112 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
115 ([tm_rfc3339 (list-ref toks 0)]
116 [tok_text (list-ref toks 1)]
117 [t (string->rfc3339-record tm_rfc3339)]
118 ; TODO handle tz offset
119 [tm_epoch (find-seconds [rfc3339-record:second t]
120 [rfc3339-record:minute t]
121 [rfc3339-record:hour t]
122 [rfc3339-record:mday t]
123 [rfc3339-record:month t]
124 [rfc3339-record:year t])])
125 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
127 (define (str->lines str)
128 (string-split str (regexp "[\r\n]+")))
130 (define (str->msgs nick uri str)
131 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
133 (define (uri-fetch uri)
134 (log-info "GET ~a" uri)
135 (define resp (http-get uri))
136 (define status (http-response-code resp))
137 (define body (http-response-body resp))
138 (log-debug "finished GET ~a status:~a body length:~a"
139 uri status (string-length body))
140 ; TODO Handle redirects
141 (if (= status 200) body (raise status)))
143 (define (timeline-print out-format timeline)
146 (msg-print out-format (odd? i) msg)))
148 (define (feed->msgs feed)
149 (log-info "downloading feed nick:~a uri:~a"
155 (log-error "network error nick:~a uri:~a exn:~a"
162 (log-error "http error nick:~a uri:~a status:~a"
167 (define uri (feed-uri feed))
168 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
170 ; TODO timeline contract : time-sorted list of messages
171 (define (timeline num_workers feeds)
172 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
173 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
175 (define (we-are-twtxt)
177 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
181 (str->lines payload)]
185 (define toks (string-split line))
192 (define (setup-logging)
193 (define logger (make-logger #f #f 'debug #f))
194 (define log-chan (make-log-receiver logger 'debug))
196 [date-display-format 'iso-8601]
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)
204 (current-logger logger))
208 (current-http-response-auto #f)
209 (current-http-user-agent "xandkar/tt 0.1.0")
210 (date-display-format 'rfc2822)
212 (define feeds (we-are-twtxt))
213 (define out-format 'multi-line)
214 (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
215 (timeline-print out-format (timeline num_workers feeds)))