This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetUtils.js
44 lines (39 loc) · 1.48 KB
/
tweetUtils.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
'use latest';
import getRecentComments from './regulations-gov-api';
import analyze from './analyzer';
const maxTweetLength = 140;
const shortUrlLength = 23;
const tweetLength = maxTweetLength - shortUrlLength - 1;
const formatTweet = (pullQuote) => `${pullQuote}`;
const getDocuments = (apiKey, docketId, backlogCount) =>
getRecentComments(apiKey, docketId, backlogCount)
.then(documents => documents.map((document) => {
const analysis = analyze(document.commentText);
return {
documentId: document.documentId,
commentText: document.commentText,
tweet: formatTweet(analysis.pullQuote.sentence),
targetUrl: `https://www.regulations.gov/document?D=${document.documentId}`,
score: analysis.pullQuote.score,
hash: analysis.hash,
};
}));
export default (apiKey, docketId, backlogCount, excludedHashes) =>
getDocuments(apiKey, docketId, backlogCount)
.then(documents => documents.reduce((o, d) => {
o[d.hash] = o[d.hash] || d;
return o;
}, {}))
.then(obj => Object.keys(obj)
.map(k => obj[k])
.filter(document => !excludedHashes[document.hash])
.filter(document => document.tweet.length < tweetLength))
.then(documents => documents.length ? documents : Promise.reject({
type: 'no-comments',
}))
.then(documents => documents.reduce((l, curr) => {
const last = l || curr;
return last.score < curr.score
? last
: (last.tweet.length < curr.tweet.length ? curr : last);
}))