<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: download code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 07:19:56 GMT</pubDate>
    <description>DZone Snippets: download code</description>
    <item>
      <title>Retrieve your Gmail messages as an XML feed</title>
      <link>http://snippets.dzone.com/posts/show/5097</link>
      <description>This Ruby example shows how to retrieve your most recent email as an atom XML feed from the Google Apps website.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'httpclient'&lt;br /&gt;&lt;br /&gt;url = "https://mail.google.com/a/yourwebsite.com/feed/atom"&lt;br /&gt;client = HTTPClient.new&lt;br /&gt;client.debug_dev = STDOUT if $DEBUG&lt;br /&gt;client.set_auth(url, 'yourname@yourwebsite.com', 'yourpassword')&lt;br /&gt;resp = client.get(url)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: You might require to gem install httpclient.</description>
      <pubDate>Sun, 03 Feb 2008 16:29:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5097</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Download all xkcd.com comics</title>
      <link>http://snippets.dzone.com/posts/show/4658</link>
      <description>This goes through all the first 329 (you might want to change this) pages, downloading the comic strips.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;for i in `seq 1 329`&lt;br /&gt;do&lt;br /&gt;	wget http://xkcd.com/$i/&lt;br /&gt;	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`&lt;br /&gt;	rm index.html&lt;br /&gt;done&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 Oct 2007 18:05:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4658</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Example file download servlet</title>
      <link>http://snippets.dzone.com/posts/show/4629</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  /**&lt;br /&gt;     *  Sends a file to the ServletResponse output stream.  Typically&lt;br /&gt;     *  you want the browser to receive a different name than the&lt;br /&gt;     *  name the file has been saved in your local database, since&lt;br /&gt;     *  your local names need to be unique.&lt;br /&gt;     *&lt;br /&gt;     *  @param req The request&lt;br /&gt;     *  @param resp The response&lt;br /&gt;     *  @param filename The name of the file you want to download.&lt;br /&gt;     *  @param original_filename The name the browser should receive.&lt;br /&gt;     */&lt;br /&gt;    private void doDownload( HttpServletRequest req, HttpServletResponse resp,&lt;br /&gt;                             String filename, String original_filename )&lt;br /&gt;        throws IOException&lt;br /&gt;    {&lt;br /&gt;        File                f        = new File(filename);&lt;br /&gt;        int                 length   = 0;&lt;br /&gt;        ServletOutputStream op       = resp.getOutputStream();&lt;br /&gt;        ServletContext      context  = getServletConfig().getServletContext();&lt;br /&gt;        String              mimetype = context.getMimeType( filename );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Set the response and go!&lt;br /&gt;        //&lt;br /&gt;        //&lt;br /&gt;        resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );&lt;br /&gt;        resp.setContentLength( (int)f.length() );&lt;br /&gt;        resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Stream to the requester.&lt;br /&gt;        //&lt;br /&gt;        byte[] bbuf = new byte[BUFSIZE];&lt;br /&gt;        DataInputStream in = new DataInputStream(new FileInputStream(f));&lt;br /&gt;&lt;br /&gt;        while ((in != null) &amp;&amp; ((length = in.read(bbuf)) != -1))&lt;br /&gt;        {&lt;br /&gt;            op.write(bbuf,0,length);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        in.close();&lt;br /&gt;        op.flush();&lt;br /&gt;        op.close();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Oct 2007 23:12:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4629</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>cURL Download</title>
      <link>http://snippets.dzone.com/posts/show/3919</link>
      <description>// function to dowload a $remote file and store as $local file, using curl&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function curl_download($remote, $local)	{&lt;br /&gt;	$cp = curl_init($remote);&lt;br /&gt;	$fp = fopen($local, "w");&lt;br /&gt;	&lt;br /&gt;	curl_setopt($cp, CURLOPT_FILE, $fp);&lt;br /&gt;	curl_setopt($cp, CURLOPT_HEADER, 0);&lt;br /&gt;	&lt;br /&gt;	curl_exec($cp);&lt;br /&gt;	curl_close($cp);&lt;br /&gt;	fclose($fp);	&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 04:22:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3919</guid>
      <author>damonp (Damon Parker)</author>
    </item>
    <item>
      <title>downloader.py</title>
      <link>http://snippets.dzone.com/posts/show/2887</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"&lt;br /&gt;__date__="3 Nov 2005 - 14 Feb 2007"&lt;br /&gt;__copyright__="Copyright 2006 2007 Andrew Pennebaker"&lt;br /&gt;__license__="GPL"&lt;br /&gt;__version__="0.5"&lt;br /&gt;__URL__="http://snippets.dzone.com/posts/show/2887"&lt;br /&gt;&lt;br /&gt;from urllib import urlopen&lt;br /&gt;&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;from getopt import getopt&lt;br /&gt;&lt;br /&gt;def getURLName(url):&lt;br /&gt;	directory=os.curdir&lt;br /&gt;&lt;br /&gt;	name="%s%s%s" % (&lt;br /&gt;		directory,&lt;br /&gt;		os.sep,&lt;br /&gt;		url.split("/")[-1]&lt;br /&gt;	)&lt;br /&gt;&lt;br /&gt;	return name&lt;br /&gt;&lt;br /&gt;def createDownload(url, proxy=None):&lt;br /&gt;	instream=urlopen(url, None, proxy)&lt;br /&gt;&lt;br /&gt;	filename=instream.info().getheader("Content-Length")&lt;br /&gt;	if filename==None:&lt;br /&gt;		filename="temp"&lt;br /&gt;&lt;br /&gt;	return (instream, filename)&lt;br /&gt;&lt;br /&gt;def download(instream, outstream):&lt;br /&gt;	outstream.write(instream.read())&lt;br /&gt;&lt;br /&gt;	outstream.close()&lt;br /&gt;&lt;br /&gt;def usage():&lt;br /&gt;	print "Usage: %s [options] &lt;url1 url2 url3 ...&gt;" % (sys.argv[0])&lt;br /&gt;	print "\n--httpproxy &lt;proxy&gt;"&lt;br /&gt;	print "--ftpproxy &lt;proxy&gt;"&lt;br /&gt;	print "--gopherproxy &lt;proxy&gt;"&lt;br /&gt;	print "\n--help (usage)"&lt;br /&gt;&lt;br /&gt;	sys.exit()&lt;br /&gt;&lt;br /&gt;def main():&lt;br /&gt;	systemArgs=sys.argv[1:] # ignore program name&lt;br /&gt;&lt;br /&gt;	urls=[]&lt;br /&gt;	proxies={}&lt;br /&gt;&lt;br /&gt;	optlist=[]&lt;br /&gt;	args=[]&lt;br /&gt;&lt;br /&gt;	try:&lt;br /&gt;		optlist, args=getopt(systemArgs, None, ["url=", "httpproxy=", "ftpproxy=", "gopherproxy=", "help"])&lt;br /&gt;	except Exception, e:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	if len(args)&lt;1:&lt;br /&gt;		usage()&lt;br /&gt;&lt;br /&gt;	for option, value in optlist:&lt;br /&gt;		if option=="--help":&lt;br /&gt;			usage()&lt;br /&gt;&lt;br /&gt;		elif option=="--httpproxy":&lt;br /&gt;			proxies["http"]=value&lt;br /&gt;		elif option=="--ftpproxy":&lt;br /&gt;			proxies["ftp"]=value&lt;br /&gt;		elif options=="--gopherproxy":&lt;br /&gt;			proxies["gopher"]=value&lt;br /&gt;&lt;br /&gt;	urls=args&lt;br /&gt;&lt;br /&gt;	for url in urls:&lt;br /&gt;		try:&lt;br /&gt;			outfile=open(getURLName(url), "wb")&lt;br /&gt;			fileName=outfile.name.split(os.sep)[-1]&lt;br /&gt;&lt;br /&gt;			url, length=createDownload(url, proxies)&lt;br /&gt;			if not length:&lt;br /&gt;				length="?"&lt;br /&gt;&lt;br /&gt;			print "Downloading %s (%s bytes) ..." % (url.url, length)&lt;br /&gt;			if length!="?":&lt;br /&gt;				length=float(length)&lt;br /&gt;			bytesRead=0.0&lt;br /&gt;&lt;br /&gt;			for line in url:&lt;br /&gt;				bytesRead+=len(line)&lt;br /&gt;&lt;br /&gt;				if length!="?":&lt;br /&gt;					print "%s: %.02f/%.02f kb (%d%%)" % (&lt;br /&gt;						fileName,&lt;br /&gt;						bytesRead/1024.0,&lt;br /&gt;						length/1024.0,&lt;br /&gt;						100*bytesRead/length&lt;br /&gt;					)&lt;br /&gt;&lt;br /&gt;				outfile.write(line)&lt;br /&gt;&lt;br /&gt;			url.close()&lt;br /&gt;			outfile.close()&lt;br /&gt;			print "Done"&lt;br /&gt;&lt;br /&gt;		except Exception, e:&lt;br /&gt;			print "Error downloading %s: %s" % (url, e)&lt;br /&gt;&lt;br /&gt;if __name__=="__main__":&lt;br /&gt;	main()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Oct 2006 07:02:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2887</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
    <item>
      <title>How to download files with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2469</link>
      <description>// Note: the "b" in "wb" in the open method may not be needed in&lt;br /&gt;// non-Windows environments.  In Windows it indicates that you're writing&lt;br /&gt;// binary information.  You probably won't need it for downloading straight text&lt;br /&gt;// or html either.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;&lt;br /&gt;Net::HTTP.start("static.flickr.com") { |http|&lt;br /&gt;  resp = http.get("/92/218926700_ecedc5fef7_o.jpg")&lt;br /&gt;  open("fun.jpg", "wb") { |file|&lt;br /&gt;    file.write(resp.body)&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;puts "Yay!!"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 25 Aug 2006 19:59:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2469</guid>
      <author>TDonaghe (Terry Donaghe)</author>
    </item>
    <item>
      <title>Download recent flickr pictures with ruby and the flickr api</title>
      <link>http://snippets.dzone.com/posts/show/2468</link>
      <description>// To make this work, you need to get your own flickr api key.  &lt;br /&gt;// Get one here: http://www.flickr.com/services/api/misc.api_keys.html&lt;br /&gt;// Other than that, just plug and chug and have fun!&lt;br /&gt;// The "b" in "wb" in the second open method may not be necessary in&lt;br /&gt;// non-windows environments.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;&lt;br /&gt;open('http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&amp;api_key=YOUR_KEY_HERE') { |f|&lt;br /&gt;    doc = REXML::Document.new f.read&lt;br /&gt;    i = 0&lt;br /&gt;    doc.elements.each("rsp/photos/photo") { |element|&lt;br /&gt;        if i &lt; 3&lt;br /&gt;            open("images/file" &lt;&lt; i.to_s &lt;&lt; ".jpg", "wb").&lt;br /&gt;                write(open("http://static.flickr.com/" &lt;&lt; \&lt;br /&gt;                element.attributes["server"] &lt;&lt; "/" &lt;&lt; \&lt;br /&gt;                element.attributes["id"] &lt;&lt; "_" &lt;&lt; \&lt;br /&gt;                element.attributes["secret"] &lt;&lt; "_o.jpg").read)&lt;br /&gt;        else&lt;br /&gt;            break&lt;br /&gt;        end&lt;br /&gt;        i = i + 1&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;puts "Done!"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 25 Aug 2006 19:51:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2468</guid>
      <author>TDonaghe (Terry Donaghe)</author>
    </item>
    <item>
      <title>Force output to download as a file in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2046</link>
      <description>&lt;code&gt;&lt;br /&gt;response.headers['Content-Type'] = 'text/csv' # I've also seen this for CSV files: 'text/csv; charset=iso-8859-1; header=present'&lt;br /&gt;response.headers['Content-Disposition'] = 'attachment; filename=thefile.csv'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 23:32:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2046</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Download the pys60 tutorial</title>
      <link>http://snippets.dzone.com/posts/show/1333</link>
      <description>Many wonderful examples by &lt;a href=http://mlab.uiah.fi/~jscheib/&gt;J&#252;rgen Scheible&lt;/a&gt;.&lt;br /&gt;I omit the zip,exe,pdf to save site's bandwidth.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;C:\&gt;wget -kmnp -l3 -Rzip,exe,pdf http://www.mobilenin.com/pys60/menu.htm&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You can just browse &lt;a href=http://www.mobilenin.com/pys60/menu.htm&gt;the site&lt;/a&gt;.</description>
      <pubDate>Wed, 01 Feb 2006 13:05:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1333</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Mirror a site with wget</title>
      <link>http://snippets.dzone.com/posts/show/133</link>
      <description>I create a batch file (w.bat) containing just&lt;br /&gt;&lt;code&gt;&lt;br /&gt;wget -krmnp %1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then I call&lt;br /&gt;&lt;code&gt;&lt;br /&gt;C:\&gt;w [what-ever-url]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Apr 2005 05:07:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/133</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
