X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=tt;h=840a8e5f4e8943e6fb1fec792936e8751a03f643;hb=refs%2Ftags%2F0.3.4;hp=aa6df5ab92ba668b4abf25500ed62f83d18e9c70;hpb=87d0eb92432ea01fe296bb17aec71d11e414524d;p=tt.git diff --git a/tt b/tt index aa6df5a..840a8e5 100755 --- a/tt +++ b/tt @@ -4,6 +4,9 @@ ; TODO optional text wrap ; TODO write ; TODO caching (use cache by default, unless explicitly asked for update) +; - [x] value --> cache +; - [ ] value <-- cache +; requires: commands ; TODO timeline limits ; TODO feed set operations (perhaps better done externally?) ; TODO timeline as a result of a query (feed set op + filter expressions) @@ -37,6 +40,7 @@ #lang racket +(require openssl/sha1) (require racket/date) (require http-client) @@ -73,12 +77,9 @@ [(cons x xs) (begin (thread-send thd (cons 'unit x)) (dispatch ws xs ys))])]))) - (define workers - (range 1 (add1 num_workers))) - (define threads - (map (λ (id) (thread (make-worker id f))) workers)) - (define results - (dispatch workers xs '())) + (define workers (range num_workers)) + (define threads (map (λ (id) (thread (make-worker id f))) workers)) + (define results (dispatch workers xs '())) (for-each thread-wait threads) results) @@ -127,6 +128,12 @@ (define (str->msgs nick uri str) (filter-map (λ (line) (str->msg nick uri line)) (str->lines str))) +(define (hash-sha1 str) + (define in (open-input-string str)) + (define digest (sha1 in)) + (close-input-port in) + digest) + (define (uri-fetch uri) (log-info "GET ~a" uri) (define resp (http-get uri)) @@ -135,7 +142,18 @@ (log-debug "finished GET ~a status:~a body length:~a" uri status (string-length body)) ; TODO Handle redirects - (if (= status 200) body (raise status))) + (if (= status 200) + (let* + ([url-digest + (hash-sha1 uri)] + [cache-file-path + (expand-user-path (string-append "~/.tt/cache/" url-digest))]) + (display-to-file + body cache-file-path + #:exists 'replace) + body) + ; TODO A more-informative exception + (raise status))) (define (timeline-print out-format timeline) (for ([msg timeline] @@ -203,14 +221,16 @@ (define user-agent (let* ([prog-name "tt"] - [prog-version "0.3.1"] + [prog-version "0.3.4"] + [prog-uri "https://github.com/xandkar/tt"] [user-feed-file (expand-user-path "~/twtxt-me.txt")] - [user (first (file->feeds user-feed-file))]) - (format "~a/~a (+~a; @~a)" - prog-name - prog-version - (feed-uri user) - (feed-nick user)))) + [user + (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/~a (~a)" prog-name prog-version user))) (setup-logging) (current-http-response-auto #f)