aboutsummaryrefslogtreecommitdiff
path: root/lisp/emms-librefm-stream.el
blob: 2b232ba5f37ecabae854c3675e3050f4135ad42a (plain) (blame)
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
;;; emms-librefm-stream.el --- Libre.FM streaming

;; Copyright (C) 2014  Free Software Foundation, Inc.

;; Author: Yoni Rabkin <yrk@gnu.org>

;; Keywords: emms, libre.fm, GNU FM

;; EMMS 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, or (at your option)
;; any later version.
;;
;; EMMS 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 EMMS; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301, USA.


;;; Code:

(require 'emms-librefm-scrobbler)

(defvar emms-librefm-stream-host-url
  "alpha.libre.fm"
  "URL for the streaming host")

(defvar emms-librefm-stream-debug
  ""
  "Temporary debug information.")


;;; ------------------------------------------------------------------
;;; radio handshake
;;; ------------------------------------------------------------------

;; http://alpha.libre.fm/radio/handshake.php?version=1.3.0.58&platform=linux&username=USERNAME&passwordmd5=PASSWORDMD5&language=en

(defun emms-librefm-stream-tune-handshake-string ()
  (when (not emms-librefm-scrobbler-username)
    (error "null username"))
  (when (not emms-librefm-scrobbler-password)
    (error "null password"))
  (let ((url (concat "http://"
		     emms-librefm-stream-host-url
		     "/radio/handshake.php?"
		     "version=1.3.0.58" "&"
		     "platform=linux" "&"
		     "username=" (url-encode-url emms-librefm-scrobbler-username) "&"
		     "passwordmd5=" (md5 emms-librefm-scrobbler-password) "&"
		     "language=en")))
    url))

(defun emms-librefm-stream-tune-handshake-call ()
  ""
  (let ((url-request-method "POST"))
    (let ((response
	   (url-retrieve-synchronously
	    (emms-librefm-stream-tune-handshake-string))))
      (setq emms-librefm-stream-debug
	    (with-current-buffer response
	      (buffer-substring-no-properties (point-min)
					      (point-max))))
      response)))


;;; ------------------------------------------------------------------
;;; tuning
;;; ------------------------------------------------------------------

(defun emms-librefm-stream-tune-string (session-id station)
  ""
  (when (not session-id)
    (error "null session id"))
  (when (not station)
    (error "null station"))
  (let ((url (concat "http://"
		     emms-librefm-stream-host-url
		     "/radio/adjust.php?"
		     "session=" session-id "&"
		     "url=" (url-encode-url station))))
    url))

(defun emms-librefm-stream-tune-call (session-id station)
  ""
  (let ((url-request-method "POST"))
    (let ((response
	   (url-retrieve-synchronously
	    (emms-librefm-stream-tune-string
	     session-id station))))
      (setq emms-librefm-stream-debug
	    (with-current-buffer response
	      (buffer-substring-no-properties (point-min)
					      (point-max))))
      response)))

(defun emms-librefm-stream-handle-tune-response (resbuf)
  "Handle the tune server response."
  (when (not (bufferp resbuf))
    (error "response not a buffer"))
  (with-current-buffer resbuf
    (goto-char (point-min))
    (when (not (re-search-forward "^.*200 OK$" (point-at-eol) t))
      (error "bad HTTP server response"))
    ;; go to the start of the FM response
    (when (not (re-search-forward "\n\n" (point-max) t))
      (error "bad FM server response"))
    (let ((status (buffer-substring (point-at-bol)
				    (point-at-eol))))
      (cond ((string= status "OK")         'ok)
	    ((string= status "BADSESSION") 'badsession)
	    (t (error "unhandled response status: [%s]" status))))))


(provide 'emms-librefm-stream)

;;; emms-librefm-stream.el ends here