| 1 | #lang racket |
| 2 | |
| 3 | (require openssl/sha1) |
| 4 | (require racket/date) |
| 5 | |
| 6 | (require http-client) |
| 7 | (require rfc3339-old) |
| 8 | |
| 9 | (module+ test |
| 10 | (require rackunit)) |
| 11 | |
| 12 | (struct msg (ts_epoch ts_rfc3339 nick uri text)) |
| 13 | (struct feed (nick uri)) |
| 14 | |
| 15 | (define (concurrent-filter-map num_workers f xs) |
| 16 | ; TODO preserve order of elements OR communicate that reorder is expected |
| 17 | ; TODO switch from mailboxes to channels |
| 18 | (define (make-worker id f) |
| 19 | (define parent (current-thread)) |
| 20 | (λ () |
| 21 | (define self (current-thread)) |
| 22 | (define (work) |
| 23 | (thread-send parent (cons 'next self)) |
| 24 | (match (thread-receive) |
| 25 | ['done (thread-send parent (cons 'exit id))] |
| 26 | [(cons 'unit x) (begin |
| 27 | (define y (f x)) |
| 28 | (when y (thread-send parent (cons 'result y))) |
| 29 | (work))])) |
| 30 | (work))) |
| 31 | (define (dispatch ws xs ys) |
| 32 | (if (empty? ws) |
| 33 | ys |
| 34 | (match (thread-receive) |
| 35 | [(cons 'exit w) (dispatch (remove w ws =) xs ys)] |
| 36 | [(cons 'result y) (dispatch ws xs (cons y ys))] |
| 37 | [(cons 'next thd) (match xs |
| 38 | ['() (begin |
| 39 | (thread-send thd 'done) |
| 40 | (dispatch ws xs ys))] |
| 41 | [(cons x xs) (begin |
| 42 | (thread-send thd (cons 'unit x)) |
| 43 | (dispatch ws xs ys))])]))) |
| 44 | (define workers (range num_workers)) |
| 45 | (define threads (map (λ (id) (thread (make-worker id f))) workers)) |
| 46 | (define results (dispatch workers xs '())) |
| 47 | (for-each thread-wait threads) |
| 48 | results) |
| 49 | |
| 50 | (module+ test |
| 51 | (let* ([f (λ (x) (if (even? x) x #f))] |
| 52 | [xs (range 11)] |
| 53 | [actual (sort (concurrent-filter-map 10 f xs) <)] |
| 54 | [expected (sort ( filter-map f xs) <)]) |
| 55 | (check-equal? actual expected "concurrent-filter-map"))) |
| 56 | |
| 57 | (define msg-print |
| 58 | (let* ([colors (vector 36 33)] |
| 59 | [n (vector-length colors)]) |
| 60 | (λ (out-format color-i msg) |
| 61 | (let ([color (vector-ref colors (modulo color-i n))] |
| 62 | [nick (msg-nick msg)] |
| 63 | [uri (msg-uri msg)] |
| 64 | [text (msg-text msg)]) |
| 65 | (match out-format |
| 66 | ['single-line |
| 67 | (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n" |
| 68 | (parameterize ([date-display-format 'iso-8601]) |
| 69 | (date->string (seconds->date [msg-ts_epoch msg]) #t)) |
| 70 | nick color text)] |
| 71 | ['multi-line |
| 72 | (printf "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n" |
| 73 | (parameterize ([date-display-format 'rfc2822]) |
| 74 | (date->string (seconds->date [msg-ts_epoch msg]) #t)) |
| 75 | nick uri color text)]))))) |
| 76 | |
| 77 | (define re-msg-begin |
| 78 | ; TODO Zulu offset. Maybe in several formats. Which ones? |
| 79 | (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}")) |
| 80 | |
| 81 | (define (str->msg nick uri str) |
| 82 | (if (not (regexp-match? re-msg-begin str)) |
| 83 | (begin |
| 84 | (log-debug "Non-msg line from nick:~a, line:~a" nick str) |
| 85 | #f) |
| 86 | (let ([toks (string-split str (regexp "\t+"))]) |
| 87 | (if (not (= 2 (length toks))) |
| 88 | (begin |
| 89 | (log-warning "Invalid msg line from nick:~a, msg:~a" nick str) |
| 90 | #f) |
| 91 | (let* |
| 92 | ([ts_rfc3339 (first toks)] |
| 93 | [text (second toks)] |
| 94 | [t (string->rfc3339-record ts_rfc3339)] |
| 95 | ; TODO handle tz offset |
| 96 | [ts_epoch (find-seconds [rfc3339-record:second t] |
| 97 | [rfc3339-record:minute t] |
| 98 | [rfc3339-record:hour t] |
| 99 | [rfc3339-record:mday t] |
| 100 | [rfc3339-record:month t] |
| 101 | [rfc3339-record:year t])]) |
| 102 | (msg ts_epoch ts_rfc3339 nick uri text)))))) |
| 103 | |
| 104 | (module+ test |
| 105 | (let* ([ts "2020-11-18T22:22:09-0500"] |
| 106 | [tab " "] |
| 107 | [text "Lorem ipsum"] |
| 108 | [nick "foo"] |
| 109 | [uri "bar"] |
| 110 | [actual (str->msg nick uri (string-append ts tab text))] |
| 111 | [expected (msg 1605756129 ts nick uri text)]) |
| 112 | ; FIXME re-enable after handling tz offset |
| 113 | ;(check-equal? |
| 114 | ; (msg-ts_epoch actual) |
| 115 | ; (msg-ts_epoch expected) |
| 116 | ; "str->msg ts_epoch") |
| 117 | (check-equal? |
| 118 | (msg-ts_rfc3339 actual) |
| 119 | (msg-ts_rfc3339 expected) |
| 120 | "str->msg ts_rfc3339") |
| 121 | (check-equal? |
| 122 | (msg-nick actual) |
| 123 | (msg-nick expected) |
| 124 | "str->msg nick") |
| 125 | (check-equal? |
| 126 | (msg-uri actual) |
| 127 | (msg-uri expected) |
| 128 | "str->msg uri") |
| 129 | (check-equal? |
| 130 | (msg-text actual) |
| 131 | (msg-text expected) |
| 132 | "str->msg text"))) |
| 133 | |
| 134 | (define (str->lines str) |
| 135 | (string-split str (regexp "[\r\n]+"))) |
| 136 | |
| 137 | (module+ test |
| 138 | (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi"))) |
| 139 | |
| 140 | (define (str->msgs nick uri str) |
| 141 | (filter-map (λ (line) (str->msg nick uri line)) (str->lines str))) |
| 142 | |
| 143 | (define (hash-sha1 str) |
| 144 | (define in (open-input-string str)) |
| 145 | (define digest (sha1 in)) |
| 146 | (close-input-port in) |
| 147 | digest) |
| 148 | |
| 149 | (define (uri-fetch use-cache uri) |
| 150 | (define cache-file-path |
| 151 | (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri)))) |
| 152 | (if (and use-cache (file-exists? cache-file-path)) |
| 153 | (begin |
| 154 | (log-info "uri-fetch cached ~a" uri) |
| 155 | (file->string cache-file-path)) |
| 156 | (begin |
| 157 | (log-info "uri-fetch new ~a" uri) |
| 158 | ; TODO Timeout. Currently hangs on slow connections. |
| 159 | (let* ([resp (http-get uri)] |
| 160 | [status (http-response-code resp)] |
| 161 | [body (http-response-body resp)]) |
| 162 | (log-debug "finished GET ~a status:~a body length:~a" |
| 163 | uri status (string-length body)) |
| 164 | ; TODO Handle redirects |
| 165 | (if (= status 200) |
| 166 | (begin |
| 167 | (display-to-file body cache-file-path #:exists 'replace) |
| 168 | body) |
| 169 | ; TODO A more-informative exception |
| 170 | (raise status)))))) |
| 171 | |
| 172 | (define (timeline-print out-format timeline) |
| 173 | (void (foldl (match-lambda** |
| 174 | [((and m (msg _ _ nick _ _)) (cons prev-nick i)) |
| 175 | (let ([i (if (string=? prev-nick nick) i (+ 1 i))]) |
| 176 | (msg-print out-format i m) |
| 177 | (cons nick i))]) |
| 178 | (cons "" 0) |
| 179 | timeline))) |
| 180 | |
| 181 | (define (feed->msgs use-cache feed) |
| 182 | (log-info "downloading feed nick:~a uri:~a" |
| 183 | (feed-nick feed) |
| 184 | (feed-uri feed)) |
| 185 | (with-handlers |
| 186 | ([exn:fail:network? |
| 187 | (λ (e) |
| 188 | (log-error "network error nick:~a uri:~a exn:~a" |
| 189 | (feed-nick feed) |
| 190 | (feed-uri feed) |
| 191 | e) |
| 192 | #f)] |
| 193 | [integer? |
| 194 | (λ (status) |
| 195 | (log-error "http error nick:~a uri:~a status:~a" |
| 196 | (feed-nick feed) |
| 197 | (feed-uri feed) |
| 198 | status) |
| 199 | #f)]) |
| 200 | (define uri (feed-uri feed)) |
| 201 | (str->msgs [feed-nick feed] uri [uri-fetch use-cache uri]))) |
| 202 | |
| 203 | ; TODO timeline contract : time-sorted list of messages |
| 204 | (define (timeline use-cache num_workers feeds) |
| 205 | (sort (append* (concurrent-filter-map num_workers (curry feed->msgs use-cache) feeds)) |
| 206 | (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)]))) |
| 207 | |
| 208 | (define (str->feed str) |
| 209 | ; TODO validation |
| 210 | (define toks (string-split str)) |
| 211 | (apply feed toks)) |
| 212 | |
| 213 | (define (str->feeds str) |
| 214 | (map str->feed (str->lines str))) |
| 215 | |
| 216 | (define (file->feeds filename) |
| 217 | (str->feeds (file->string filename))) |
| 218 | |
| 219 | (define (user-agent prog-name prog-version) |
| 220 | (let* |
| 221 | ([prog-uri "https://github.com/xandkar/tt"] |
| 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 | (define (start-logger level) |
| 232 | (let* ([logger (make-logger #f #f level #f)] |
| 233 | [log-receiver (make-log-receiver logger level)]) |
| 234 | (void (thread (λ () |
| 235 | (parameterize |
| 236 | ([date-display-format 'iso-8601]) |
| 237 | (let loop () |
| 238 | (define data (sync log-receiver)) |
| 239 | (define level (vector-ref data 0)) |
| 240 | (define msg (vector-ref data 1)) |
| 241 | (define ts (date->string (current-date) #t)) |
| 242 | (eprintf "~a [~a] ~a~n" ts level msg) |
| 243 | (loop)))))) |
| 244 | (current-logger logger))) |
| 245 | |
| 246 | (module+ main |
| 247 | (require setup/getinfo) |
| 248 | |
| 249 | (current-http-response-auto #f) |
| 250 | (let* ([prog-name "tt"] |
| 251 | [prog-version ((get-info (list prog-name)) 'version)] |
| 252 | [user-agent (user-agent prog-name prog-version)]) |
| 253 | (current-http-user-agent user-agent)) |
| 254 | (let* ([use-cache |
| 255 | #f] |
| 256 | [log-level |
| 257 | 'info] |
| 258 | [out-format |
| 259 | 'multi-line] |
| 260 | [num_workers |
| 261 | 15]) ; 15 was fastest out of the tried 1, 5, 10, 15 and 20. |
| 262 | (command-line |
| 263 | #:once-each |
| 264 | [("-c" "--cached") |
| 265 | "Read cached data instead of downloading." |
| 266 | (set! use-cache #t)] |
| 267 | |
| 268 | [("-d" "--debug") |
| 269 | "Enable debug log level." |
| 270 | (set! log-level 'debug)] |
| 271 | |
| 272 | [("-j" "--jobs") |
| 273 | njobs "Number of concurrent jobs." |
| 274 | (set! num_workers (string->number njobs))] |
| 275 | |
| 276 | #:once-any |
| 277 | [("-s" "--short") |
| 278 | "Short output format" |
| 279 | (set! out-format 'single-line)] |
| 280 | |
| 281 | [("-l" "--long") |
| 282 | "Long output format" |
| 283 | (set! out-format 'multi-line)] |
| 284 | |
| 285 | #:args (filename) |
| 286 | (start-logger log-level) |
| 287 | (timeline-print out-format |
| 288 | (timeline use-cache |
| 289 | num_workers |
| 290 | (file->feeds filename)))))) |