-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_drive.ts
178 lines (163 loc) · 5.17 KB
/
google_drive.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/** @format */
// import fs from "fs";
// import { createInterface } from "readline"; //Used for setting token
// import { google } from "googleapis"; //Used for accessing google drive
import parse = require("csv-parse");
import stringify = require("csv-stringify");
import request = require("request");
// Logs into Google Account
// function authorize() {
// return new Promise((resolve, reject) => {
// const oAuth2Client = new google.auth.OAuth2(
// process.env.G_ID,
// process.env.G_SECRET,
// "urn:ietf:wg:oauth:2.0:oob"
// );
// if (!process.env.access_token) {
// return getAccessToken(oAuth2Client);
// } else {
// oAuth2Client.setCredentials({
// access_token: process.env.access_token,
// refresh_token: process.env.refresh_token,
// token_type: process.env.token_type,
// expiry_date: process.env.expiry_date,
// });
// resolve(oAuth2Client);
// }
// });
// }
// Creates access token for oAuth 2
// function getAccessToken(oAuth2Client) {
// const authUrl = oAuth2Client.generateAuthUrl({
// access_type: "offline",
// scope: ["https://www.googleapis.com/auth/drive.readonly"],
// });
// console.log("\nAuthorize by visiting this url:\n\n", authUrl, "\n");
// const rl = createInterface({
// input: process.stdin,
// output: process.stdout,
// });
// rl.question("The enter code: ", (code) => {
// rl.close();
// oAuth2Client.getToken(code, (err, token) => {
// if (err) return console.error("Error retrieving access token", err);
// oAuth2Client.setCredentials(token);
// console.log("Store token values in .env:");
// console.log(JSON.stringify(token));
// });
// });
// }
// Returns JSON of CSV string
export const parseCSV = (csvData: string): Promise<Object[]> => {
return new Promise((resolve, reject) => {
parse(csvData, { columns: true }, (err, JSONData) => {
if (err) reject(err);
resolve(JSONData);
});
});
};
// Returns JSON of CSV string
export const parseJSON = (JSONData, callback = console.log) => {
stringify(JSONData, { header: true, quoted_string: true }, (err, csvData) => {
if (err) console.log(err);
callback(csvData);
});
};
// Merges two arrays of objects via a single shared key
function merge(Obj1, Obj2, key) {
return Obj1.map((u) => {
try {
const v = Obj2.filter((v) => v[key] === u[key])[0];
Object.keys(v).forEach((k) => (u[k] = v[k]));
return u;
} catch (e) {
return false;
}
}).filter((u) => u);
}
// function getDataSheet(auth, location) {
// return new Promise((resolve, reject) => {
// const drive = google.drive({ version: "v3", auth });
// drive.files.export(
// { fileId: location, mimeType: "text/csv" },
// (err, response) => {
// // console.log(response);
// if (err) reject(err, err.stack);
// // console.log(response.data);
// resolve(response.data);
// }
// );
// });
// }
// function getSheetsData(auth, location) {
// return new Promise((resolve, reject) => {
// const client = google.sheets({ version: "v4", auth: auth });
// client.spreadsheets.get(
// {
// spreadsheetId: location,
// fields: "*",
// },
// (err, response) => {
// if (err) reject(err, err.stack);
// resolve(response.data);
// }
// );
// });
// }
function getRequestSheetCSV(url, title) {
return new Promise((resolve, reject) => {
request(
{
url: url,
headers: { Authorization: `Bearer ${process.env.access_token}` },
},
(err, response, body) => {
if (err) reject(err);
resolve({ title: title, data: body });
}
);
});
}
function getSheetsContent(sheetsData) {
return Promise.all(
sheetsData.sheets.map((s) => {
return getRequestSheetCSV(
`https://docs.google.com/spreadsheets/d/${sheetsData.spreadsheetId}/export?format=csv&id=${sheetsData.spreadsheetId}&gid=${s.properties.sheetId}`,
s.properties.title
);
})
);
}
function parseCSVs(csvDataList) {
return Promise.all(csvDataList.map((sheet) => parseCSV(sheet.data)));
}
//Logs if thre's an issue
function logError(error) {
console.log(`Logging error: ${error}`);
console.log(error.stack);
}
// function loadData() {
// return authorize()
// .then(getDataSheet)
// .then(parseCSV)
// .then(console.log("Imported data"))
// .catch(logError);
// }
// function loadCSV(location) {
// return authorize()
// .then((a) => getDataSheet(a, location))
// .then(parseCSV)
// .then(console.log("Imported data"))
// .catch(logError);
// }
// function loadAllSheetsCSV(location) {
// return authorize()
// .then((a) => getSheetsData(a, location))
// .then(getSheetsContent)
// .then(parseCSVs)
// .then(console.log("Imported sheets data"))
// .catch(logError);
// }
// export const loadData = loadData;
// export const loadCSV = loadCSV;
// export const loadAllSheetsCSV = loadAllSheetsCSV;