4 ; TODO optional text wrap
6 ; TODO caching (use cache by default, unless explicitly asked for update)
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
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?
23 ; TODO console logger colors by level ('error)
24 ; TODO file logger ('debug)
27 ; - see timeline ops above
30 ; - nick expand to URI
32 ; - see timeline ops above
33 ; - see hashtag and channels above
36 ; - calls user-configured command to upload user's own feed file to their server
45 (struct msg (tm_epoch tm_rfc3339 nick uri text))
46 (struct feed (nick uri))
48 (define (concurrent-filter-map num_workers f xs)
49 (define (make-worker id f)
50 (define parent (current-thread))
52 (define self (current-thread))
54 (thread-send parent (cons 'next self))
55 (match (thread-receive)
56 ['done (thread-send parent (cons 'exit id))]
57 [(cons 'unit x) (begin
59 (when y (thread-send parent (cons 'result y)))
62 (define (dispatch ws xs ys)
65 (match (thread-receive)
66 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
67 [(cons 'result y) (dispatch ws xs (cons y ys))]
68 [(cons 'next thd) (match xs
70 (thread-send thd 'done)
73 (thread-send thd (cons 'unit x))
74 (dispatch ws xs ys))])])))
76 (range 1 (add1 num_workers)))
78 (map (λ (id) (thread (make-worker id f))) workers))
80 (dispatch workers xs '()))
81 (for-each thread-wait threads)
84 (define (msg-print out-format odd msg)
87 ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
88 ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
89 [_ (raise (format "Invalid output format: ~a" out-format))])
90 (date->string (seconds->date [msg-tm_epoch msg]) #t)
97 ; TODO Zulu offset. Maybe in several formats. Which ones?
98 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
100 (define (str->msg nick uri str)
101 (if (not (regexp-match? re-msg-begin str))
103 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
105 (let ([toks (string-split str (regexp "\t+"))])
106 (if (not (= 2 (length toks)))
108 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
111 ([tm_rfc3339 (list-ref toks 0)]
112 [tok_text (list-ref toks 1)]
113 [t (string->rfc3339-record tm_rfc3339)]
114 ; TODO handle tz offset
115 [tm_epoch (find-seconds [rfc3339-record:second t]
116 [rfc3339-record:minute t]
117 [rfc3339-record:hour t]
118 [rfc3339-record:mday t]
119 [rfc3339-record:month t]
120 [rfc3339-record:year t])])
121 (msg tm_epoch tm_rfc3339 nick uri tok_text))))))
123 (define (str->lines str)
124 (string-split str (regexp "[\r\n]+")))
126 (define (str->msgs nick uri str)
127 (filter-map (λ (line) (str->msg nick uri line)) (str->lines str)))
129 (define (uri-fetch uri)
130 (log-info "GET ~a" uri)
131 (define resp (http-get uri))
132 (define status (http-response-code resp))
133 (define body (http-response-body resp))
134 (log-debug "finished GET ~a status:~a body length:~a"
135 uri status (string-length body))
136 ; TODO Handle redirects
137 (if (= status 200) body (raise status)))
139 (define (timeline-print out-format timeline)
142 (msg-print out-format (odd? i) msg)))
144 (define (feed->msgs feed)
145 (log-info "downloading feed nick:~a uri:~a"
151 (log-error "network error nick:~a uri:~a exn:~a"
158 (log-error "http error nick:~a uri:~a status:~a"
163 (define uri (feed-uri feed))
164 (str->msgs [feed-nick feed] uri [uri-fetch uri])))
166 ; TODO timeline contract : time-sorted list of messages
167 (define (timeline num_workers feeds)
168 (sort (append* (concurrent-filter-map num_workers feed->msgs feeds))
169 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
171 (define (str->feed str)
173 (define toks (string-split str))
178 (define (str->feeds str)
179 (map str->feed (str->lines str)))
181 (define (file->feeds filename)
182 (str->feeds (file->string filename)))
184 (define (we-are-twtxt)
186 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt")
187 (str->feeds (uri-fetch uri)))
189 (define (setup-logging)
190 (define logger (make-logger #f #f 'debug #f))
191 (define log-chan (make-log-receiver logger 'debug))
193 [date-display-format 'iso-8601]
195 (define data (sync log-chan))
196 (define level (vector-ref data 0))
197 (define msg (vector-ref data 1))
198 (define ts (date->string (current-date) #t))
199 (eprintf "~a [~a] ~a~n" ts level msg)
201 (current-logger logger))
207 [prog-version "0.3.0"]
208 [user-feed-file (expand-user-path "~/twtxt-me.txt")]
209 [user (list-ref (file->feeds user-feed-file) 0)])
210 (format "~a/~a (+~a; @~a)"
217 (current-http-response-auto #f)
218 (current-http-user-agent user-agent)
219 (date-display-format 'rfc2822)
221 (define args (current-command-line-arguments))
223 (if (vector-empty? args)
225 (file->feeds (vector-ref args 0))))
226 (define out-format 'multi-line)
227 (define num_workers 15) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
228 (timeline-print out-format (timeline num_workers feeds)))