The code imports necessary dependencies, sets up constants for Google Drive authentication and file paths, and defines two asynchronous functions: convertGoogleDoc
to convert Google Docs to various output types and downloadDocs
to download Google Docs from a specified folder. The code uses Node.js modules such as path
, fs
, and google-auth
to perform tasks, and includes error handling mechanisms to prevent file corruption or security issues.
npm run import -- "download all docs as actual data files"
const listDrive = importer.import("merge google drive")
const getRpcFromSpec = importer.import("get rpc from spec")
const authorize = importer.import("google oauth token client")
const google = {drive: ({version, auth}) => getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/drive.${version}.json`)), auth)}
const { safeurl } = importer.import("domain cache tools")
const GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/drive'
]
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const DOWNLOAD_PATH = path.join(PROFILE_PATH, 'Downloads')
async function convertGoogleDoc(fileId, outType) {
let client = await authorize(GOOGLE_AUTH_SCOPE)
let drive = await google.drive({version: 'v3', auth: client})
return await drive.files.export({
fileId: fileId,
mimeType: outType
})
}
async function downloadDocs(folder = 'root') {
let drive = await listDrive(folder)
for(let i = 0; i < drive.length; i++) {
if(drive[i].mimeType == 'application/vnd.google-apps.document') {
console.log('Exporting ' + drive[i].name)
try {
let response = await convertGoogleDoc(drive[i].id, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
let data = await response.data.arrayBuffer()
fs.writeFileSync(path.join(DOWNLOAD_PATH, safeurl(drive[i].name + '_' + drive[i].id) + '.docx'), Buffer.from(data))
} catch (e) {
console.log(e)
}
}
if(drive[i].mimeType == 'application/vnd.google-apps.spreadsheet') {
console.log('Exporting ' + drive[i].name)
try {
let response = await convertGoogleDoc(drive[i].id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
let data = await response.data.arrayBuffer()
fs.writeFileSync(path.join(DOWNLOAD_PATH, safeurl(drive[i].name + '_' + drive[i].id) + '.xlsx'), Buffer.from(data))
} catch (e) {
console.log(e)
}
}
// TODO: application/vnd.openxmlformats-officedocument.presentationml.presentation
}
}
module.exports = {
downloadDocs,
convertGoogleDoc,
}
// Import required modules
const { importFromCache } = require('import-cache');
const { google } = require('googleapis');
const fs = require('fs');
const path = require('path');
const { safeUrl } = require('domain-cache-tools');
const { getRpcFromSpec } = require('get-rpc-from-spec');
const { listDrives, authorize } = require('google-drive-tools');
// Define constants
const GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/drive'
];
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const DOWNLOAD_PATH = path.join(PROFILE_PATH, 'Downloads');
// Define a function to get the Google Drive API client
async function getGoogleDriveClient(version, auth) {
const driveApi = await getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/drive.${version}.json`)), auth);
return google.drive({ version, auth });
}
// Define a function to convert a Google Doc to a specific file type
async function convertGoogleDoc(fileId, outType) {
const client = await authorize(GOOGLE_AUTH_SCOPE);
const drive = await getGoogleDriveClient('v3', client);
return await drive.files.export({
fileId,
mimeType: outType
});
}
// Define a function to download Google Docs from a specific folder
async function downloadDocs(folder = 'root') {
const drives = await listDrives(folder);
for (const drive of drives) {
if (drive.mimeType === 'application/vnd.google-apps.document') {
console.log(`Exporting ${drive.name}`);
try {
const data = await convertGoogleDoc(drive.id, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
fs.writeFileSync(path.join(DOWNLOAD_PATH, safeUrl(`${drive.name}_${drive.id}.docx`)), data);
} catch (error) {
console.error(error);
}
} else if (drive.mimeType === 'application/vnd.google-apps.spreadsheet') {
console.log(`Exporting ${drive.name}`);
try {
const data = await convertGoogleDoc(drive.id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
fs.writeFileSync(path.join(DOWNLOAD_PATH, safeUrl(`${drive.name}_${drive.id}.xlsx`)), data);
} catch (error) {
console.error(error);
}
}
}
}
module.exports = {
downloadDocs,
convertGoogleDoc
};
Breakdown of the Code
The code starts by importing various dependencies using the importer.import
function. These dependencies include:
merge google drive
: likely a function for merging Google Drive dataget rpc from spec
: a function for getting an RPC (Remote Procedure Call) from a specificationgoogle oauth token client
: a client for authorizing Google OAuth tokensdomain cache tools
: a module for working with domain cache tools, specifically importing the safeurl
functionThe code sets up several constants:
GOOGLE_AUTH_SCOPE
: an array of scopes for authorizing Google Drive access (in this case, only the Drive scope is used)PROFILE_PATH
: the path to the user's profile directory (determined by environment variables)DOWNLOAD_PATH
: the path to the user's downloads directory (determined by the PROFILE_PATH
constant and a hardcoded path)The code defines two functions:
convertGoogleDoc(fileId, outType)
: an asynchronous function that converts a Google Doc to a specified output type (e.g., Word document or Excel file). It:
authorize
functiongoogle.drive
functiondrive.files.export
functiondownloadDocs(folder = 'root')
: an asynchronous function that downloads Google Docs from a specified folder. It:
listDrive
functionapplication/vnd.google-apps.document
or application/vnd.google-apps.spreadsheet
MIME typepath
and fs
modules are available, which suggests that it is running in a Node.js environment.safeurl
function is used to sanitize the filename by removing any invalid characters, which is a good practice to prevent file corruption or security issues.try
-catch
blocks are used to handle any errors that may occur during the file export or writing process.