Add timeline format TODOs
[tt.git] / tt
1 #! /usr/bin/env racket
2 ; vim: filetype=racket
3
4 ; TODO write
5 ; TODO caching (use cache by default, unless explicitly asked for update)
6 ; TODO timeline limits
7 ; TODO user-defined feed sets (a la twitter lists)
8 ; TODO feed set operations
9 ; TODO timeline as a result of a query (feed set op + filter expressions)
10 ; TODO named timelines
11 ; TODO alt timeline formats: irc-like | email-like
12 ;
13 ; - irc-like:
14 ; <timestamp> <nick> <msg><lf>
15 ;
16 ; - email-like:
17 ; <timestamp>
18 ; <nick> <uri>
19 ; <msg>
20 ; <lf><lf>
21 ;
22 ; TODO CLI params
23 ; TODO config files
24 ; TODO parse mentions:
25 ; - @<source.nick source.url> | @<source.url>
26 ; TODO highlight mentions
27 ; TODO filter on mentions
28 ; TODO highlight hashtags
29 ; TODO filter on hashtags
30 ; TODO hashtags as channels? initial hashtag special?
31 ; TODO query language
32 ; TODO concurrency
33 ; TODO console logger colors by level ('error)
34 ; TODO file logger ('debug)
35 ; TODO commands:
36 ; - r | read
37 ; - see timeline ops above
38 ; - w | write
39 ; - arg or stdin
40 ; - nick expand to URI
41 ; - q | query
42 ; - see timeline ops above
43 ; - see hashtag and channels above
44 ; - d | download
45 ; - u | upload
46 ; - calls user-configured command to upload user's own feed file to their server
47 ; TODO user-agent format: <client>/<version> (+<source.url>; @<source.nick>)
48 ; - requires configurability
49 ; - ref: https://twtxt.readthedocs.io/en/latest/user/discoverability.html
50
51 #lang racket
52
53 (require racket/date)
54
55 (require http-client)
56 (require rfc3339-old)
57
58 (struct msg (tm_epoch tm_rfc3339 nick text))
59 (struct feed (nick uri))
60
61 (define (msg-print odd m)
62 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
63 (date->string (seconds->date [msg-tm_epoch m]) #t)
64 [msg-nick m]
65 [if odd 36 33]
66 [msg-text m]))
67
68 (define re-msg-begin
69 ; TODO Zulu offset. Maybe in several formats. Which ones?
70 (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))
71
72 (define (str->msg nick str)
73 (if (not (regexp-match? re-msg-begin str))
74 (begin
75 (log-debug "Non-msg line from nick:~a, line:~a" nick str)
76 #f)
77 (let ([toks (string-split str (regexp "\t+"))])
78 (if (not (= 2 (length toks)))
79 (begin
80 (log-warning "Invalid msg line from nick:~a, msg:~a" nick str)
81 #f)
82 (let*
83 ([tm_rfc3339 (list-ref toks 0)]
84 [tok_text (list-ref toks 1)]
85 [t (string->rfc3339-record tm_rfc3339)]
86 ; TODO handle tz offset
87 [tm_epoch (find-seconds [rfc3339-record:second t]
88 [rfc3339-record:minute t]
89 [rfc3339-record:hour t]
90 [rfc3339-record:mday t]
91 [rfc3339-record:month t]
92 [rfc3339-record:year t])])
93 (msg tm_epoch tm_rfc3339 nick tok_text))))))
94
95 (define (str->lines str)
96 (string-split str (regexp "[\r\n]+")))
97
98 (define (str->msgs nick str)
99 (filter-map (λ (line) (str->msg nick line)) (str->lines str)))
100
101 (define (uri-fetch uri)
102 (log-info "GET ~a" uri)
103 (define resp (http-get uri))
104 (define status (http-response-code resp))
105 (define body (http-response-body resp))
106 (log-debug "finished GET ~a status:~a body length:~a"
107 uri status (string-length body))
108 ; TODO Handle redirects
109 (if (= status 200) body (raise status)))
110
111 (define (timeline-print timeline)
112 (for ([msg timeline]
113 [i (in-naturals)])
114 (msg-print (odd? i) msg)))
115
116 (define (feed->msgs feed)
117 (log-info "downloading feed nick:~a uri:~a"
118 (feed-nick feed)
119 (feed-uri feed))
120 (with-handlers
121 ([exn:fail:network?
122 (λ (e)
123 (log-error "network error nick:~a uri:~a exn:~a"
124 (feed-nick feed)
125 (feed-uri feed)
126 e)
127 #f)]
128 [integer?
129 (λ (status)
130 (log-error "http error nick:~a uri:~a status:~a"
131 (feed-nick feed)
132 (feed-uri feed)
133 status)
134 #f)])
135 (str->msgs [feed-nick feed] [uri-fetch (feed-uri feed)])))
136
137 ; TODO timeline contract : time-sorted list of messages
138 (define (timeline feeds)
139 (sort (append* (filter-map feed->msgs feeds))
140 (λ (a b) [< (msg-tm_epoch a) (msg-tm_epoch b)])))
141
142 (define (we-are-twtxt)
143 (let* ([uri
144 "https://raw.githubusercontent.com/mdom/we-are-twtxt/master/we-are-twtxt.txt"]
145 [payload
146 (uri-fetch uri)]
147 [lines
148 (str->lines payload)]
149 [feeds
150 (map (λ (line)
151 ; TODO validation
152 (define toks (string-split line))
153 (feed
154 [list-ref toks 0]
155 [list-ref toks 1]))
156 lines)])
157 feeds))
158
159 (define (setup-logging)
160 (define logger (make-logger #f #f 'debug #f))
161 (define log-chan (make-log-receiver logger 'debug))
162 (void (thread (λ ()
163 [date-display-format 'iso-8601]
164 [let loop ()
165 (define data (sync log-chan))
166 (define level (vector-ref data 0))
167 (define msg (vector-ref data 1))
168 (define ts (date->string (current-date) #t))
169 (eprintf "~a [~a] ~a~n" ts level msg)
170 (loop)])))
171 (current-logger logger))
172
173 (define (main)
174 (setup-logging)
175 (current-http-response-auto #f)
176 (current-http-user-agent "xandkar/tt 0.0.0")
177 (date-display-format 'rfc2822)
178
179 (define feeds (we-are-twtxt))
180 (timeline-print (timeline feeds)))
181
182 (main)
This page took 0.076396 seconds and 4 git commands to generate.