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