Add --cache option for feeds fetched by URL, completing the UNIX interface

This commit is contained in:
Jaidyn Ann 2022-12-28 16:01:35 -06:00
parent 8041ec2655
commit 45646725e0

View File

@ -142,7 +142,7 @@
;; or #f if literally nothing's changed ;; or #f if literally nothing's changed
(define (updated-feed-string url old-string) (define (updated-feed-string url old-string)
(let* ([new-string (fetch-http-string url)] (let* ([new-string (fetch-http-string url)]
[updated? (not (eq? (hash old-string) (hash new-string)))]) [updated? (not (string=? old-string new-string))])
(if updated? (if updated?
new-string new-string
#f))) #f)))
@ -248,17 +248,22 @@
'message (string-append base-out-path " either isn't accessible or isn't a directory.")))))) 'message (string-append base-out-path " either isn't accessible or isn't a directory."))))))
;; Switch the cached version of the feed with a newer version, if available ;; Switch the cached version of the feed with a newer version, if available.
;; If the feed-path doesn't exist, then the feed will be downloaded fresh.
(define (update-feed-file feed-path feed-url) (define (update-feed-file feed-path feed-url)
(let* ([old-string (call-with-input-file feed-path (let* ([old-string (if (file-exists? feed-path)
(lambda (in-port) (read-string #f in-port)))] (call-with-input-file feed-path
(lambda (in-port)
(read-string #f in-port)))
"")]
[new-string (updated-feed-string [new-string (updated-feed-string
feed-url feed-url
old-string)]) old-string)])
(if new-string (if new-string
(call-with-output-file feed-path (call-with-output-file feed-path
(lambda (out) (write-string new-string #f out)))) (lambda (out-port)
new-string)) (write-string new-string #f out-port)))
#f)))
;; List of all entries of the feed ;; List of all entries of the feed
@ -360,6 +365,7 @@
date-strings date-strings
feedsnake feedsnake-helpers feedsnake feedsnake-helpers
getopt-long getopt-long
uri-common
xattr) xattr)
@ -370,7 +376,7 @@
" feedsnake [-h] [-s] [-o|d]\n\n" " feedsnake [-h] [-s] [-o|d]\n\n"
"Feedsnake is a program for converting Atom feeds into mbox/maildir files.\n" "Feedsnake is a program for converting Atom feeds into mbox/maildir files.\n"
"Any Atom feeds passed as input will be output in mbox or maildir format.\n\n" "Any Atom feeds passed as input will be output in mbox or maildir format.\n\n"
"If a FILE value is '-' not provided, feedsnake will read a feed over standard\n" "If a FILE value is '-' or not provided, feedsnake will read a feed over standard\n"
"input. --since-last and similar arguments have no impact on these feeds.\n\n" "input. --since-last and similar arguments have no impact on these feeds.\n\n"
"If you want to subscribe to feeds with Feedsnake, you'll probably do something\n" "If you want to subscribe to feeds with Feedsnake, you'll probably do something\n"
"like so:\n" "like so:\n"
@ -463,25 +469,48 @@
(help) (help)
(map (lambda (feed-pair) (map (lambda (feed-pair)
(process-feed args feed-pair)) (process-feed args feed-pair))
(get-feeds free-args)))))) (get-feeds free-args args))))))
;; Turn the scripts free-args into parsed Feedsnake feed alists ;; Turn the scripts free-args into parsed Feedsnake feed alists
(define (get-feeds free-args) (define (get-feeds free-args args)
(let ([feed-paths (let ([feed-paths
(if (eq? (length free-args) 0) (if (eq? (length free-args) 0)
'("-") '("-")
free-args)]) free-args)])
(map get-feed feed-paths))) (map (lambda (path) (get-feed path args))
feed-paths)))
;; Turn a given feed-path (free-arg) into a parsed Feedsnake feed, if possible ;; Turn a given feed-path (free-arg) into a parsed Feedsnake feed, if possible
(define (get-feed feed-path) (define (get-feed feed-path args)
(let ([feed (let*
(if (string=? feed-path "-") ([uri (ignore-errors (absolute-uri feed-path))]
(call-with-input-string (read-string) read-feed) [out-path (cond
(ignore-errors (call-with-input-file feed-path read-feed)))]) [(and uri (alist-ref 'cache args))
(list feed-path feed))) (alist-ref 'cache args)]
[uri "-"]
[#t feed-path])]
[feed
(cond
[(string=? feed-path "-")
(call-with-input-string (read-string)
read-feed)]
[(and uri (not (string=? "-" out-path)))
(begin
(update-feed-file out-path (uri->string uri))
(ignore-errors (call-with-input-file out-path read-feed)))]
[uri
(call-with-input-string (updated-feed-string (uri->string uri) "")
read-feed)]
[#t
(ignore-errors (call-with-input-file out-path read-feed))])])
;; Set the origin URL, if newly-created cache file
(if (and uri (not (string=? "-" out-path)))
(set-xattr out-path "user.xdg.origin.url" (uri->string uri)))
(list out-path feed)))
;; Process a parsed feed, given arguments passed to the script ;; Process a parsed feed, given arguments passed to the script