~cytrogen/.emacs.d

ref: b2550155de22f34a19e99877ce79c7d8a23c9731 .emacs.d/config/pkg-reading.el -rw-r--r-- 11.5 KiB
b2550155 — Cytrogen feat(email): 完善 mu4e 多账户配置和 HTML 渲染 a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
;;; pkg-reading.el --- RSS reader, OPDS browser, and ebook tools -*- lexical-binding: t -*-

;; Copyright (C) 2024 Cytrogen

;;; Commentary:

;; This module provides:
;; - elfeed + elfeed-protocol for FreshRSS (Fever API)
;; - Custom OPDS browser for Calibre Content Server
;; - nov.el for EPUB reading
;; - pdf-tools for PDF reading
;;
;; Configuration is stored in ~/.emacs.d/reading-feeds.el

;;; Code:

(require 'url)
(require 'xml)
(require 'dom)

;;; ============================================================
;;; Configuration Loading
;;; ============================================================

(defvar my/reading-config nil
  "Reading configuration loaded from external file.")

(defvar my/reading-config-file
  (expand-file-name "reading-feeds.el" user-emacs-directory)
  "Path to the reading configuration file.")

(defun my/reading--load-config ()
  "Load reading configuration from external file."
  (when (file-exists-p my/reading-config-file)
    (load-file my/reading-config-file)))

;; Load config immediately
(my/reading--load-config)

(defun my/reading--get-config (key &optional subkey)
  "Get configuration value for KEY, optionally SUBKEY."
  (let ((section (plist-get my/reading-config key)))
    (if subkey
        (plist-get section subkey)
      section)))

;;; ============================================================
;;; Package Installation
;;; ============================================================

(unless (package-installed-p 'elfeed)
  (package-refresh-contents)
  (package-install 'elfeed))

(unless (package-installed-p 'elfeed-protocol)
  (package-install 'elfeed-protocol))

(unless (package-installed-p 'nov)
  (package-install 'nov))

(unless (package-installed-p 'pdf-tools)
  (package-install 'pdf-tools))

;;; ============================================================
;;; EPUB Reader (nov.el)
;;; ============================================================

(require 'nov)
(add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))

(with-eval-after-load 'nov
  (setq nov-text-width t)
  (add-hook 'nov-mode-hook 'visual-line-mode))

;;; ============================================================
;;; PDF Reader (pdf-tools)
;;; ============================================================

(pdf-loader-install)

(with-eval-after-load 'pdf-tools
  (setq pdf-view-use-scaling t))

;;; ============================================================
;;; RSS Reader (elfeed + FreshRSS)
;;; ============================================================

(require 'elfeed)
(require 'elfeed-protocol)

(defun my/reading--setup-elfeed ()
  "Setup elfeed with FreshRSS configuration."
  (my/reading--load-config)
  (let* ((freshrss (my/reading--get-config :freshrss))
         (url (plist-get freshrss :url))
         (api-path (plist-get freshrss :api-path))
         (user (plist-get freshrss :user))
         (password (plist-get freshrss :password)))
    (when (and url user password)
      ;; 基础设置
      (setq elfeed-use-curl t)
      (elfeed-set-timeout 36000)
      (setq elfeed-curl-extra-arguments '("--insecure"))

      ;; 调试日志
      (setq elfeed-protocol-log-trace t)

      ;; 协议设置
      (setq elfeed-protocol-enabled-protocols '(fever))
      ;; FreshRSS 兼容性:不提供有效 item ID,需要只更新未读
      (setq elfeed-protocol-fever-update-unread-only t)
      (setq elfeed-protocol-feeds
            `((,(format "fever+%s://%s@%s"
                        (if (string-prefix-p "https" url) "https" "http")
                        user
                        (replace-regexp-in-string "^https?://" "" url))
               :api-url ,(concat url api-path)
               :password ,password)))

      ;; 重新启用协议(确保 advice 生效)
      (when (fboundp 'elfeed-protocol-disable)
        (elfeed-protocol-disable))
      (elfeed-protocol-enable)

      (message "elfeed configured: %s" (caar elfeed-protocol-feeds)))))

;; 延迟配置
(with-eval-after-load 'elfeed
  (my/reading--setup-elfeed))

(defun my/elfeed ()
  "Start elfeed and update feeds."
  (interactive)
  (my/reading--setup-elfeed)
  (elfeed)
  (elfeed-update))

;;; ============================================================
;;; OPDS Browser (Calibre)
;;; ============================================================

(defvar my/opds-current-entries nil
  "Current OPDS entries being displayed.")

(defvar my/opds-history nil
  "Navigation history for OPDS browser.")

(defun my/opds--fetch-url (url &optional callback)
  "Fetch URL and return parsed XML. If CALLBACK, call it with result."
  (let* ((opds-config (my/reading--get-config :opds))
         (user (plist-get opds-config :user))
         (password (plist-get opds-config :password))
         (url-request-extra-headers
          (when (and user password)
            `(("Authorization" .
               ,(concat "Basic "
                        (base64-encode-string
                         (format "%s:%s" user password))))))))
    (if callback
        (url-retrieve url
                      (lambda (_status)
                        (goto-char url-http-end-of-headers)
                        (skip-chars-forward "\r\n \t")
                        (set-buffer-multibyte t)
                        (decode-coding-region (point) (point-max) 'utf-8)
                        (let ((xml (libxml-parse-xml-region (point) (point-max))))
                          (funcall callback xml))))
      (with-current-buffer (url-retrieve-synchronously url)
        (goto-char url-http-end-of-headers)
        (skip-chars-forward "\r\n \t")
        (set-buffer-multibyte t)
        (decode-coding-region (point) (point-max) 'utf-8)
        (prog1 (libxml-parse-xml-region (point) (point-max))
          (kill-buffer))))))

(defun my/opds--parse-entries (xml)
  "Parse OPDS XML and return list of entries."
  (let ((entries (dom-by-tag xml 'entry)))
    (mapcar (lambda (entry)
              (let* ((title (dom-text (dom-by-tag entry 'title)))
                     (author (dom-text (dom-by-tag (dom-by-tag entry 'author) 'name)))
                     (links (dom-by-tag entry 'link))
                     (acquisition-link
                      (cl-find-if (lambda (link)
                                    (let ((rel (dom-attr link 'rel))
                                          (type (dom-attr link 'type)))
                                      (or (string-match-p "acquisition" (or rel ""))
                                          (string-match-p "epub\\|pdf" (or type "")))))
                                  links))
                     (nav-link
                      (cl-find-if (lambda (link)
                                    (let ((type (dom-attr link 'type)))
                                      (string-match-p "atom\\|opds" (or type ""))))
                                  links)))
                `(:title ,title
                  :author ,author
                  :download-url ,(when acquisition-link (dom-attr acquisition-link 'href))
                  :download-type ,(when acquisition-link (dom-attr acquisition-link 'type))
                  :nav-url ,(when nav-link (dom-attr nav-link 'href)))))
            entries)))

(defun my/opds--format-entry (entry)
  "Format ENTRY for display."
  (let ((title (plist-get entry :title))
        (author (plist-get entry :author))
        (has-download (plist-get entry :download-url))
        (has-nav (plist-get entry :nav-url)))
    (format "%s%s%s"
            (or title "Unknown")
            (if author (format " - %s" author) "")
            (cond (has-download " [BOOK]")
                  (has-nav " [>]")
                  (t "")))))

(defun my/opds--select-entry (entry)
  "Handle selection of ENTRY."
  (let ((download-url (plist-get entry :download-url))
        (nav-url (plist-get entry :nav-url)))
    (cond
     (download-url
      (my/opds-download-and-open entry))
     (nav-url
      (push (my/reading--get-config :opds :url) my/opds-history)
      (my/opds--browse-url nav-url)))))

(defun my/opds--browse-url (url)
  "Browse OPDS catalog at URL."
  (message "Fetching OPDS catalog...")
  (let* ((base-url (my/reading--get-config :opds :url))
         (full-url (if (string-prefix-p "http" url)
                       url
                     (concat (replace-regexp-in-string "/opds/?$" "" base-url) url))))
    (condition-case err
        (let* ((xml (my/opds--fetch-url full-url))
               (entries (my/opds--parse-entries xml)))
          (setq my/opds-current-entries entries)
          (let* ((choices (mapcar (lambda (e)
                                    (cons (my/opds--format-entry e) e))
                                  entries))
                 (selection (completing-read "OPDS: " choices nil t)))
            (when selection
              (my/opds--select-entry (cdr (assoc selection choices))))))
      (error (message "OPDS error: %s" (error-message-string err))))))

(defun my/opds-browse ()
  "Browse OPDS catalog."
  (interactive)
  (my/reading--load-config)
  (let ((url (my/reading--get-config :opds :url)))
    (if url
        (progn
          (setq my/opds-history nil)
          (my/opds--browse-url url))
      (message "OPDS URL not configured. Edit reading-feeds.el"))))

(defun my/opds-back ()
  "Go back in OPDS navigation history."
  (interactive)
  (if my/opds-history
      (my/opds--browse-url (pop my/opds-history))
    (message "No history")))

(defun my/opds-download-and-open (entry)
  "Download and open ENTRY."
  (let* ((url (plist-get entry :download-url))
         (type (plist-get entry :download-type))
         (title (plist-get entry :title))
         (download-dir (or (my/reading--get-config :download-dir) "~/Books/"))
         (ext (cond ((string-match-p "epub" (or type "")) ".epub")
                    ((string-match-p "pdf" (or type "")) ".pdf")
                    (t ".epub")))
         (filename (concat (replace-regexp-in-string "[^a-zA-Z0-9]+" "_" (or title "book")) ext))
         (filepath (expand-file-name filename download-dir))
         (opds-config (my/reading--get-config :opds))
         (base-url (plist-get opds-config :url))
         (full-url (if (string-prefix-p "http" url)
                       url
                     (concat (replace-regexp-in-string "/opds/?$" "" base-url) url))))
    ;; Ensure download directory exists
    (unless (file-directory-p download-dir)
      (make-directory download-dir t))
    (message "Downloading %s..." title)
    (let* ((user (plist-get opds-config :user))
           (password (plist-get opds-config :password))
           (url-request-extra-headers
            (when (and user password)
              `(("Authorization" .
                 ,(concat "Basic "
                          (base64-encode-string
                           (format "%s:%s" user password))))))))
      (url-copy-file full-url filepath t)
      (message "Downloaded to %s" filepath)
      (find-file filepath))))

(defun my/opds-search ()
  "Search OPDS catalog."
  (interactive)
  (my/reading--load-config)
  (let* ((query (read-string "Search books: "))
         (base-url (my/reading--get-config :opds :url))
         (search-url (format "%s/search?query=%s"
                             (replace-regexp-in-string "/opds/?$" "/opds" base-url)
                             (url-hexify-string query))))
    (my/opds--browse-url search-url)))

;;; ============================================================
;;; Interactive Commands
;;; ============================================================

(defun my/reading-reload-config ()
  "Reload reading configuration."
  (interactive)
  (my/reading--load-config)
  (when (featurep 'elfeed)
    (my/reading--setup-elfeed))
  (message "Reading configuration reloaded"))

(provide 'pkg-reading)
;;; pkg-reading.el ends here