From eb4229b372252d0e709760edcfc1ffe759367b70 Mon Sep 17 00:00:00 2001 From: William Xu Date: Tue, 12 Feb 2008 06:48:00 +0000 Subject: - emms-url.el: (emms-url-quote, emms-url-quote-plus), New functions renamed from emms-lyrics-url-quote, emms-lyrics-url-quote-plus defined in emms-lyrics.el. - emms-url.el: (emms-escape-url, emms-url-specials), deleted, since they can only escape ASCII characters, emms-url-quote can handle all characters. - Update affected files accordingly. darcs-hash:20080212064822-cfa61-759d28b271024ab1a17bd23a2444c076d04e55e7.gz --- emms-lastfm.el | 78 ++++++++++++++++++++++++++++++------------------------ emms-lyrics.el | 34 +++--------------------- emms-player-mpd.el | 2 +- emms-url.el | 45 +++++++++++++++++-------------- 4 files changed, 73 insertions(+), 86 deletions(-) diff --git a/emms-lastfm.el b/emms-lastfm.el index 2f86a8c..7735615 100644 --- a/emms-lastfm.el +++ b/emms-lastfm.el @@ -204,23 +204,25 @@ These will be displayed on the user's last.fm page." (musicbrainz-id "") (track-length (number-to-string (emms-track-get emms-lastfm-current-track - 'info-playing-time)))) - ;; wait up to 5 seconds to submit np infos in order to finish handshaking. - (dotimes (i 5) - (when (not (and emms-lastfm-session-id - emms-lastfm-now-playing-url)) - (sit-for 1))) - (when (and emms-lastfm-session-id - emms-lastfm-now-playing-url) - (emms-lastfm-http-POST emms-lastfm-now-playing-url - (concat "&s=" emms-lastfm-session-id - "&a[0]=" (emms-escape-url artist) - "&t[0]=" (emms-escape-url title) - "&b[0]=" (emms-escape-url album) - "&l[0]=" track-length - "&n[0]=" track-number - "&m[0]=" musicbrainz-id) - 'emms-lastfm-submit-now-playing-sentinel)))) + 'info-playing-time))) + (url-http-attempt-keepalives nil) + (url-show-status emms-lastfm-submission-verbose-p) + (url-request-method "POST") + (url-request-extra-headers + '(("Content-type" . + "application/x-www-form-urlencoded; charset=utf-8"))) + (url-request-data + (encode-coding-string + (concat "&s=" emms-lastfm-session-id + "&a[0]=" (emms-escape-url artist) + "&t[0]=" (emms-escape-url title) + "&b[0]=" (emms-escape-url album) + "&l[0]=" track-length + "&n[0]=" track-number + "&m[0]=" musicbrainz-id) + 'utf-8))) + (url-retrieve emms-lastfm-now-playing-url + 'emms-lastfm-submit-now-playing-sentinel))) (defun emms-lastfm-submit-now-playing-sentinel (&rest args) "Parses the server reponse and inform the user if all worked @@ -340,7 +342,7 @@ handshake." "?hs=true&p=1.2" "&c=" emms-lastfm-client-id "&v=" (number-to-string emms-lastfm-client-version) - "&u=" (emms-escape-url emms-lastfm-username) + "&u=" (emms-url-quote emms-lastfm-username) "&t=" timestamp "&a=" (md5 (concat (md5 emms-lastfm-password) timestamp))) 'emms-lastfm-handshake-sentinel))) @@ -377,20 +379,28 @@ last.fm." (musicbrainz-id "") (track-length (number-to-string (emms-track-get emms-lastfm-current-track - 'info-playing-time)))) - (emms-lastfm-http-POST - emms-lastfm-submit-url - (concat "&s=" emms-lastfm-session-id - "&a[0]=" (emms-escape-url artist) - "&t[0]=" (emms-escape-url title) - "&i[0]=" emms-lastfm-current-track-starting-time-string - "&o[0]=P" ;; TODO: Maybe support others. See the API. - "&r[0]=" ;; The rating. Empty if not applicable (for P it's not) - "&l[0]=" track-length - "&b[0]=" (emms-escape-url album) - "&n[0]=" track-number - "&m[0]=" musicbrainz-id) - 'emms-lastfm-submission-sentinel))) + 'info-playing-time))) + (url-http-attempt-keepalives nil) + (url-show-status emms-lastfm-submission-verbose-p) + (url-request-method "POST") + (url-request-extra-headers + '(("Content-type" . + "application/x-www-form-urlencoded; charset=utf-8"))) + (url-request-data + (encode-coding-string + (concat "&s=" emms-lastfm-session-id + "&a[0]=" (emms-escape-url artist) + "&t[0]=" (emms-escape-url title) + "&i[0]=" emms-lastfm-current-track-starting-time-string + "&o[0]=P" ;; TODO: Maybe support others. See the API. + "&r[0]=" ;; The rating. Empty if not applicable (for P it's not) + "&l[0]=" track-length + "&b[0]=" (emms-escape-url album) + "&n[0]=" track-number + "&m[0]=" musicbrainz-id) + 'utf-8))) + (url-retrieve emms-lastfm-submit-url + 'emms-lastfm-submission-sentinel))) (defun emms-lastfm-submission-sentinel (&rest args) "Parses the server reponse and inform the user if all worked @@ -437,7 +447,7 @@ well or if an error occured." "handshake.php?version=" (number-to-string emms-lastfm-client-version) "&platform=" emms-lastfm-client-id - "&username=" (emms-escape-url emms-lastfm-username) + "&username=" (emms-url-quote emms-lastfm-username) "&passwordmd5=" (md5 emms-lastfm-password) "&debug=" (number-to-string 9))) @@ -469,7 +479,7 @@ Calls FN when done with RADIO-URL as its only argument." (concat emms-lastfm-radio-base-url "adjust.php?" "session=" emms-lastfm-radio-session - "&url=" (emms-escape-url lastfm-url) + "&url=" (emms-url-quote lastfm-url) "&debug=" (number-to-string 0)) 'emms-lastfm-radio-sentinel)) (message "EMMS: Cannot play Last.fm stream"))) diff --git a/emms-lyrics.el b/emms-lyrics.el index a9ed2fd..f79a550 100644 --- a/emms-lyrics.el +++ b/emms-lyrics.el @@ -65,6 +65,7 @@ (require 'emms-player-simple) (require 'emms-source-file) (require 'time-date) +(require 'emms-url) ;;; User Customization @@ -218,12 +219,12 @@ If we can't find it from local disk, then search it from internet." ;; systems, we'd better fall back on filename. (setq url (format "http://mp3.baidu.com/m?f=ms&rn=10&tn=baidump3lyric&ct=150994944&word=%s&lm=-1" - (emms-lyrics-url-quote-plus + (emms-url-quote-plus (encode-coding-string filename 'gb2312))))) (t ; english lyrics (setq url (format "http://search.lyrics.astraweb.com/?word=%s" ;;"http://www.lyrics007.com/cgi-bin/s.cgi?q=" - (emms-lyrics-url-quote-plus title))))) + (emms-url-quote-plus title))))) (browse-url url) (message "lyric file does not exist, search it from internet...done"))))) @@ -476,35 +477,6 @@ NEXT-LYRIC." (setq time (+ time emms-lyrics-scroll-timer-interval)) (setq pos (1+ pos)))))) - -;;; Utilities - -(defun emms-lyrics-url-quote (s &optional safe) - "Replace special characters in S using the `%xx' escape. -Characters in [a-zA-Z_.-/] and SAFE(default is \"\")) will never be -quoted. -e.g., - (url-quote \"abc def\") => \"abc%20def\"." - (or safe (setq safe "")) - (mapconcat (lambda (c) - (if (if (string-match "]" safe) - ;; ] should be place at the beginning inside [] - (string-match - (format "[]a-zA-Z_.-/%s]" - (replace-regexp-in-string "]" "" safe)) - (char-to-string c)) - (string-match (format "[a-zA-Z_.-/%s]" safe) - (char-to-string c))) - (char-to-string c) - (format "%%%02x" c))) - (string-to-list (encode-coding-string s 'utf-8)) - "")) - -(defun emms-lyrics-url-quote-plus (s &optional safe) - "Run (emms-url-quote s \" \"), then replace ` ' with `+'." - (replace-regexp-in-string - " " "+" (emms-lyrics-url-quote s (concat safe " ")))) - ;;; emms-lyrics-mode diff --git a/emms-player-mpd.el b/emms-player-mpd.el index f291668..16d035d 100644 --- a/emms-player-mpd.el +++ b/emms-player-mpd.el @@ -758,7 +758,7 @@ Execute CALLBACK with CLOSURE as its first argument when done." (progn (require 'emms-url) (with-temp-buffer - (url-insert-file-contents (emms-escape-url url)) + (url-insert-file-contents (emms-url-quote url)) (emms-http-decode-buffer (current-buffer)) (emms-player-mpd-add-buffer-contents (current-buffer) closure callback))) diff --git a/emms-url.el b/emms-url.el index 7fe399f..9fca29d 100644 --- a/emms-url.el +++ b/emms-url.el @@ -26,28 +26,33 @@ ;;; Code: (require 'url) +(require 'emms-compat) -(defvar emms-url-specials - '((?\ . "%20") - (?\n . "%0D%0A") - (?& . "%26") - (?? . "%3F")) - "*An alist of characters which must be represented specially in URLs. -The transformation is the key of the pair.") +(defun emms-url-quote (s &optional safe) + "Replace special characters in S using the `%xx' escape. +Characters in [a-zA-Z_.-/] and SAFE(default is \"\") will never be +quoted. +e.g., + (url-quote \"abc def\") => \"abc%20def\"." + (or safe (setq safe "")) + (mapconcat (lambda (c) + (if (if (string-match "]" safe) + ;; ] should be place at the beginning inside [] + (string-match + (format "[]a-zA-Z_.-/%s]" + (emms-replace-regexp-in-string "]" "" safe)) + (char-to-string c)) + (string-match (format "[a-zA-Z_.-/%s]" safe) + (char-to-string c))) + (char-to-string c) + (format "%%%02x" c))) + (string-to-list (encode-coding-string s 'utf-8)) + "")) -(defun emms-escape-url (url) - "Escape specials in URL. - -The specials to escape are specified by the `emms-url-specials' -variable." - (apply (function concat) - (mapcar - (lambda (ch) - (let ((repl (assoc ch emms-url-specials))) - (if (null repl) - (char-to-string ch) - (cdr repl)))) - (append url nil)))) +(defun emms-url-quote-plus (s &optional safe) + "Run (emms-url-quote s \" \"), then replace ` ' with `+'." + (emms-replace-regexp-in-string + " " "+" (emms-url-quote s (concat safe " ")))) (defun emms-http-content-coding () (save-match-data -- cgit v1.2.3