summaryrefslogtreecommitdiff
path: root/youtube-stream.el
blob: 2c5cc649e9906252455ac75967d5cf2a51d8eb3f (about) (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
;;; youtube-stream.el --- Open YouTube stream video and chat

;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>

;; This file is part of Emacs-youtube-stream.

;; Emacs-YouTube-stream 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.
;;
;; Emacs-YouTube-stream 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 Emacs-YouTube-stream.
;; If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file provides `youtube-stream-open', the function to open
;; YouTube stream video and live chat.

;;; Code:

(defgroup youtube-stream nil
  "Settings for `youtube-stream'."
  :prefix "youtube-stream-"
  :group  'youtube-stream)

(defcustom youtube-stream-url-regexp
  (rx "http" (zero-or-one "s") "://"
      (zero-or-one "www.") "youtube.com")
  "Regexp matching YouTube URL."
  :type 'regexp
  :group 'youtube-stream)

(defcustom youtube-stream-video-id-url-regexp
  (concat youtube-stream-url-regexp
          (rx "/watch?v=" (group (one-or-more alphanumeric))))
  "Regexp matching Youtube video ID URL."
  :type 'regexp
  :group 'youtube-stream)

(defcustom youtube-stream-open-chat-function #'browse-url
  "Function to open YouTube live chat."
  :type 'function
  :group 'youtube-stream)

(defcustom youtube-stream-open-video-function #'browse-url
  "Function to open YouTube stream."
  :type 'string
  :group 'youtube-stream)

(defcustom youtube-stream-open-video t
  "If `t' open YouTube video in `youtube-stream-stream-player'."
  :type 'boolean
  :group 'youtube-stream)

(defcustom youtube-stream-open-chat t
  "If `t' open YouTube live chat in `youtube-stream-stream-browser'."
  :type 'boolean
  :group 'youtube-stream)

(defcustom youtube-stream-live-chat-url-template
  "https://www.youtube.com/live_chat?v=%s&is_popout=1"
  "URL template used in `youtube-stream-live-chat-url'."
  :type 'string
  :group 'youtube-stream)

(defcustom youtube-stream-video-url-template
  "https://www.youtube.com/watch?v=%s"
  "URL template used id `youtube-stream-video-url'")

(defun youtube-stream-get-id (url)
  "Return video id from YouTube URL."
  (string-match youtube-stream-video-id-url-regexp url)
  (match-string 1 url))

(defun youtube-stream-live-chat-url (id)
  "Return a YouTube live chat URL from ID."
  (format youtube-stream-live-chat-url-template id))

(defun youtube-stream-video-url (id)
  "Return a YouTube video URL from ID."
  (format youtube-stream-video-url-template id))

(defun youtube-stream-quote-url (url)
  "Return a quoted URL."
  (format "\"%s\"" url))

(defun youtube-stream-open (url)
  "Open YouTube live chat from URL

Use YouTube video URL present from a GUI clipboard if available."
  (interactive
   (let ((clipboard (x-get-clipboard)))
     (list
      (if (string-match-p youtube-stream-video-id-url-regexp
                          clipboard)
          clipboard
        (read-string "YouTube URL: ")))))
  (let ((id (youtube-stream-get-id url)))
    (if youtube-stream-open-chat
        (funcall youtube-stream-open-chat-function
                 (youtube-stream-live-chat-url id)))
    (if youtube-stream-open-video
        (funcall youtube-stream-open-video-function
                 (youtube-stream-video-url id)))))

(provide 'youtube-stream)

;;; youtube-stream.el ends here