Add TODOs for typed and executable
[tt.git] / tt.rkt
diff --git a/tt.rkt b/tt.rkt
index e834505..59214ff 100644 (file)
--- a/tt.rkt
+++ b/tt.rkt
            ['single-line
             (printf "~a  \033[1;37m<~a>\033[0m  \033[0;~am~a\033[0m~n"
                     (parameterize ([date-display-format 'iso-8601])
-                      (date->string (seconds->date [msg-ts_epoch msg]) #t))
+                                  (date->string (seconds->date [msg-ts_epoch msg]) #t))
                     nick color text)]
            ['multi-line
             (printf "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
                     (parameterize ([date-display-format 'rfc2822])
-                      (date->string (seconds->date [msg-ts_epoch msg]) #t))
+                                  (date->string (seconds->date [msg-ts_epoch msg]) #t))
                     nick uri color text)])))))
 
 ; TODO Implement rfc3339->epoch and remove dependency on rfc3339-old
   (close-input-port in)
   digest)
 
-(define (uri-fetch use-cache uri)
+(define (uri-read-cached uri)
   (define cache-file-path
     (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
-  (if (and use-cache (file-exists? cache-file-path))
+  (if (file-exists? cache-file-path)
+      (file->string cache-file-path)
       (begin
-        (log-info "uri-fetch cached ~a" uri)
-        (file->string cache-file-path))
-      (begin
-        (log-info "uri-fetch new ~a" uri)
-        ; TODO Timeout. Currently hangs on slow connections.
-        (let* ([resp   (http-get uri)]
-               [status (http-response-code resp)]
-               [body   (http-response-body resp)])
-          (log-debug "finished GET ~a status:~a  body length:~a"
-                     uri status (string-length body))
-          ; TODO Handle redirects
-          (if (= status 200)
-              (begin
-                (display-to-file body cache-file-path #:exists 'replace)
-                body)
-              ; TODO A more-informative exception
-              (raise status))))))
+        (log-warning "Cache file not found for URI: ~a" uri)
+        "")))
+
+; uri-download : String -> Void
+(define (uri-download uri)
+  (define cache-file-path
+    (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
+  (log-info "uri-download ~a" uri)
+  ; TODO Timeout. Currently hangs on slow connections.
+  (let* ([resp   (http-get uri)]
+         [status (http-response-code resp)]
+         [body   (http-response-body resp)])
+    (log-debug "finished GET ~a status:~a  body length:~a"
+               uri status (string-length body))
+    ; TODO Handle redirects
+    (if (= status 200)
+        (display-to-file body cache-file-path #:exists 'replace)
+        ; TODO A more-informative exception
+        (raise status))))
 
 (define (timeline-print out-format timeline)
   (void (foldl (match-lambda**
                (cons "" 0)
                timeline)))
 
-(define (feed->msgs use-cache feed)
-  (log-info "downloading feed nick:~a uri:~a"
+; feed->msgs : Feed -> (Listof Msg)
+(define (feed->msgs feed)
+  (log-info "Reading feed nick:~a uri:~a"
+            (feed-nick feed)
+            (feed-uri feed))
+  (define uri (feed-uri feed))
+  (str->msgs (feed-nick feed) uri (uri-read-cached uri)))
+
+; feed-download : Feed -> Void
+(define (feed-download feed)
+  (log-info "Downloading feed nick:~a uri:~a"
             (feed-nick feed)
             (feed-uri feed))
   (with-handlers
     ([exn:fail:network?
        (λ (e)
-          (log-error "network error nick:~a uri:~a  exn:~a"
+          (log-error "Network error nick:~a uri:~a  exn:~a"
                      (feed-nick feed)
                      (feed-uri feed)
                      e)
           #f)]
      [integer?
        (λ (status)
-          (log-error "http error nick:~a uri:~a  status:~a"
+          (log-error "HTTP error nick:~a uri:~a  status:~a"
                      (feed-nick feed)
                      (feed-uri feed)
                      status)
           #f)])
-    (define uri (feed-uri feed))
-    (str->msgs [feed-nick feed] uri [uri-fetch use-cache uri])))
+    (uri-download (feed-uri feed))))
+
+; timeline-download : Integer -> (Listof Feed) -> Void
+(define (timeline-download num_workers feeds)
+  ; TODO No need for map - can just iter
+  (void (concurrent-filter-map num_workers feed-download feeds)))
 
 ; TODO timeline contract : time-sorted list of messages
-(define (timeline use-cache num_workers feeds)
-  (sort (append* (concurrent-filter-map num_workers (curry feed->msgs use-cache) feeds))
-        (λ (a b) [< (msg-ts_epoch a) (msg-ts_epoch b)])))
+; timeline-read : (U 'old->new 'new->old) -> (Listof Feeds) -> (Listof Msg)
+(define (timeline-read order feeds)
+  (define cmp (match order
+                ['old->new <]
+                ['new->old >]))
+  (sort (append* (filter-map feed->msgs feeds))
+        (λ (a b) (cmp (msg-ts_epoch a) (msg-ts_epoch b)))))
 
 (define (str->feed str)
   ; TODO validation
        (if (file-exists? user-feed-file)
            (let ([user (first (file->feeds user-feed-file))])
              (format "+~a; @~a" (feed-uri user) (feed-nick user)))
-           (format "+~a" prog-uri))]
-     )
+           (format "+~a" prog-uri))])
     (format "~a/~a (~a)" prog-name prog-version user)))
 
 (define (start-logger level)
       #:help-labels
       ""
       "and <command> is one of"
-      "r, read : Read the timeline."
+      "r, read i   : Read the timeline."
+      "d, download : Download the timeline."
+      "u, upload   : Upload your twtxt file (alias to execute ~/.tt/upload)."
       ""
       #:args (command . args)
+      (start-logger log-level)
+      (current-command-line-arguments (list->vector args))
       (match command
-        [(or "r" "read")
-         (current-command-line-arguments (list->vector args))
-         (let ([use-cache
-                 #f]
-               [out-format
-                 'multi-line]
-               [num_workers
-                 ; 15 was fastest out of the tried 1, 5, 10, 15 and 20.
-                 15])
+        [(or "d" "download")
+         (let ([num_workers 15]) ; 15 was fastest out of the tried: 1, 5, 10, 20.
            (command-line
              #:program
-             "tt read"
+             "tt download"
              #:once-each
              [("-j" "--jobs")
               njobs "Number of concurrent jobs."
               (set! num_workers (string->number njobs))]
-             [("-c" "--cached")
-              "Read cached data instead of downloading."
-              (set! use-cache #t)]
+
+             #:args (filename)
+
+             (current-http-client/response-auto #f)
+             (let* ([prog-name    "tt"]
+                    [prog-version ((info:get-info (list prog-name)) 'version)]
+                    [user-agent   (user-agent prog-name prog-version)])
+               (current-http-client/user-agent user-agent))
+             (timeline-download num_workers (file->feeds filename))
+             ))]
+        [(or "u" "upload")
+         (command-line
+             #:program
+             "tt upload"
+             #:args ()
+             (if (system (path->string (expand-user-path "~/.tt/upload")))
+               (exit 0)
+               (exit 1)))]
+        [(or "r" "read")
+         (let ([out-format 'multi-line]
+               [order      'old->new])
+           (command-line
+             #:program
+             "tt read"
+             #:once-each
+             [("-r" "--rev")
+              "Reverse displayed timeline order."
+              (set! order 'new->old)]
              #:once-any
              [("-s" "--short")
               "Short output format"
               "Long output format"
               (set! out-format 'multi-line)]
              #:args (filename)
-             (start-logger log-level)
-             (current-http-client/response-auto #f)
-             (let* ([prog-name    "tt"]
-                    [prog-version ((info:get-info (list prog-name)) 'version)]
-                    [user-agent   (user-agent prog-name prog-version)])
-               (current-http-client/user-agent user-agent))
-             (timeline-print out-format
-                             (timeline use-cache
-                                       num_workers
-                                       (file->feeds filename)))))]
+             (timeline-print out-format (timeline-read order (file->feeds filename)))))]
         ))))
This page took 0.034617 seconds and 4 git commands to generate.