Last active
January 4, 2017 19:58
-
-
Save ajmas/f37984b5fc333d5f215bc8242a174207 to your computer and use it in GitHub Desktop.
Downloads a directory structure from Google Drive. Also handles exporting of the Google Docs.
This file contains hidden or 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
const fs = require('fs-extra'); | |
const google = require('googleapis'); | |
const OAuth2 = google.auth.OAuth2; | |
const key = require('./key.json'); | |
var baseFolder = 'base'; | |
function downloadFile(file, path) { | |
setTimeout(function () { | |
var filePath = path.concat(file.name).join('/'); | |
console.log('creating file: ', filePath); | |
var dest = fs.createWriteStream(filePath); | |
// For Google Docs files only | |
drive.files.get({ | |
fileId: file.id, | |
alt: 'media' | |
}) | |
.on('end', function() { | |
console.log('Done'); | |
}) | |
.on('error', function(err) { | |
console.log('Error during download', err); | |
}) | |
.pipe(dest); | |
}, 1000); | |
} | |
function exportFile(file, path, mimeType, suffix) { | |
setTimeout(function () { | |
var name = file.name + suffix; | |
var filePath = path.concat(name).join('/'); | |
console.log('exporting to file: ', filePath); | |
var dest = fs.createWriteStream(filePath); | |
// For Google Docs files only | |
drive.files.export({ | |
fileId: file.id, | |
mimeType: mimeType | |
}, { | |
encoding: null // Make sure we get the binary data | |
}) | |
.on('end', function() { | |
console.log('Done'); | |
}) | |
.on('error', function(err) { | |
console.log('Error during download', err); | |
}) | |
.pipe(dest); | |
}, 1000); | |
} | |
function visitDirectory(fileId, parentPath) { | |
console.log('path: ', parentPath.join('/')); | |
setTimeout(function () { | |
drive.files.list({ | |
includeRemoved: false, | |
spaces: 'drive', | |
fileId: fileId, | |
fields: 'nextPageToken, files(id, name, parents, mimeType, modifiedTime)', | |
q: `'${fileId}' in parents` | |
}, function (err, resp) { | |
if (!err) { | |
var i; | |
var files = resp.files; | |
for (i=0; i<files.length; i++) { | |
if (files[i].mimeType == 'application/vnd.google-apps.folder') { | |
console.log('directory: ' + files[i].name); | |
var path = parentPath.concat(files[i].name); | |
visitDirectory(files[i].id, path); | |
try { | |
fs.mkdirp( parentPath.concat(files[i].name).join('/')); | |
} catch (err) { | |
// Ignored | |
} | |
} else if (files[i].mimeType === 'application/vnd.google-apps.document') { | |
exportFile(files[i], parentPath, 'application/pdf', '.pdf'); | |
} else if (files[i].mimeType === 'application/vnd.google-apps.spreadsheet') { | |
// exportFile(files[i], parentPath, 'text/csv, '.csv'); | |
exportFile(files[i], parentPath, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx'); | |
} else if (files[i].mimeType.startsWith('application/vnd.google-apps')) { | |
console.log('unhandled Google Doc type: ', files[i].mimeType); | |
} else { | |
console.log('unhandled type: ', files[i].mimeType); | |
downloadFile(files[i], parentPath); | |
} | |
} | |
} else { | |
console.log('error: ', err); | |
} | |
}); | |
}, 1000); | |
} | |
var jwtClient = new google.auth.JWT( | |
key.client_email, | |
null, | |
key.private_key, | |
['https://www.googleapis.com/auth/drive.readonly'], | |
null | |
); | |
const drive = google.drive({ | |
version: 'v3', | |
auth: jwtClient | |
}); | |
jwtClient.authorize(function (err, tokens) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
var fileId = 'xxxxx'; | |
visitDirectory(fileId, [baseFolder]); | |
}); | |
// ref: https://developers.google.com/drive/v3/web/folder | |
// ref: https://www.npmjs.com/package/googleapis | |
// ref: https://developers.google.com/drive/v3/web/search-parameters | |
// ref: https://developers.google.com/drive/v3/web/manage-downloads | |
// ref: https://developers.google.com/drive/v3/reference/files#resource |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment