Skip to content

Instantly share code, notes, and snippets.

@nielsvanvelzen
Last active March 30, 2016 11:29
Show Gist options
  • Save nielsvanvelzen/8ca6a780d3c7989870b5c761d2844a51 to your computer and use it in GitHub Desktop.
Save nielsvanvelzen/8ca6a780d3c7989870b5c761d2844a51 to your computer and use it in GitHub Desktop.
/* npm install request */
var user = null;
var album = null;
var authkey = null;
var downloadDir = './download/{user}-{album}/';
var args = process.argv.slice(2);
if(args.length >= 1 && args[0].substr(0, 4) === 'http'){
user = args[0].substr(args[0].indexOf('.com/') + 5).split('/')[0];
album = args[0].substr(args[0].indexOf(user + '/') + user.length + 1).split('?')[0];
authkey = args[0].substr(args[0].indexOf('authkey=') + 8).split('&')[0];
downloadDir = downloadDir.replace('{album}', album);
var https = require('https');
var htmlReq = https.get(args[0], function(res) {
var albumHTML = '';
res.on('data', function(chunk) {
albumHTML += chunk;
});
res.on('end', function() {
album = albumHTML.substr(albumHTML.indexOf('var _album = {id:\'') + 18).split('\'')[0];
download();
});
});
if(args.length >= 2)
downloadDir = args[1];
}else{
if(args.length >= 1)
user = args[0];
if(args.length >= 2)
album = args[1];
if(args.length >= 3)
authkey = args[2];
if(args.length >= 4)
downloadDir = args[3];
if(user === null || album === null)
console.error('No user or album given.');
else
download();
}
var download = function(){
downloadDir = downloadDir.replace('{user}', user).replace('{album}', album);
var url = 'https://picasaweb.google.com/data/feed/base/user/' + user + '/albumid/' + album + '?alt=json&kind=photo' + (authkey != null ? '&authkey=' + authkey : '');
var https = require('https');
var req = https.get(url, function(res) {
var json = '';
res.on('data', function(chunk) {
json += chunk;
});
res.on('end', function() {
_searchPhotos(JSON.parse(json));
});
});
}
var _searchPhotos = function (result){
result.feed.entry.forEach(function (item){
var url = item.content.src;
url = url.replace("%20", " ");
url = url.replace("%20", " ");
url = url.replace("%22", "\"");
url = url.replace("%27", "'");
var index = url.lastIndexOf('/');
url = url.substr(0, index) + '/d' + url.substr(index);
_downloadPhoto(url);
});
};
var _downloadPhoto = function (url){
var fs = require('fs');
var request = require('request');
if(!fs.existsSync(downloadDir))
fs.mkdirSync(downloadDir);
request.head(url, function(err, res, body){
var fileName = url.substr(url.lastIndexOf('/') + 1);
console.log('Downloaded ' + fileName);
request(url).pipe(fs.createWriteStream(downloadDir + fileName));
});
}
npm install request && node ./picasa.js [url]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment