-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
216 lines (207 loc) · 6.42 KB
/
cli.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"use strict";
const inquirer = require("inquirer");
const passwordPrompt = require("./auth.js");
const { updateFromTextFile } = require("./upload");
const { scrapePageToTextFile } = require("./download.js");
const { cleanRecord } = require("./transform.js");
const range = require("lodash/range");
const RECORDS_PER_PAGE = 1000;
const SOURCE_PAGES = {
spuf: 14,
postpr: 104,
};
const OFFSETS = {
spuf: range(0 * 1000 + 1, SOURCE_PAGES.spuf * RECORDS_PER_PAGE + 1, 1000),
postpr: range(0 * 1000 + 1, SOURCE_PAGES.postpr * RECORDS_PER_PAGE + 1, 1000),
};
process.on("exit", () => console.log("👑 Bye!"));
const onQuit = () => process.exit(0);
process.on("SIGINT", onQuit);
// TODO: Refactor (never)
const onHydrate = ({
rutgersTableName = "spuf",
collectionName = "spuffordCurrency",
verbose = false,
nPagesToSkip = 0,
dstDir,
}) => async ({ db }) => {
try {
const collection = await db.collection(collectionName);
const onRecord = cleanRecord[collectionName];
const hydrateResults = await Promise.all(
OFFSETS[rutgersTableName].slice(nPagesToSkip).map(async (offset) => {
try {
const { textFile } = await scrapePageToTextFile({
offset,
rutgersTableName,
dstDir,
})();
const parseResult = await updateFromTextFile({
offset,
textFile,
collection,
onRecord,
});
console.log(verbose ? parseResult : ".");
return parseResult;
} catch (scrapeAndReplacePageError) {
const error =
scrapeAndReplacePageError.codeName ||
scrapeAndReplacePageError.message;
console.log(verbose ? error : ".");
return {
nSuccess: 0,
nFail: 0,
nDocuments: 0,
offset,
error,
};
}
})
);
const result = hydrateResults.reduce(
(acc, curr) => ({
nSuccess: acc.nSuccess + curr.nSuccess,
nFail: acc.nFail + curr.nFail,
[`page${curr.offset}`]: JSON.stringify(
{ errors: curr.nFailByCodeName || curr.error },
2,
null
),
nDocuments: acc.nDocuments + curr.nDocuments,
}),
{ nSuccess: 0, nFail: 0, nDocuments: 0 }
);
return {
result,
nPages: OFFSETS[rutgersTableName].length,
};
} catch (scrapeAndReplaceAllError) {
return { error: scrapeAndReplaceAllError };
}
};
const retrieveValidator = ({ collectionName = "spuffordCurrency" }) => async ({
db,
}) => {
try {
const collectionInfos = await db
.listCollections({ name: collectionName })
.toArray();
return JSON.stringify(collectionInfos[0].options.validator, null, 2);
} catch (seeValidatorError) {
return { error: seeValidatorError };
}
};
// todo: refactor to get distinct whatever
const getDistinctPlacesSpuf = async ({ db }) => {
try {
const collection = await db.collection("spuffordCurrency");
const results = await collection.distinct("Place");
return results;
} catch (seeMetaDataErr) {
return { error: seeMetaDataErr };
}
};
const getDistinctCurrenciesSpuf = async ({ db }) => {
try {
const collection = await db.collection("spuffordCurrency");
const currFrom = await collection.distinct("Currency (From)");
const currTo = await collection.distinct("Currency (To)");
return Array.from(new Set([...currFrom, ...currTo]));
} catch (seeMetaDataErr) {
return { error: seeMetaDataErr };
}
};
const handleLog = ({ cb }) => async ({ db }) => {
const result = await cb({ db });
console.log("------------- Done! -------------");
console.log(result);
console.log("---------------------------------");
return true;
};
const continueCli = async ({ db, dstDir }) => {
return await inquirer
.prompt({
type: "list",
name: "actions",
message: "Choose action:",
loop: false,
choices: [
{
name: "Hydrate spuffordCurrency collection from Rutgers",
disabled: "spuffordCurrency is current as of 2020-01-18",
},
{
name: "Hydrate posthumusPrices collection from Rutgers",
disabled: "posthumusPrices is current as of 2020-01-21",
},
"Get Currency (Spufford) validation schema",
"Get Prices (Posthumus) validation schema",
"Scrape 1 page from Posthumus Price",
"Get distinct Spufford Currency currency names",
"Get distinct Spufford Currency place names",
"Quit",
],
})
.then((answer) => {
let handler = null;
switch (answer.actions) {
case "Hydrate spuffordCurrency collection from Rutgers":
handler = handleLog({
cb: onHydrate({
rutgersTableName: "spuf",
collectionName: "spuffordCurrency",
pagesToSkip: 0,
}),
});
break;
case "Hydrate posthumusPrices collection from Rutgers":
handler = handleLog({
cb: onHydrate({
rutgersTableName: "postpr",
collectionName: "posthumusPrices",
pagesToSkip: 0,
dstDir,
}),
});
break;
case "Scrape 1 page from Posthumus Price":
handler = handleLog({
cb: scrapePageToTextFile({
offset: 1,
rutgersTableName: "postpr",
dstDir,
}),
});
break;
case "Get Currency (Spufford) validation schema":
handler = handleLog({
cb: retrieveValidator({ collectionName: "spuffordCurrency" }),
});
break;
case "Get Prices (Posthumus) validation schema":
handler = handleLog({
cb: retrieveValidator({ collectionName: "posthumusPrices" }),
});
break;
case "Get distinct Spufford Currency place names":
handler = handleLog({ cb: getDistinctPlacesSpuf });
break;
case "Get distinct Spufford Currency currency names":
handler = handleLog({ cb: getDistinctCurrenciesSpuf });
break;
case "Quit":
handler = onQuit;
break;
default:
handler = onQuit;
break;
}
return handler({ db }).then(() => continueCli({ db, dstDir }));
});
};
module.exports = ({ db = null, dstDir }) => {
return inquirer
.prompt(passwordPrompt)
.then(() => continueCli({ db, dstDir }));
};