-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add action to download forms from google drive
Closes #3
- Loading branch information
Showing
6 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const fs = require('../lib/sync-fs'); | ||
const google = require('googleapis'); | ||
const googleAuth = require('../lib/google-auth'); | ||
const info = require('../lib/log').info; | ||
|
||
module.exports = projectDir => { | ||
return googleAuth() | ||
.then(auth => { | ||
const drive = google.drive({ | ||
version: 'v3', | ||
auth: auth, | ||
}); | ||
|
||
const forms = fs.readJson(`${projectDir}/forms-on-google-drive.json`); | ||
|
||
return Object.keys(forms) | ||
.reduce(fetchForm, Promise.resolve()); | ||
|
||
function fetchForm(promiseChain, localName) { | ||
return promiseChain | ||
.then(() => new Promise((resolve, reject) => { | ||
const remoteName = forms[localName]; | ||
|
||
const fetchOpts = { | ||
auth: auth, | ||
fileId: forms[localName], | ||
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
}; | ||
|
||
const formDir = 'forms'; | ||
fs.mkdir(formDir); | ||
const target = `${formDir}/${localName}`; | ||
|
||
info(`Exporting ${remoteName} from google drive to ${target}…`); | ||
|
||
drive.files.export(fetchOpts, (err, buffer) => { | ||
if(err) return reject(err); | ||
try { | ||
fs.writeBinary(target, buffer); | ||
info(`Successfully wrote ${target}.`); | ||
resolve(); | ||
} catch(e) { | ||
reject(e); | ||
} | ||
}); | ||
})); | ||
} | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const child_process = require('child_process'); | ||
const fs = require('../lib/sync-fs'); | ||
const google = require('googleapis'); | ||
const info = require('../lib/log').info; | ||
const readline = require('readline-sync'); | ||
const warn = require('../lib/log').warn; | ||
|
||
const SECRETS_FILE = './.gdrive.secrets.json'; | ||
|
||
module.exports = () => { | ||
const client = oauthClient(); | ||
|
||
const authUrl = client.generateAuthUrl({ | ||
scope: 'https://www.googleapis.com/auth/drive.readonly' | ||
}); | ||
|
||
openBrowserAt(authUrl); | ||
const accessCode = readline.question(`Enter access code from browser: `); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.getToken(accessCode, function (err, tokens) { | ||
if(err) return reject(err); | ||
|
||
client.setCredentials(tokens); | ||
|
||
resolve(client); | ||
}); | ||
}); | ||
}; | ||
|
||
function oauthClient() { | ||
let configFile; | ||
try { | ||
configFile = fs.readJson(SECRETS_FILE); | ||
} catch(e) { | ||
info('Failed to load google drive secrets from file.', e); | ||
configFile = {}; | ||
} | ||
|
||
const clientId = process.env.GOOGLE_CLIENT_ID || configFile.client_id; | ||
const clientSecret = process.env.GOOGLE_CLIENT_SECRET || configFile.client_secret; | ||
const redirectUri = process.env.GOOGLE_REDIRECT_URI || | ||
(configFile.redirect_uris && configFile.redirect_uris[0]); | ||
|
||
const missingConfig = | ||
checkRequred(clientId, 'client_id', 'GOOGLE_CLIENT_ID') | | ||
checkRequred(clientSecret, 'client_secret', 'GOOGLE_CLIENT_SECRET') | | ||
checkRequred(redirectUri, 'redirect_uris', 'GOOGLE_REDIRECT_URI'); | ||
|
||
if(missingConfig) throw new Error('Missing required config for google drive access. Please check warnings.'); | ||
|
||
return new google.auth.OAuth2(clientId, clientSecret, redirectUri); | ||
} | ||
|
||
function openBrowserAt(url) { | ||
info(`Should open browser at: ${url}`); | ||
child_process.exec(`open "${url}" || firefox "${url}" || chromium-browser "${url}" || chrome "${url}"`); | ||
} | ||
|
||
function checkRequred(value, jsonKey, envVar) { | ||
if(value) return; | ||
|
||
warn(`Missing .${jsonKey} in ${SECRETS_FILE} or env var ${envVar}!`); | ||
return true; | ||
} |
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