11 (struct msg (tm_epoch tm_rfc3339 nick text))
12 (struct feed (nick uri))
14 (define (msg<-toks nick toks)
15 (define tm_rfc3339 (list-ref toks 0))
16 (define tok_text (list-ref toks 1))
17 (define t (string->rfc3339-record tm_rfc3339))
18 ; TODO handle tz offset
19 (define tm_epoch (find-seconds [rfc3339-record:second t]
20 [rfc3339-record:minute t]
21 [rfc3339-record:hour t]
22 [rfc3339-record:mday t]
23 [rfc3339-record:month t]
24 [rfc3339-record:year t]))
25 (msg tm_epoch tm_rfc3339 nick tok_text))
27 (define (uri-fetch uri)
28 (log-info "GET ~a" uri)
29 (define resp (http-get uri))
30 (define status (http-response-code resp))
31 (define body (http-response-body resp))
32 (log-debug "finished GET ~a status:~a body length:~a"
33 uri status (string-length body))
34 ; TODO Handle redirects
35 (if (= status 200) body (raise status)))
38 ; TODO Zulu offset. Maybe in several formats. Which ones?
39 (let ([timestamp "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"])
40 (pregexp (string-append "^" timestamp))))
42 (define (msg-print odd m)
43 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
44 (date->string (seconds->date [msg-tm_epoch m]) #t)
49 (define (timeline-print timeline)
52 (msg-print (odd? i) msg)))
54 (define (str->lines str)
55 (string-split str (regexp "[\r\n]+")))
57 (define (str->msgs nick str)
61 (filter (λ (l) (regexp-match? re-msg-begin l)) lines)]
64 (log-debug "nick:~a lines:~a msg_lines:~a"
70 (define toks (string-split line (regexp "\t+")))
71 (if (= 2 (length toks))
74 (log-warning "Invalid msg from nick:~a, msg:~a" nick line)
80 (define (timeline feeds)
84 (log-info "processing feed nick:~a uri:~a"
90 (log-error "network error nick:~a uri:~a exn:~a"
97 (log-error "http error nick:~a uri:~a status:~a"
102 (str->msgs [feed-nick feed] [uri-fetch (feed-uri feed)])))
107 (sort timeline (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)]))])
110 (define (we-are-twtxt)
112 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
116 (str->lines payload)]
119 (define toks (string-split line))
127 (define logger (make-logger #f #f 'debug #f))
128 (define log-chan (make-log-receiver logger 'debug))
130 [date-display-format 'iso-8601]
132 (define data (sync log-chan))
133 (define level (vector-ref data 0))
134 (define msg (vector-ref data 1))
135 (define ts (date->string (current-date) #t))
136 (eprintf "~a [~a] ~a~n" ts level msg)
138 (current-logger logger))
142 (define feeds (we-are-twtxt))
143 (current-http-response-auto #f)
144 (current-http-user-agent "tt 0.0.0")
145 (date-display-format 'rfc2822)
146 (timeline-print (timeline feeds)))