-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-drive-repository.js
116 lines (106 loc) · 3.4 KB
/
google-drive-repository.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const {google} = require('googleapis');
const fs = require('fs');
const readline = require('readline');
const rp = require('request-promise');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = './credentials.json';
async function promptLogin(boxScoreData){
fs.readFile('./google-client-id.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), uploadSheet, boxScoreData);
});
}
function authorize(credentials, callback, csv) {
const {client_secret, client_id, redirect_uris} = credentials.web;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, 'https://www.google.com/');
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client, csv);
});
}
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function GetFileName(boxScoreData){
return boxScoreData.awayTeamName + ' vs ' + boxScoreData.homeTeamName + ' - ' + boxScoreData.dateTime;
}
async function getFolderList(drive){
var response = await drive.files.list({
q: "mimeType='application/vnd.google-apps.folder' AND name = '" + new Date().toDateString() +"'"
});
return response.data.files;
}
async function getFolderId(drive){
var folders = await getFolderList(drive);
if(folders.length > 0){
return folders[0].id;
}
var id;
var fileMetadata = {
'name': new Date().toDateString(),
'mimeType': 'application/vnd.google-apps.folder'
};
drive.files.create({
resource: fileMetadata,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
id = file.data.id;
}
});
return id;
}
async function uploadSheet(auth, boxScoreData) {
const drive = google.drive({version: 'v3', auth});
var folderId = await getFolderId(drive);
var fileMetadata = {
'name': GetFileName(boxScoreData),
'mimeType': 'application/vnd.google-apps.spreadsheet',
parents: [folderId]
};
var media = {
mimeType: 'text/csv',
body: boxScoreData.csv
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id:', file.data.id);
}
});
}
module.exports.promptLogin = promptLogin;