2024-08-01 19:55:06 +00:00
|
|
|
;;; elfeed-export.el --- Elfeed database export -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2024 Bram Schoenmakers
|
|
|
|
|
|
|
|
;; Author: Bram Schoenmakers <me@bramschoenmakers.nl>
|
|
|
|
;; Maintainer: Bram Schoenmakers <me@bramschoenmakers.nl>
|
|
|
|
;; Created: 1 August 2024
|
|
|
|
;; Package-Version: 0.1
|
|
|
|
;; Package-Requires: ((emacs "29.1"))
|
|
|
|
;; Keywords:
|
|
|
|
;; URL:
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;; MIT License
|
|
|
|
|
|
|
|
;; Copyright (c) 2024 Bram Schoenmakers
|
|
|
|
|
|
|
|
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
;; of this software and associated documentation files (the "Software"), to deal
|
|
|
|
;; in the Software without restriction, including without limitation the rights
|
|
|
|
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
;; copies of the Software, and to permit persons to whom the Software is
|
|
|
|
;; furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
;; The above copyright notice and this permission notice shall be included in all
|
|
|
|
;; copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
;; SOFTWARE.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2024-08-01 21:17:54 +00:00
|
|
|
(require 'elfeed)
|
|
|
|
|
2024-08-01 20:05:19 +00:00
|
|
|
(defcustom elfeed-export-fields
|
2024-08-01 20:59:07 +00:00
|
|
|
'((title . elfeed-entry-title)
|
|
|
|
(date . elfeed-export--entry-date)
|
|
|
|
(url . elfeed-entry-link)
|
|
|
|
(tags . elfeed-entry-tags)
|
|
|
|
(feed-title . elfeed-export--feed-title)
|
|
|
|
(feed . elfeed-export--entry-feed)
|
|
|
|
(content . elfeed-export--entry-content))
|
|
|
|
"Which fields to export per entry and a function to obtain the value.
|
|
|
|
|
|
|
|
The function takes an entry argument for extracting the desired value."
|
|
|
|
:type 'alist
|
2024-08-01 19:55:06 +00:00
|
|
|
:group 'elfeed-export)
|
|
|
|
|
2024-08-03 07:33:26 +00:00
|
|
|
(defcustom elfeed-export-predicates
|
2024-08-03 19:50:00 +00:00
|
|
|
(list #'elfeed-export--include-entry-by-tag-p)
|
2024-08-03 07:33:26 +00:00
|
|
|
"Entries that match all of these predicates are exported.
|
|
|
|
|
|
|
|
Each predicate accepts an entry and a feed object and should
|
|
|
|
return t when the entry should be exported."
|
|
|
|
:group 'elfeed-export
|
|
|
|
:type '(repeat function))
|
|
|
|
|
|
|
|
(defcustom elfeed-export-include-tags nil
|
|
|
|
"Export entries that have any of these tags."
|
|
|
|
:group 'elfeed-export
|
|
|
|
:type '(repeat symbol))
|
|
|
|
|
|
|
|
(defcustom elfeed-export-exclude-tags '(noexport)
|
|
|
|
"Do not export entries that have any of these tags."
|
|
|
|
:group 'elfeed-export
|
|
|
|
:type '(repeat symbol))
|
|
|
|
|
2024-08-03 19:51:40 +00:00
|
|
|
(defun elfeed-export--include-entry-by-tag-p (entry _feed)
|
2024-08-03 07:33:26 +00:00
|
|
|
(let ((tags (elfeed-entry-tags entry)))
|
|
|
|
(and
|
|
|
|
(or (not elfeed-export-include-tags)
|
|
|
|
(seq-intersection elfeed-export-include-tags tags))
|
|
|
|
(or (not elfeed-export-exclude-tags)
|
|
|
|
(not (seq-intersection elfeed-export-exclude-tags tags))))))
|
|
|
|
|
2024-08-01 20:59:07 +00:00
|
|
|
(defun elfeed-export--entry-date (entry)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return an ISO formatted date string for the given ENTRY."
|
2024-08-01 20:59:07 +00:00
|
|
|
(format-time-string "%F %T" (elfeed-entry-date entry)))
|
|
|
|
|
|
|
|
(defun elfeed-export--feed-title (entry)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return the feed title for the given ENTRY."
|
2024-08-01 20:59:07 +00:00
|
|
|
(elfeed-feed-title (elfeed-entry-feed entry)))
|
|
|
|
|
|
|
|
(defun elfeed-export--entry-feed (entry)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return the feed URL for the given ENTRY."
|
2024-08-01 20:59:07 +00:00
|
|
|
(elfeed-feed-url (elfeed-entry-feed entry)))
|
|
|
|
|
|
|
|
(defun elfeed-export--entry-content (entry)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return the content of the ENTRY."
|
2024-08-01 20:59:07 +00:00
|
|
|
(elfeed-deref (elfeed-entry-content entry)))
|
|
|
|
|
2024-08-01 20:03:45 +00:00
|
|
|
(defun elfeed-export--entry-to-alist (entry)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Convert an ENTRY to an alist."
|
2024-08-01 21:24:30 +00:00
|
|
|
(mapcar (lambda (field)
|
|
|
|
(cons (symbol-name (car field))
|
|
|
|
(funcall (cdr field) entry)))
|
|
|
|
elfeed-export-fields))
|
2024-08-01 19:55:06 +00:00
|
|
|
|
2024-08-03 07:33:26 +00:00
|
|
|
(defun elfeed-export-filter-entries (entries)
|
2024-08-03 19:50:46 +00:00
|
|
|
"Filter ENTRIES according to all predictates in `elfeed-export-predicates'."
|
2024-08-03 07:33:26 +00:00
|
|
|
(seq-filter (lambda (entry)
|
|
|
|
(let ((feed (elfeed-entry-feed entry)))
|
|
|
|
(seq-every-p (lambda (pred)
|
|
|
|
(funcall pred entry feed))
|
|
|
|
elfeed-export-predicates)))
|
|
|
|
entries))
|
|
|
|
|
2024-08-01 20:32:25 +00:00
|
|
|
(defun elfeed-export--entries ()
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return all entries that need to be exported."
|
2024-08-01 19:55:06 +00:00
|
|
|
(let ((entries)
|
|
|
|
(feeds (elfeed-feed-list)))
|
2024-08-01 20:32:25 +00:00
|
|
|
(dolist (feed feeds)
|
2024-08-01 19:55:06 +00:00
|
|
|
(setq entries
|
|
|
|
(append entries
|
|
|
|
(elfeed-feed-entries feed))))
|
2024-08-03 07:33:26 +00:00
|
|
|
(mapcar #'elfeed-export--entry-to-alist
|
|
|
|
(elfeed-export-filter-entries entries))))
|
2024-08-01 20:32:25 +00:00
|
|
|
|
|
|
|
(defun elfeed-export-to-json ()
|
2024-08-02 13:29:54 +00:00
|
|
|
"Return the JSON string for all entries that should be exported."
|
2024-08-01 20:59:07 +00:00
|
|
|
(json-encode (elfeed-export--entries)))
|
2024-08-01 19:55:06 +00:00
|
|
|
|
2024-08-03 19:48:37 +00:00
|
|
|
(defun elfeed-export-to-lisp ()
|
|
|
|
"Return the Lisp data string for all entries that should be exported."
|
|
|
|
(prin1-to-string (elfeed-export--entries)))
|
|
|
|
|
|
|
|
(defun elfeed-export-to-json-file (path)
|
2024-08-02 13:29:54 +00:00
|
|
|
"Write the JSON string to a file at PATH."
|
2024-08-01 19:55:06 +00:00
|
|
|
(interactive "F")
|
|
|
|
(write-region (elfeed-export-to-json) nil path nil nil nil t))
|
|
|
|
|
2024-08-03 19:48:37 +00:00
|
|
|
(defun elfeed-export-to-lisp-file (path)
|
|
|
|
"Write the JSON string to a file at PATH."
|
|
|
|
(interactive "F")
|
|
|
|
(write-region (elfeed-export-to-lisp) nil path nil nil nil t))
|
|
|
|
|
2024-08-01 19:55:06 +00:00
|
|
|
(provide 'elfeed-export)
|
2024-08-03 07:33:26 +00:00
|
|
|
|
2024-08-01 19:55:06 +00:00
|
|
|
;;; elfeed-export.el ends here
|