/
parse.js
181 lines (163 loc) · 5.47 KB
/
parse.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
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
179
180
181
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var archieml = require('archieml');
var fs = require('fs');
var data = "";
module.exports = exports;
// Edit the file ID for the Google Doc. Be sure it's set to the "Share" preferences to "anyone on the internet"
//var fileId = '1eXbE48sI_6qaGeU6gCE3gv3Pw1L5xFapiiKRCKxqX0c';
var fileIds = {
"hiphop": "1ieI3nN9jke_bAr7sK9VQAI9NMhWTKivI6ttv3iGBhRQ",
"hiphopespanol": "1ZZaEkkP_Y7cDVmgrBRsfPpk_GVvnAnNKqL18Dg32ins",
"harbor": "1hYVU1e8l0vP7U5vGmWJY1k2B7mnjHpQi1qd4gn9xkFE",
"harborespanol": "1tH-m6bW_3YiGFtH7_Sd_DJDxIKAKJl_rdErOeSUmQ0s",
"dorian": "1GVusAF86-rrv6RwSMF_H47xO2eG2tVayfumcj7Q1akM",
"dorianespanol": "1Xw8ruEIxYb7JykzH1MLcniNYjY_ck5BmH0j_AyXJUfw",
"family": "1RY9sG1oazoFfE9ZGEK9U3zaAmyOJB6mYcc6VDKy7xBA",
"skate": "1M1o12fbc33KOhkNJl-Vagimqbm3Diof-cKVbNvZuFi0",
"familyespanol": "1RgGokPsPC_3UOU456wH8jcJmpG2t5TZ5nn9ucwC3yN4",
"skate": "1M1o12fbc33KOhkNJl-Vagimqbm3Diof-cKVbNvZuFi0",
"skateespanol": "1-hkABBltJ4dIrOueAgHyT16wra6o3YmFBzwily49I0Y"
}
module.exports.sendIds = function(){
return fileIds;
}
// Overall varibales
var SCOPES = ['https://www.googleapis.com/auth/drive'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'drive-api-quickstart.json';
// Load client secrets from a local file.
var oauth2Client;
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Drive API.
authorize(JSON.parse(content), getExportLink);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
/**
* Get the actual file contents as plain text
*
* @param {String} The URL to request the content from
*/
function getFileContents(exportLink, currentTopic){
oauth2Client.request({
method: 'GET',
uri: exportLink
}, function(err, body) {
if (err) {
console.log('Getting file contents failed.', err);
return;
}
data = archieml.load(body);
//fs.writeFile("~"+count+".txt", JSON.stringify(data)); count++; //write 'data' to a file
data.text.forEach(function(el, index, arr){
if (el.type !== "text" ) return;
el.value = el.value.replace(/\[a\]/gm, ""); //remove "[a]"
el.value = el.value.replace(/\*\*([^*]+)\*\*/gm, "<span class='italic'>$1</span>"); //add italics class to **text**
});
sendIt(data, currentTopic);
});
}
function sendIt(data, currentTopic){
var fileLocation = './public/data/' + currentTopic + '.json';
fs.writeFileSync(fileLocation, JSON.stringify(data,null,2));
}
/**
* Get the download link, also called the export link, from the file response
*
* @param {Object} Google Drive authorization
*/
function getExportLink(auth){
for (i in fileIds){
(function(i){
var currentTopic = i;
var service = google.drive('v2');
var request = service.files.get({
auth: auth,
fileId: fileIds[i]
},function(err,response){
if(err){
console.log('Getting export link failed.', err);
return;
}
var exportLink = response['exportLinks']['text/plain'];
getFileContents(exportLink, currentTopic);
});
})(i);
}
}