-
Notifications
You must be signed in to change notification settings - Fork 21
/
grantsgov.js
223 lines (208 loc) · 7.58 KB
/
grantsgov.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
217
218
219
220
221
222
223
/* eslint-disable no-await-in-loop */
/* eslint-disable no-param-reassign */
const got = require('got');
const delay = require('util').promisify(setTimeout);
const BatchProcessor = require('./batchProcessor');
const REQUEST_DELAY = process.env.GRANTS_SCRAPER_DELAY || 500;
const HTTP_TIMEOUT = parseInt(process.env.GRANTS_SCRAPER_HTTP_TIMEOUT, 10) || 20000;
function simplifyString(string) {
return string.toLowerCase().replace(/ /g, '').replace(/[ \-_.;&"']/g, '');
}
async function enrichHitWithDetails(keywords, hit) {
// console.log(`[grantsgov] pulling description page for ${hit.number}`);
const resp = await got.post({
url: 'https://www.grants.gov/grantsws/rest/opportunity/details',
responseType: 'json',
form: {
oppId: hit.id,
},
timeout: HTTP_TIMEOUT,
});
hit.matchingKeywords = [];
if ((resp.body.synopsis || resp.body.forecast) && resp.body.opportunityTitle) {
let desc = null;
if (resp.body.synopsis) {
desc = resp.body.synopsis.synopsisDesc;
hit.description = desc;
hit.awardCeiling = resp.body.synopsis.awardCeiling;
hit.costSharing = resp.body.synopsis.costSharing;
if (resp.body.synopsis.applicantTypes) {
hit.eligibilityCodes = resp.body.synopsis.applicantTypes.map((appl) => appl.id).join(' ');
}
} else {
desc = resp.body.forecast.forecastDesc;
hit.awardCeiling = resp.body.forecast.awardCeiling;
hit.costSharing = resp.body.forecast.costSharing;
if (resp.body.forecast.applicantTypes) {
hit.eligibilityCodes = resp.body.forecast.applicantTypes.map((appl) => appl.id).join(' ');
}
}
if (resp.body.opportunityCategory) {
hit.opportunityCategory = resp.body.opportunityCategory.description;
}
keywords.forEach((kw) => {
if (
simplifyString(desc).includes(simplifyString(kw))
|| simplifyString(resp.body.opportunityTitle).includes(simplifyString(kw))
) {
// console.log(`matches ${kw}`);
hit.matchingKeywords.push(kw);
}
});
hit.rawBody = JSON.stringify(resp.body);
} else {
console.log(`unexpected response: ${resp.body}`);
}
}
async function search(postBody) {
try {
const resp = await got.post({
url: 'https://www.grants.gov/grantsws/rest/opportunities/search/',
json: postBody,
responseType: 'json',
timeout: HTTP_TIMEOUT,
});
if (resp.statusCode < 200 || resp.statusCode >= 300) {
console.log('request failed with code', resp.statusCode);
console.log(resp.body);
}
return resp;
} catch (err) {
console.log('request failed', err);
return null;
}
}
async function getEligibilities() {
const resp = await search({
startRecordNum: 0,
keyword: '',
oppNum: '',
cfda: '',
// oppStatuses: 'posted',
oppStatuses: 'forecasted|posted|closed|archived',
sortBy: 'openDate|desc',
});
const res = {};
if (resp) {
/*
Example raw JSON response for `/search`
"eligibilities": [
{
"label": "City or township governments",
"value": "02",
"count": 1138
},
...
]
*/
resp.body.eligibilities.forEach((item) => {
res[item.value] = item.label;
});
}
/*
Example `result`
{
"02": "City or township governments",
...
}
*/
return res;
}
async function allOpportunities0({ keyword, eligibilities }, pageSize, offset) {
console.log(`searching for ${keyword} @ ${offset}`);
const resp = await search({
startRecordNum: offset,
keyword,
dateRange: process.env.GRANTS_SCRAPER_DATE_RANGE || 56,
eligibilities,
oppNum: '',
cfda: '',
oppStatuses: 'posted|forecasted',
sortBy: 'openDate|desc',
});
if (resp && resp.body && resp.body.oppHits) {
const hits = resp.body.oppHits.filter((hit) => !hit.closeDate || hit.closeDate.match(/202[0-9]$/));
return hits;
}
return [];
}
async function allOpportunities(keywords, eligibilities) {
const results = {};
for (const i in keywords) {
const thisKeyword = keywords[i];
const hits = await allOpportunities0(thisKeyword, eligibilities, 0);
hits.forEach((hit) => {
if (!results[hit.number]) {
results[hit.number] = hit;
results[hit.number].searchKeywords = [];
}
results[hit.number].searchKeywords.push(thisKeyword);
});
}
return Object.values(results);
}
async function processGrants({
keyword, insertKeywords, insertAllKeywords, allKeywords, syncFn,
}, grants) {
const grantsToSynch = [];
// eslint-disable-next-line no-restricted-syntax
for (let grant of grants) {
try {
await delay(REQUEST_DELAY);
await enrichHitWithDetails(allKeywords.slice(0), grant);
grant.searchKeywords = [keyword];
} catch (err) {
console.log(`attempted to enrich grant but failed with ${err}`);
grant = null;
}
if (grant) {
// eslint-disable-next-line max-len
const matchingInsertKeywords = grant.matchingKeywords.filter((kw) => insertKeywords.indexOf(kw) >= 0);
// eslint-disable-next-line max-len
const searchedInsertAllKeywords = grant.searchKeywords.filter((kw) => insertAllKeywords.indexOf(kw) >= 0);
if (matchingInsertKeywords.length > 0 || searchedInsertAllKeywords.length > 0) {
grantsToSynch.push(grant);
}
}
}
await syncFn(grantsToSynch);
const used = process.memoryUsage();
for (const key in used) {
// eslint-disable-next-line no-mixed-operators
console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}
}
async function synchGrants(insertKeywords, insertAllKeywords, allKeywords, eligibilities, syncFn) {
const batchProcessors = [];
// eslint-disable-next-line no-restricted-syntax
for (const keyword of allKeywords) {
const batchProcessor = new BatchProcessor({
pageSize: 10,
offset: 0,
runOnce: true,
sleepMs: REQUEST_DELAY,
fetchRecordsFn: allOpportunities0,
processRecordsFn: processGrants,
context: {
keyword,
eligibilities,
insertKeywords,
insertAllKeywords,
allKeywords,
syncFn,
},
});
batchProcessors.push(batchProcessor.start());
}
return Promise.all(batchProcessors);
}
// previous hits is array of [{id, number}]
// all previous hits are always checked for updates to keywords
async function allOpportunitiesOnlyMatchDescription(previousHits, keywords, eligibilities, syncFn) {
const insertKeywords = keywords.filter((v) => v.insertMode).map((v) => v.term);
// eslint-disable-next-line max-len
const insertAllKeywords = keywords.filter((v) => v.insertMode && v.insertAll).map((v) => v.term);
const allKeywords = keywords.map((v) => v.term);
await synchGrants(insertKeywords, insertAllKeywords, allKeywords, eligibilities, syncFn);
}
module.exports = { allOpportunities, allOpportunitiesOnlyMatchDescription, getEligibilities };