1 ;;; iplayer.el --- Browse and download BBC TV/radio shows
3 ;; Copyright (C) 2012-2013 Christophe Rhodes
5 ;; Author: Christophe Rhodes <csr21@cantab.net>
7 ;; Keywords: multimedia
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; Requires and uses the 'get-iplayer' script to provide a
25 ;; convenient interface to BBC iPlayer.
30 "Browse and download BBC TV/radio shows."
34 (defcustom iplayer-download-directory "~/iPlayer/"
35 "Directory into which shows will be downloaded."
39 (defvar iplayer-updating-cache-process nil)
40 (defvar iplayer-updating-cache-sentinel-info nil)
41 (defvar iplayer-updating-cache-sentinel-executing nil)
43 (defun iplayer-updating-cache-sentinel (process event)
44 ;; FIXME: assumes that all went well
45 (let* ((iplayer-updating-cache-sentinel-executing t)
46 (info (reverse iplayer-updating-cache-sentinel-info)))
47 (setq iplayer-updating-cache-process nil
48 iplayer-updating-cache-sentinel-info nil)
50 (let ((iplayer-command-frame (nth 0 info))
51 (iplayer-command-window (nth 1 info))
52 (iplayer-command-buffer (nth 2 info))
54 (function (nth 4 info)))
55 (when (and (frame-live-p iplayer-command-frame)
56 (window-live-p iplayer-command-window)
57 (buffer-live-p iplayer-command-buffer))
58 (let ((old-frame (selected-frame))
59 (old-window (selected-window))
60 (old-buffer (current-buffer)))
62 ((version< emacs-version "24")
63 (let ((pre-command-hook
65 (select-frame iplayer-command-frame)
66 (select-window iplayer-command-window)
67 (set-buffer iplayer-command-buffer)
68 (setq pre-command-hook nil))))
69 ;; KLUDGE: execute-kbd-macro executes a normal
70 ;; command-loop, whose first action is to select the
71 ;; current frame and window, which is why we contort
72 ;; things to select the frame/window/buffer we actually
73 ;; want in pre-command-hook. I'm actually surprised
74 ;; that it works, but mine is not too much to reason
75 ;; why; lots of other ways to try to achieve this didn't
77 (execute-kbd-macro keys)
78 ;; KLUDGE: and then we restore old state
79 (select-window old-window)
80 (select-frame old-frame)
81 (set-buffer old-buffer)))
83 ;; KLUDGE: we store the function name, which is fine,
84 ;; but some of our functions need to know which
85 ;; keystrokes were used to invoke them, so we need to
86 ;; pass those along, so we need to make sure that all
87 ;; iplayer-functions accept an optional argument, argh
89 (with-selected-frame iplayer-command-frame
90 (with-current-buffer iplayer-command-buffer
91 (with-selected-window iplayer-command-window
92 (funcall function keys)))))))))
93 (message "Done updating iPlayer cache"))))
95 (defmacro define-iplayer-command (name arglist &rest body)
96 (let (docstring interactive)
97 (when (stringp (car body))
98 (setq docstring (car body) body (cdr body)))
99 (when (and (consp (car body)) (eql (car (car body)) 'interactive))
100 (setq interactive (car body) body (cdr body)))
101 `(defun ,name ,arglist
102 ,@(when docstring (list docstring))
103 ,@(when interactive (list interactive))
104 (unless iplayer-updating-cache-process
105 (setq iplayer-updating-cache-process
106 (start-process "updating-iplayer" " *updating-iplayer*"
107 "get-iplayer" "--type" "radio,tv" "-q"))
108 (set-process-sentinel iplayer-updating-cache-process
109 'iplayer-updating-cache-sentinel)
110 (message "Updating iPlayer cache"))
111 (if iplayer-updating-cache-sentinel-executing
113 (push (list (selected-frame) (selected-window) (current-buffer) (this-command-keys-vector) ',name)
114 iplayer-updating-cache-sentinel-info)))))
116 (defun get-iplayer-tree (&rest args)
118 (apply #'call-process "get-iplayer" nil t nil "--nocopyright" "--type" "radio,tv" "--tree" "--terse" args)
119 (goto-char (point-min))
120 (let (result program episodes)
121 (while (< (point) (point-max))
124 (when (and program episodes)
125 (push (cons program (reverse episodes)) result))
126 (setf program (buffer-substring (point) (progn (end-of-line) (point))))
127 (when (string-match "^\\(tv\\|radio\\), " program)
128 (setq program (substring program (match-end 0))))
130 (unless (= (point) (point-max))
132 ((looking-at "^ \\([0-9]+\\):\\s-\\(.*\\)$")
134 (cons (buffer-substring (match-beginning 1) (match-end 1))
135 (buffer-substring (match-beginning 2) (match-end 2)))))
136 (when (string-match "^\\(tv\\|radio\\), " (cdr episode))
137 (rplacd episode (substring (cdr episode) (match-end 0))))
138 (push episode episodes))
143 (defun display-iplayer-tree (tree)
144 (with-current-buffer (get-buffer-create "*iplayer*")
145 (delete-region (point-min) (point-max))
149 (let ((program (car entry))
150 (episodes (cdr entry)))
151 (insert (propertize (format "* %s\n" program) 'face 'outline-1))
152 (dolist (episode episodes)
153 (insert (propertize (format "** %s\n" (cdr episode))
154 'face 'outline-2 'iplayer-id (car episode))))))
156 (goto-char (point-min)))
157 (switch-to-buffer (get-buffer-create "*iplayer*")))
159 (defvar iplayer-presets
165 ("!" . "BBC Radio 1")
166 ("\"" . "BBC Radio 2")
167 ("£" . "BBC Radio 3")
168 ("$" . "BBC Radio 4")
169 ("%" . "BBC Radio 5 live")
170 ("^" . "BBC 6 Music")
172 ("*" . "BBC Radio 4 Extra"))
173 "Alist mapping keys to iPlayer channels.
175 Used in the `iplayer-preset' command.")
177 (defun iplayer-frob-presets (presets)
179 ((version< emacs-version "24")
180 (mapcar (lambda (x) (cons (read-kbd-macro (car x)) (cdr x))) presets))
183 (define-iplayer-command iplayer-preset (&optional keys)
184 "Switch display to a preset channel.
186 The presets are defined in the variable `iplayer-presets'."
188 (let ((keys (or (and keys (concat keys)) (this-command-keys)))
189 (presets (iplayer-frob-presets iplayer-presets)))
192 (let ((channel (cdr (assoc keys presets))))
195 (setq mode-line-process (format "[%s]" channel))
196 (iplayer-channel (format "^%s$" channel)))
197 (error "no preset for key %s" keys)))))))
199 (defun iplayer-channel (channel)
200 (display-iplayer-tree (get-iplayer-tree "--channel" channel)))
202 (defun iplayer-download ()
204 (let ((id (get-text-property (point) 'iplayer-id)))
206 (let ((default-directory iplayer-download-directory))
207 ;; should probably use a process filter instead to give us a
209 (message "downloading id %s" id)
210 (start-process "get-iplayer" " *get-iplayer*" "get-iplayer" "--modes=best" "--get" (format "%s" id)))
211 (message "no id at point"))))
213 (defun iplayer-previous ()
216 (outline-previous-heading)
217 (while (and (= (funcall outline-level) 1) (not (bobp)))
218 (outline-previous-heading)))
222 (outline-up-heading 1 t)
225 (defun iplayer-next ()
228 (outline-next-heading)
229 (while (and (= (funcall outline-level) 1) (not (eobp)))
230 (outline-next-heading)))
233 (outline-up-heading 1 t)
236 (defconst iplayer-mode-map
237 (let ((map (make-sparse-keymap)))
238 (define-key map (kbd "0") 'iplayer)
239 (let ((presets "123456789!\"£$%^&*()"))
240 (dotimes (i (length presets))
241 (define-key map (read-kbd-macro (substring presets i (1+ i)))
243 (define-key map (kbd "RET") 'iplayer-download)
244 (define-key map (kbd "j") 'iplayer-next)
245 (define-key map (kbd "k") 'iplayer-previous)
249 (defun iplayer-mode ()
250 "A major mode for the BBC's iPlayer.
251 \\{iplayer-mode-map}"
253 (use-local-map iplayer-mode-map)
254 (setq major-mode 'iplayer-mode mode-name "iPlayer"))
256 (define-iplayer-command iplayer (&optional keys)
257 "Start the emacs iPlayer interface."
259 (setq mode-line-process nil)
260 (display-iplayer-tree (get-iplayer-tree)))
263 (autoload 'iplayer "iplayer" "Start the emacs iPlayer interface." t)
266 ;;; iplayer.el ends here