summaryrefslogtreecommitdiff
path: root/elfeed-conf.el
blob: 4f1c2cca455774cb914007fbc2966807b8765ba6 (plain)
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
;;; elfeed-conf.el --- My configurations for elfeed  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  李俊緯

;; Author: 李俊緯 <mmemmew@gmail.com>
;; Keywords: comm, hypermedia

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This is my configuration file of elfeed, for viewing RSS/atom
;; feeds.

;;; Code:

(use-package "elfeed" 'elfeed)

(setq elfeed-feeds
      (list (list "https://blog.tanyakhovanova.com/feed/"
                  'math 'khovanova 'blog)
            (list "https://wiwi.video/feeds/videos.atom?sort=-publishedAt"
                  'wiwi)
            (list "https://wiwikuan.com/index.xml"
                  'wiwi 'blog)
            (list "https://protesilaos.com/codelog.xml"
                  'prot 'code)
            (list "https://protesilaos.com/news.xml"
                  'prot 'news)
            (list "https://protesilaos.com/politics.xml"
                  'prot 'politics)
            (list "https://www.youtube.com/feeds/videos.xml?channel_id=UCcXhhVwCT6_WqjkEniejRJQ"
                  'wintergaten 'youtube)
            (list "https://wintergatan.net/blogs/wintergatan.atom"
                  'wintergaten 'blog)
            (list "https://notrelated.xyz/rss"
                  'lukesmith 'notrelated)
            (list "https://lukesmith.xyz/peertube"
                  'lukesmith 'peertube)
            (list "https://www.youtube.com/feeds/videos.xml?channel_id=UC2eYFnH61tmytImy1mTYvhA"
                  'lukesmith 'youtube)
            (list "https://www.youtube.com/feeds/videos.xml?channel_id=UCiWXd0nmBjlKROwzMyPV-Nw"
                  'zhichi 'youtube)
            ;; (list "https://www.youtube.com/feeds/videos.xml?channel_id=UCPtfAE6_21oqjO7Bh2bJcog"
            ;;       'teukkaniikka 'arduino)
            ))

(define-key elfeed-search-mode-map (vector #x78) 'elfeed-search-exit)

;; I still prefer the defaults.

(setq elfeed-show-entry-switch #'switch-to-buffer)
(setq elfeed-show-entry-delete #'elfeed-kill-buffer)

;;;###autoload
(defun elfeed-display-buffer (buffer)
  "Display BUFFER in the bottom window."
  (display-buffer buffer
                  '(display-buffer-reuse-mode-window display-buffer-at-bottom)))

;;;###autoload
(defun durand-elfeed-kill-buffer ()
  "Kill the buffer and delete its window."
  (kill-buffer (current-buffer))
  (cond
   ((one-window-p))
   ((delete-window (get-buffer-window (current-buffer))))))

;;;###autoload
(defun elfeed-search-exit ()
  "Save the database, then kill all related buffers."
  (interactive)
  (elfeed-db-save)
  (mapc
   (function
    (lambda (buffer)
      (cond ((string-match-p "^\\*elfeed" (buffer-name buffer))
             (kill-buffer buffer)))))
   (buffer-list)))

;;;###autoload
(defun durand-elfeed-play-video (process)
  "Play the video file downloaded by PROCESS."
  (let* ((command (process-command process))
         (link (car (last command)))
         (dir "/Users/durand/Desktop/Centre/Vidéos/")
         (id (process-get process 'id))
         (title (process-get process 'title))
         (ytp (string-match
               (rx-to-string '(seq bos "yt:"
                               (one-or-more (not ":"))
                               ":"
                               (group (one-or-more anything))
                               eos)
                             t)
               id))
         (video-file
          (cond
           (ytp (concat title "-" (match-string 1 id)))
           ("^/xxx")))
         buffer)
    (cond
     ((let* ((files (directory-files dir t video-file t)))
        (and files (= (length files) 1)
             (setq video-file (car files))))
      (setq buffer (generate-new-buffer "*mpv*"))
      (start-process "elfeed-mpv"
                     buffer
                     "mpv"
                     video-file)
      (display-buffer buffer
                      '(durand-display-in-one-window)))
     ((message
       "The file %S does not exist, and id = %s, title = %s"
       video-file id title)))))

;;;###autoload
(defun durand-eshell-download-sentinel (process status)
  "Inform the user when the download process is terminated.
PROCESS should have the name \"elfeed download video\".

This informs the user when STATUS is the string \"finished\\n\"."
  (cond ((string-equal (process-name process) "elfeed download video"))
        ((error "The download sentinel is applied to a wrong process: %S"
                process)))
  (cond ((string-equal status "finished\n")
         (message "Downloading videos has finished."))))

;;;###autoload
(defun durand-eshell-download-play-sentinel (process status)
  "A combination of `durand-eshell-download-sentinel' and \
`durand-elfeed-play-video'."
  (durand-eshell-download-sentinel process status)
  (cond
   ((and (string= status "finished\n")
         (y-or-n-p "Play video?"))
    (durand-elfeed-play-video process))))
                                     
;;;###autoload
(defun durand-eshell-download-filter (process output)
  "Display OUTPUT in the buffer associated with PROCESS.
By display it is meant that before inserting the ouput the buffer
will first be erased."
  (let ((buffer (process-buffer process)))
    (cond
     (buffer
      (with-current-buffer buffer
        (erase-buffer)
        (insert
         (replace-regexp-in-string "
\\|" "" output)))))))

;;;###autoload
(defun durand-elfeed-download-video ()
  "Download the video in the feed."
  (interactive)
  (let ((default-directory "/Users/durand/Desktop/Centre/Vidéos/")
        (entry (cond
                ((derived-mode-p 'elfeed-show-mode)
                 elfeed-show-entry)
                ((derived-mode-p 'elfeed-search-mode)
                 (elfeed-search-selected :ignore-region))))
        link proc)
    (cond ((derived-mode-p 'elfeed-search-mode)
           (setq link (elfeed-entry-link (elfeed-search-selected :ignore-region))))
          ((derived-mode-p 'elfeed-show-mode)
           (setq link (elfeed-entry-link elfeed-show-entry)))
          ((user-error "Not in an elfeed buffer")))
    (message "Downloading starts.")
    (setq proc
          (make-process
           :name "elfeed download video"
           :buffer "*elfeed download*"
           :sentinel #'durand-eshell-download-sentinel
           :filter #'durand-eshell-download-filter
           :command (list
                     "youtube-dl" "-f" "bestvideo[height<=720]+bestaudio/best[height<=720]/best"
                     link)))
    (process-put proc 'id (cdr (elfeed-entry-id entry)))
    (process-put proc 'title (elfeed-entry-title entry))
    proc))

;;;###autoload
(defun durand-elfeed-download-play-video ()
  "Download and confirm to play the video."
  (interactive)
  (let ((proc (durand-elfeed-download-video)))
    (set-process-sentinel proc #'durand-eshell-download-play-sentinel)))

(define-key elfeed-search-mode-map (vector ?w) #'durand-elfeed-download-video)
(define-key elfeed-show-mode-map (vector ?w) #'durand-elfeed-download-video)

(define-key elfeed-search-mode-map (vector ?W) #'durand-elfeed-download-play-video)
(define-key elfeed-show-mode-map (vector ?W) #'durand-elfeed-download-play-video)


(provide 'elfeed-conf)
;;; elfeed-conf.el ends here