Last active
August 29, 2015 13:58
-
-
Save mashihua/9928757 to your computer and use it in GitHub Desktop.
download qzone images
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 | |
http = require 'http' | |
url = require 'url' | |
fs = require 'fs' | |
{spawn} = require 'child_process' | |
JS = """ | |
//For qzone: | |
var doc, imgs, img, i, val, s, len; | |
doc = document.querySelectorAll('.app_canvas_frame')[0].contentDocument; | |
imgs = doc.querySelectorAll('img[data-src]'); | |
vals = []; | |
for(i = 0 ,len= imgs.length; i < len; i++){ | |
img = imgs.item(i); | |
vals.push(img.getAttribute('data-src')); | |
} | |
s = doc.createElement('img') | |
s.src='http://localhost:8000/?q='+ encodeURIComponent(JSON.stringify(vals)) | |
s='Send to server is done!' | |
""" | |
CHROME = """ | |
tell application "Google Chrome" | |
execute front window's active tab javascript "#{JS}" | |
end tell | |
""" | |
SAFARI= """ | |
tell application "Safari" | |
do JavaScript "#{JS}" in document 1 | |
end tell | |
""" | |
SCRIPT = CHROME | |
PATH = "#{__dirname}/qzone_imgs" | |
rmDir = (dirPath)-> | |
try | |
files = fs.readdirSync dirPath | |
catch e | |
return | |
if files.length > 0 | |
for file in files | |
filePath = "#{dirPath}/#{file}" | |
if fs.statSync(filePath).isFile() | |
fs.unlinkSync filePath | |
else | |
rmDir filePath | |
fs.rmdirSync dirPath | |
init = -> | |
rmDir PATH | |
fs.mkdirSync PATH | |
CountDownLatch = (@count, @cb)-> | |
CountDownLatch::done = -> | |
if @count < 0 | |
throw new Error 'illegal state.' | |
@count-- | |
if @count is 0 | |
@cb() | |
start = -> | |
server = http.createServer (req, res)-> | |
# set timeout on TCP | |
server.on "connection", (stream)-> | |
stream.setTimeout 1000 | |
server.on 'request', (req, res)-> | |
opts = url.parse req.url, true | |
if urls = opts.query.q | |
try | |
download JSON.parse urls | |
catch e | |
console.log e | |
res.statusCode = 204 | |
res.end() | |
server.listen 8000, -> | |
child = spawn "osascript" , ["-e", SCRIPT] | |
child.stderr.on 'data', (data)-> | |
console.error '' + data | |
child.stdout.on 'data', (data)-> | |
if /^missing/.test data.toString('utf8') | |
console.error 'Plase active qzone web page on browser' | |
child.on 'close', (code)-> | |
if code is not 0 | |
console.log code | |
download = (urls) -> | |
count = new CountDownLatch urls.length, -> | |
console.log "Downloaded %d images.", urls.length | |
server.close() | |
for _url,ind in urls | |
do (_url, ind)-> | |
console.log "Start download: %s", _url | |
http.get _url, (res)-> | |
if res.statusCode is 200 | |
ws = fs.createWriteStream "#{PATH}/#{ind}.jpeg" | |
res.pipe ws | |
ws.on 'finish', -> | |
console.log "Downloaded: %s", _url | |
count.done() | |
else | |
console.log "Downloaded: %s failure", _url | |
count.done() | |
if require.main is module | |
SCRIPT = SAFARI if process.argv.length > 2 | |
init() | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment