From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: Function to download a URL and return it as a string Date: Sat, 8 Dec 2012 21:38:15 -0800 Message-ID: <48DA15B7852B46D1A879470B55F3AC5A@us.oracle.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1355031515 6014 80.91.229.3 (9 Dec 2012 05:38:35 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 9 Dec 2012 05:38:35 +0000 (UTC) To: "'Sean McAfee'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 09 06:38:49 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ThZbD-0000zE-7B for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 06:38:47 +0100 Original-Received: from localhost ([::1]:40343 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThZb0-0002Z4-Ry for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 00:38:34 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:58692) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThZau-0002Y8-2z for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 00:38:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ThZat-0002sD-2h for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 00:38:28 -0500 Original-Received: from aserp1040.oracle.com ([141.146.126.69]:17878) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThZas-0002s8-Sh for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 00:38:27 -0500 Original-Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by aserp1040.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id qB95cOfY017670 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 9 Dec 2012 05:38:25 GMT Original-Received: from acsmt356.oracle.com (acsmt356.oracle.com [141.146.40.156]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id qB95cORe009497 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 9 Dec 2012 05:38:24 GMT Original-Received: from abhmt115.oracle.com (abhmt115.oracle.com [141.146.116.67]) by acsmt356.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id qB95cOiY022648; Sat, 8 Dec 2012 23:38:24 -0600 Original-Received: from dradamslap1 (/71.202.147.44) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 08 Dec 2012 21:38:24 -0800 X-Mailer: Microsoft Office Outlook 11 In-reply-to: X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Thread-Index: Ac3VzNzqrneDiURFTEu9f8SnEIlq5gAAiGDw X-Source-IP: acsinet22.oracle.com [141.146.126.238] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 141.146.126.69 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88098 Archived-At: > I recently wanted to be able to download a document from a URL and > return it as a string. I couldn't find a function that did that > precisely, but I was able to construct my own: > > (defun download (url) > (with-current-buffer (url-retrieve-synchronously url) > (prog2 > (when (not (search-forward-regexp "^$" nil t)) > (error "Unable to locate downloaded data")) > (buffer-substring (1+ (point)) (point-max)) > (kill-buffer)))) > > This seems rather busy for such a basic-seeming operation--in > particular, having to take care to delete the buffer created by > url-retrieve-synchronously. Is there a better way to do it? Dunno anything about whether there is a better way, but consider using `unwind-protect', with `kill-buffer' as your cleanup part. And to play safe, pass the actual buffer (returned from `url-*') as arg to `kill-buffer'. Using `unwind-protect' makes sure the buffer is killed when you're done, no matter what.