forked from skeeto/youtube-dl-emacs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-dl-view.el
384 lines (346 loc) · 14.2 KB
/
youtube-dl-view.el
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
;;; youtube-dl-view.el --- shows video clip info in a separate buffer -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Igor B. Poretsky <[email protected]>
;; This file is not part of GNU Emacs.
;; 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 2, 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, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This module shows video clip info in a separate buffer. In this
;; buffer you can navigate and activate highlighted references.
;; The `youtube-dl-view' command retrieves video description
;; from specified URL or from an item under point in the download
;; list and shows it.
;;; Code:
(require 'cl-lib)
(require 'button)
(require 'custom)
(require 'youtube-dl)
(declare-function w3m-bookmark-add "w3m-bookmark" (url &optional title))
;;;###autoload
(defgroup youtube-dl-view ()
"YouTube video descriptions view settings."
:group 'youtube-dl)
(defcustom youtube-dl-view-fill-column 0
"Column beyond which automatic line-wrapping should happen
in view buffer. Positive number means just that column. If it is 0,
full window width is used. Negative number means respective
subtraction from the window width. If it is nil, no filling
will be applied."
:group 'youtube-dl-view
:type '(choice (const :tag "No filling" nil) integer))
(defgroup youtube-dl-view-faces ()
"Video description display faces."
:group 'youtube-dl-view)
(defface youtube-dl-view-title
'((t :inherit font-lock-comment-face))
"Face for highlighting item title."
:group 'youtube-dl-view-faces)
(defface youtube-dl-view-header
'((t :inherit font-lock-comment-delimiter-face))
"Face for highlighting header titles."
:group 'youtube-dl-view-faces)
(defface youtube-dl-view-header-value
'((t :inherit font-lock-comment-face))
"Face for highlighting header values."
:group 'youtube-dl-view-faces)
(define-button-type 'youtube-dl-view-play-start-time 'action
(lambda (button)
(youtube-dl-view-play-current-url (button-get button 'start-time)))
:supertype 'button)
(define-button-type 'youtube-dl-view-play 'action
(lambda (_button)
(youtube-dl-view-play-current-url))
:supertype 'button)
(define-button-type 'youtube-dl-view-download 'action
(lambda (button)
(cl-declare (special youtube-dl-view-item))
(youtube-dl-submit (list youtube-dl-view-item)
:immediate (youtube-dl-request-immediate)
:extract-audio (button-get button 'audio-only)
:display t))
:supertype 'button)
(defvar youtube-dl-view-history nil
"Viewing history.")
(defun youtube-dl-view--current-url ()
"Return currently viewed item url."
(cl-declare (special youtube-dl-view-item))
(plist-get youtube-dl-view-item :url))
(defun youtube-dl-view-play-current-url (&optional start-time)
"Play current url from specified start time."
(interactive)
(unless (eq major-mode 'youtube-dl-view-mode)
(error "Not in youtube-dl-view buffer."))
(youtube-dl-play (youtube-dl-view--current-url) start-time))
(defun youtube-dl-view-yank-current-url ()
"Yank current clip URL into kill-ring."
(interactive)
(unless (eq major-mode 'youtube-dl-view-mode)
(error "Not in youtube-dl-view buffer."))
(let ((url (youtube-dl-view--current-url)))
(kill-new url)
(message "Yanked %s" url)))
(defun youtube-dl-view--stored-p ()
"Returns non-nil if currently viewed item is stored at the top of history."
(and youtube-dl-view-history
(string-equal (plist-get (car youtube-dl-view-history) :url)
(youtube-dl-view--current-url))))
(define-button-type 'youtube-dl-view-link 'action
(lambda (_button)
(unless (youtube-dl-view--stored-p)
(push youtube-dl-view-item youtube-dl-view-history))
(let ((url (thing-at-point 'url t)))
(if (zerop (call-process youtube-dl-program nil nil nil
"--ignore-config"
"--simulate"
"--flat-playlist"
"--"
url))
(youtube-dl-view url)
(browse-url url))))
:supertype 'button)
(define-button-type 'youtube-dl-view-mail 'action
(lambda (_button)
(compose-mail (thing-at-point 'email t)))
:supertype 'button)
(define-button-type 'youtube-dl-view-back 'action
(lambda (_button)
(when (youtube-dl-view--stored-p)
(pop youtube-dl-view-history))
(unless youtube-dl-view-history
(error "No more history."))
(youtube-dl-view (pop youtube-dl-view-history)))
:supertype 'button)
(defun youtube-dl-view-quit ()
"Clear history and close window. Also stops playback if any."
(interactive)
(unless (eq major-mode 'youtube-dl-view-mode)
(error "Not in youtube-dl-view buffer."))
(when (featurep 'youtube-dl-play)
(youtube-dl-play-stop))
(setq youtube-dl-view-history nil)
(quit-window))
(defun youtube-dl-view-add-w3m-bookmark ()
"Add currently viewed clip to the w3m bookmarks."
(interactive)
(unless (eq major-mode 'youtube-dl-view-mode)
(error "Not in youtube-dl-view buffer."))
(require 'w3m-bookmark)
(w3m-bookmark-add (youtube-dl-view--current-url) header-line-format)
(message "Bookmark added"))
(defvar youtube-dl-view-mode-map
(let ((map (make-sparse-keymap)))
(prog1 map
(set-keymap-parent map button-buffer-map)
(define-key map "y" #'youtube-dl-view-yank-current-url)
(define-key map "a" #'youtube-dl-view-add-w3m-bookmark)
(define-key map " " #'youtube-dl-view-play-current-url)
(define-key map "k" #'youtube-dl-play-stop)
(define-key map "q" #'youtube-dl-view-quit)))
"Keymap for `youtube-dl-view-mode'")
(define-derived-mode youtube-dl-view-mode special-mode "youtube-dl-view"
"Major mode for viewing youtube-dl items info."
:group 'youtube-dl-view
(use-local-map youtube-dl-view-mode-map))
(defconst youtube-dl-view-time-spec
"\\(?:[0-9]+:\\)?[0-5]?[0-9]:[0-5][0-9]\\(?:\\.[0-9]+\\)?"
"Timespec matching regexp.")
(cl-defun youtube-dl-view--show-description
(text url submitted-p &key id title filesize duration timestamp)
"Show a description represented by given text.
The second argument specifies source URL for reference.
The third argument indicates whether this URL is already submitted
for download."
(cl-declare (special youtube-dl-view-item))
(with-current-buffer (get-buffer-create " *youtube-dl view*")
(youtube-dl-view-mode)
(let ((window (get-buffer-window))
(inhibit-read-only t)
(playable-p (youtube-dl-playable-p url)))
(erase-buffer)
(when title
(setq header-line-format
(propertize title 'face 'youtube-dl-view-title)))
(when filesize
(insert (propertize "FileSize" 'face 'youtube-dl-view-header)
" "
(propertize (format "%d" filesize) 'face 'youtube-dl-view-header-value)
"\n"))
(when duration
(insert (propertize "Duration" 'face 'youtube-dl-view-header)
" "
(propertize (format-time-string "%T" (seconds-to-time duration) t)
'face 'youtube-dl-view-header-value)
"\n"))
(when timestamp
(insert (propertize "Date" 'face 'youtube-dl-view-header)
" "
(propertize (format-time-string "%c" (seconds-to-time timestamp))
'face 'youtube-dl-view-header-value)
"\n"))
(unless (bobp)
(insert "\n"))
(when playable-p
(insert-button "Play"
:type 'youtube-dl-view-play))
(unless submitted-p
(unless (bolp)
(insert " "))
(insert-button "Download"
:type 'youtube-dl-view-download)
(when playable-p
(insert " ")
(insert-button "Download audio" 'audio-only t
:type 'youtube-dl-view-download)))
(unless (bolp)
(insert "\n\n"))
(let ((start (point)))
(insert (or text ""))
(unless (or (bobp) (= (char-before) ?\n))
(insert "\n"))
(when youtube-dl-view-history
(insert "\n")
(insert-button "Back"
:type 'youtube-dl-view-back)
(insert "\n"))
(goto-char start)
(while
(re-search-forward
(concat
"\\( +\\)\\[?"
youtube-dl-view-time-spec)
nil t)
(replace-match "\n" nil nil nil 1))
(when (integerp youtube-dl-view-fill-column)
(let ((fill-column
(if (> youtube-dl-view-fill-column 0)
youtube-dl-view-fill-column
(- (window-body-width) youtube-dl-view-fill-column))))
(fill-individual-paragraphs start (point-max) nil
(concat
"\\[?"
youtube-dl-view-time-spec
"]? \\|-*[0-9]+\\.? \\|.+[ \t\n][^ \t\n]+\\(?:@\\|://\\)"))))
(goto-char start))
(while
(re-search-forward
(concat
"\\([a-zA-Z0-9]@[a-zA-Z0-9]\\)\\|\\(https?://[a-zA-Z0-9]+\\.[a-zA-Z0-9]\\)\\|^\\(?:\\(-*\\([0-9]+\\.?\\)\\(?: \\| .* \\)(?\\("
youtube-dl-view-time-spec
"\\))?\\)\\|\\[?\\("
youtube-dl-view-time-spec
"\\)]?\\)")
nil t)
(cond
((match-string 1)
(let ((link (bounds-of-thing-at-point 'email)))
(when link
(make-button (car link) (cdr link)
:type 'youtube-dl-view-mail))))
((match-string 2)
(let ((link (bounds-of-thing-at-point 'url)))
(when link
(make-button (car link) (cdr link)
:type 'youtube-dl-view-link))))
((match-string 3)
(make-button (match-beginning 4) (match-end 4)
'start-time (buffer-substring-no-properties (match-beginning 5) (match-end 5))
:type 'youtube-dl-view-play-start-time))
(t (make-button (match-beginning 0) (match-end 0)
'start-time (buffer-substring-no-properties (match-beginning 6) (match-end 6))
:type 'youtube-dl-view-play-start-time))))
(set (make-local-variable 'youtube-dl-view-item)
(list
:index 1
:id id
:url url
:title title
:description text
:duration duration
:filesize filesize))
(goto-char (point-min))
(when window
(set-window-point window (point-min)))
(let ((split-width-threshold nil))
(pop-to-buffer (current-buffer))))))
;;;###autoload
(defun youtube-dl-view (thing)
"Retrieves and shows info from specified URL or, being invoked
in the download listing, for an item under point. If specified URL
points to a playlist and it has more than one item, this playlist
is submitted for download as paused and shown in the listing.
The argument can be a plist of clip data as well."
(interactive (youtube-dl-thing))
(let* ((submitted-p (youtube-dl-item-p thing))
(playlist
(cond
((stringp thing)
(youtube-dl-playlist-list thing))
((and submitted-p
(or (not (youtube-dl-item-description thing))
(not (youtube-dl-item-title thing))))
(youtube-dl-playlist-list (youtube-dl-item-url thing)))
(t nil)))
(item
(if playlist
(car playlist)
thing))
(id
(if (youtube-dl-item-p item)
(youtube-dl-item-id item)
(plist-get item :id)))
(title
(if (youtube-dl-item-p item)
(youtube-dl-item-title item)
(or (plist-get item :title) id)))
(filesize
(if (youtube-dl-item-p item)
(youtube-dl-item-filesize item)
(plist-get item :filesize)))
(duration
(if (youtube-dl-item-p item)
(youtube-dl-item-duration item)
(plist-get item :duration)))
(timestamp
(if (youtube-dl-item-p item)
(youtube-dl-item-timestamp item)
(plist-get item :timestamp)))
(text
(or (if (youtube-dl-item-p item)
(youtube-dl-item-description item)
(plist-get item :description))
""))
(url
(if (youtube-dl-item-p item)
(youtube-dl-item-url item)
(or (plist-get item :url) thing))))
(if (> (length playlist) 1)
(youtube-dl-submit playlist
:display t)
(when (and submitted-p playlist)
(youtube-dl-item-title-set thing title)
(let ((name (youtube-dl-item-dest-name thing)))
(when (string-match (format "^\\(?:[0-9]+-\\)?\\(%s\\)" id) name)
(youtube-dl-item-dest-name-set
thing (replace-match title nil nil name 1))
(youtube-dl-redisplay)))
(youtube-dl-item-description-set thing text)
(youtube-dl-item-filesize-set thing filesize)
(youtube-dl-item-duration-set thing duration)
(youtube-dl-item-timestamp-set thing timestamp))
(youtube-dl-view--show-description text url submitted-p
:id id
:title title
:filesize filesize
:duration duration
:timestamp timestamp))))
(provide 'youtube-dl-view)
;;; youtube-dl-view.el ends here