/
get-user-info.ts
39 lines (31 loc) · 953 Bytes
/
get-user-info.ts
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
import * as numeral from 'numeral';
import { GetUserInfo } from 'src/common/interfaces';
const getUserInfo: GetUserInfo = async function getUserInfo(username) {
return this.getPage(`/${username}`, async (page) => {
const [posts, followers, following] = await page.evaluate(() => {
const { scraper } = window as any;
return scraper
.find({
selector: 'li span',
})
.slice(1)
.map(el => el.text());
});
const isFollowed = await page.evaluate(() => {
const { scraper } = window as any;
const followButton = scraper.findOneWithText({ selector: 'button', text: 'Follow' });
if (followButton) {
return false;
}
return true;
});
return {
isFollowed,
username,
following: numeral(following).value(),
followers: numeral(followers).value(),
posts: numeral(posts).value(),
};
});
};
export { getUserInfo };