Created
June 14, 2012 17:05
-
-
Save machty/2931511 to your computer and use it in GitHub Desktop.
async queue download
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
ownloadQueue = async.queue (photo, callback) -> | |
# Here we define the task handled by this concurrency-limiting queue. | |
downloadfile = photo.url | |
host = url.parse(downloadfile).hostname | |
filename = url.parse(downloadfile).pathname.split("/").pop() | |
filename = "./public/downloads/#{filename}" | |
photo.localFilename = filename | |
console.log "set photo.localFilename to #{photo.localFilename}" | |
theurl = http.createClient(80, host) | |
requestUrl = downloadfile | |
request = theurl.request('GET', requestUrl, {"host": host}) | |
request.end() | |
request.addListener 'response', (response) -> | |
downloadfile = fs.createWriteStream(filename, {'flags': 'a'}) | |
#sys.puts("File size " + filename + ": " + response.headers['content-length'] + " bytes.") | |
response.addListener 'data', (chunk) -> | |
downloadfile.write(chunk, encoding='binary'); | |
response.addListener "end", -> | |
downloadfile.end() | |
callback photo | |
, DOWNLOAD_CONCURRENCY | |
# Later... | |
# Enqueue all photos | |
downloadQueue.push photos, (photo) -> | |
job.statuses.push "Finished downloading #{photo.localFilename}" | |
if --imagesToDownload == 0 | |
job.statuses.push "Finished downloading all files" | |
publish job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment