-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembers-api.mjs
77 lines (62 loc) · 1.89 KB
/
members-api.mjs
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
import { Agent, get } from 'https';
import { mkdirSync, writeFileSync } from 'fs';
const agent = new Agent({ keepAlive:true });
const options = {
hostname: 'members-api.parliament.uk',
agent
};
const request = path =>
new Promise((resolve, reject) => {
const req = get({ ...options, path }, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
const response = JSON.parse(data);
resolve(response);
});
});
req.on('error', error => {
reject(error);
});
req.end();
});
const getMembers = (skip) => {
const path = `/api/Members/Search?House=1&IsCurrentMember=true&skip=${skip}`;
return request(path);
}
const getContactDetails = (id) => {
const path = `/api/Members/${id}/Contact`;
return request(path);
}
(async () => {
const allMembers = [];
let skip = 0;
while (true) {
const members = await getMembers(skip);
allMembers.push(...members.items.map(member => ({
id: member.value.id,
name: member.value.nameDisplayAs,
party: member.value.latestParty.name,
constituency: member.value.latestHouseMembership.membershipFrom
})));
if (allMembers.length < members.totalResults) {
skip = allMembers.length;
console.log(skip, members.totalResults);
} else {
break;
}
}
await Promise.all(allMembers.map(async member => {
const contactDetails = await getContactDetails(member.id);
const twitterAccount = contactDetails.value.find(contactDetail => contactDetail.type === 'Twitter');
if (twitterAccount) {
member.twitterUsername = twitterAccount.line1.split("https://twitter.com/")[1];
}
console.log(member.name, member.twitterUsername);
}));
mkdirSync('output', { recursive: true });
writeFileSync('output/members-api.json', JSON.stringify(allMembers, null, 2));
console.log(allMembers);
})();