Created
April 5, 2011 17:29
-
-
Save chrishamant/904064 to your computer and use it in GitHub Desktop.
use node to scrape and save some images from a url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
sys = require 'sys' | |
fs = require 'fs' | |
http = require 'http' | |
tovisit = [] | |
imgcount = 0 | |
getImage= (url)-> | |
options = | |
host: 'hostname.com' | |
port: 80 | |
path: url | |
http.get(options, (res)-> | |
size = parseInt(res.headers['content-length'],10) | |
buffer = new Buffer(size) | |
start = 0 | |
res.on 'data', (chunk)-> | |
chunk.copy(buffer,start,0,chunk.length) | |
start += chunk.length | |
res.on 'end', ()-> | |
filename = url.match(/id=(\d+)/)[1] + '.jpg' | |
fs.writeFile(filename , buffer, (err) -> | |
console.log err if err | |
imgcount++ | |
) | |
).on('error', (e)-> | |
console.log("Got error: " + e.message) | |
) | |
tovisit.push "/path/to/img.jpg" | |
getImage(url) for url in tovisit | |
#console.log("wrote " + imgcount + " images") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment