Add -uS alias, --update-since/-U; fix parsing-date saving

Previously, if the feed was updated, the parse-date was saved before
actually filtering entries, leading to filtering everything out.
This commit is contained in:
Jaidyn Ann 2022-12-28 14:27:07 -06:00
parent 2269ac403f
commit 3f0bb384ca

View File

@ -121,15 +121,20 @@
#f))) #f)))
;; Construct a filter function for feeds, given the script's arguments
(define (filter-entries feed filter) (define (filter-entries feed filter)
(let ([entry-date (lambda (entry) (car (alist-ref 'updated entry)))] (let ([entry-date (lambda (entry) (car (alist-ref 'updated entry)))]
[unfiltered (if feed
(alist-car-ref 'entries feed)
#f)]
[entries '()]) [entries '()])
(map (if unfiltered
(lambda (entry) (map
(if (apply filter (list entry)) (lambda (entry)
(set! entries (if (apply filter (list entry))
(append entries (list entry))))) (set! entries
(car (alist-ref 'entries feed))) (append entries (list entry)))))
unfiltered))
entries)) entries))
@ -258,7 +263,7 @@
;; List of all entries of the feed ;; List of all entries of the feed
(define (all-entries feed) (define (all-entries feed)
(car (alist-ref 'entries feed))) (alist-car-ref 'entries feed))
;; Atom parsing ;; Atom parsing
@ -383,6 +388,9 @@
(update (update
"Update feeds by downloading new versions to the same path." "Update feeds by downloading new versions to the same path."
(single-char #\u)) (single-char #\u))
(update-since
"Alias for --update and --since-last. This is probably the option you want."
(single-char #\U))
(since (since
"Output entries after the given date, in YYYY-MM-DD hh:mm:ss format." "Output entries after the given date, in YYYY-MM-DD hh:mm:ss format."
(single-char #\s) (single-char #\s)
@ -428,14 +436,14 @@
;; The `main` procedure that should be called to run feedsnake-unix for use as script. ;; The `main` procedure that should be called to run feedsnake-unix for use as script.
(define (main) (define (main)
;; (exception-condom (exception-condom
(let* ([args (getopt-long (command-line-arguments) *opts*)] (let* ([args (getopt-long (command-line-arguments) *opts*)]
[free-args (alist-ref '@ args)]) [free-args (alist-ref '@ args)])
(if (alist-ref 'help args) (if (alist-ref 'help args)
(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))))))
;; Turn the scripts free-args into parsed Feedsnake feed alists ;; Turn the scripts free-args into parsed Feedsnake feed alists
@ -459,24 +467,30 @@
;; Process a parsed feed, given arguments passed to the script ;; Process a parsed feed, given arguments passed to the script
(define (process-feed args feed-pair) (define (process-feed args feed-pair)
(let* ([feed (last feed-pair)] (let* ([feed (last feed-pair)]
[feed-path (first feed-pair)]) [feed-path (first feed-pair)]
[update? (or (alist-ref 'update args) (alist-ref 'update-since args))])
;; Update the feed ;; Update the feed
(if (alist-ref 'update args) (if update?
(begin (begin
(update-feed-file feed-path (update-feed-file feed-path
(get-xattr feed-path "user.xdg.origin.url")) (get-xattr feed-path "user.xdg.origin.url"))
(set! feed (call-with-input-file feed-path read-feed)) (set! feed (call-with-input-file feed-path read-feed))))
(if (not (alist-ref 'no-save-date args))
(set-xattr feed-path "user.feedsnake.updated"
(date->rfc339-string (current-date-utc))))))
;; Save the parsing date, unless the user doesn't want that ;; Print all entries to stdout
(output-entries args `(,feed-path ,feed))
;; Change file's update-date
(if (and update?
(not (alist-ref 'no-save-date args)))
(set-xattr feed-path "user.feedsnake.updated"
(date->rfc339-string (current-date-utc))))
;; Save the file's parsing date
(if (and (file-exists? feed-path) (if (and (file-exists? feed-path)
(not (alist-ref 'no-save-date args))) (not (alist-ref 'no-save-date args)))
(set-xattr feed-path "user.feedsnake.parsed" (set-xattr feed-path "user.feedsnake.parsed"
(date->rfc339-string (current-date-utc)))) (date->rfc339-string (current-date-utc))))))
(output-entries args `(,feed-path ,feed))))
;; Output the appropriate entrise of the given feed, using script's args ;; Output the appropriate entrise of the given feed, using script's args
@ -485,15 +499,18 @@
[output-dir (alist-ref 'outdir args)] [output-dir (alist-ref 'outdir args)]
[output (or (alist-ref 'output args) output-dir)] [output (or (alist-ref 'output args) output-dir)]
[template (if output-dir *maildir-template* *mbox-template*)] [template (if output-dir *maildir-template* *mbox-template*)]
[filter (entry-filter feed-pair args)]) [filter (entry-filter feed-pair args)]
[entries (filter-entries feed filter)])
(cond (cond
[(not entries)
#f]
[output [output
(write-entries-to-file (filter-entries feed filter) template output)] (write-entries-to-file entries template output)]
[(not output) [(not output)
(map (lambda (entry) (map (lambda (entry)
(write-entry entry template (write-entry entry template
(open-output-file* fileno/stdout))) (open-output-file* fileno/stdout)))
(filter-entries feed filter))]))) entries)])))
;; Construct a filter function for feeds, given the script's arguments ;; Construct a filter function for feeds, given the script's arguments
@ -516,15 +533,14 @@
(lambda (entry) (lambda (entry)
(cond [since (cond [since
(date>=? (entry-date entry) since)] (date>=? (entry-date entry) since)]
[(alist-ref 'since-last args) [(or (alist-ref 'since-last args) (alist-ref 'update-since args))
(date>=? (entry-date entry) last-parse)] (date>=? (entry-date entry) (or last-parse last-update))]
[(alist-ref 'since-update args) [(alist-ref 'since-update args)
(date>=? (entry-date entry) last-update)] (date>=? (entry-date entry) last-update)]
[#t [#t
#t])))) #t]))))
;; Supposed config root of the user (as per XDG, or simple ~/.config) ;; Supposed config root of the user (as per XDG, or simple ~/.config)
(define (config-directory) (define (config-directory)
(or (get-environment-variable "XDG_CONFIG_HOME") (or (get-environment-variable "XDG_CONFIG_HOME")