-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
94 lines (82 loc) · 2.75 KB
/
utils.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
import { groupBy, maxBy, orderBy, get, set as _set, union } from "lodash";
import pThrottle from "p-throttle";
import logger from "./logger";
import boa from "@pipcook/boa";
import differenceInDays from "date-fns/differenceInDays";
import parseISO from "date-fns/parseISO";
const { list } = boa.builtins();
const networkx = boa.import("networkx");
const json = boa.import("jsonpickle");
const louvain = boa.import("community");
import { getConnectedFriends, makeDistinctList, writeListDB } from "./queries";
import { RATE_LIMITS, CACHE_DAYS } from "./globals";
import Twit from "twit";
export const j = v =>
JSON.parse(
json.encode(v, boa.kwargs({ unpicklable: false, make_refs: false }))
);
export const createPerson = token => {
const tClient = new Twit({
consumer_key: process.env.TWITTER_ID,
consumer_secret: process.env.TWITTER_SECRET,
access_token: token.accessToken,
access_token_secret: token.refreshToken
});
return {
...token,
screen_name: token.screenName,
profile_image_url_https: token.image,
id_str: token.id,
tClient,
lists: {},
throttle: {}
};
};
export const getThrottle = (person, path, method) => {
if (!get(person, `throttle.${path}.${method}`)) {
const rateLimit = RATE_LIMITS[path][method];
const throttleDefault = pThrottle({ limit: 1, interval: rateLimit });
_set(person, `throttle.${path}.${method}`, throttleDefault);
}
return person.throttle[path][method];
};
export const nx = (d, account) => {
const G = networkx.Graph();
G.add_nodes_from(Object.keys(d.nodes));
G.add_edges_from(d.links.map(l => [String(l.source), String(l.target)]));
getClusters(G, d, account);
};
export const makeLists = async account => {
const connectedFriends = await getConnectedFriends(account.id_str);
const distinctFriends = await makeDistinctList(account.id_str);
const getClusters = nx(connectedFriends, account);
};
const getClusters = (G, d, account) => {
let nodesClusterMap = j(louvain.best_partition(G));
let clusters = orderBy(
Object.values(
groupBy(Object.keys(nodesClusterMap), node => nodesClusterMap[node])
),
"length",
"desc"
);
let clusterCenter = {};
// Change clusters length to 10
if (clusters.length > 10) {
const others = clusters.splice(5);
clusterCenter["listtweet-others"] = union(...others);
}
clusterCenter = clusters.reduce((acc, c) => {
const centerNode = maxBy(c, n => j(list(G.neighbors(n))).length);
acc[`listtweet-${d.nodes[centerNode].screenName}`] = c;
return acc;
}, clusterCenter);
writeListDB(clusterCenter, account);
};
export const networkCached = timestamp => {
if (!timestamp) return false;
return (
differenceInDays(parseISO(new Date().toISOString()), parseISO(timestamp)) <
CACHE_DAYS
);
};