/
news.ycombinator.com.ts
68 lines (55 loc) · 2.23 KB
/
news.ycombinator.com.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
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
import agent, { Observable } from 'secret-agent';
// process.env.SA_SHOW_BROWSER = 'true';
async function run() {
await agent.configure({ userAgent: '~ chrome = 88' });
await agent.goto('https://news.ycombinator.com/');
await agent.waitForPaintingStable();
console.log('\n-- PRINTING location.href ---------');
console.log(await agent.url);
const stories = await agent.document.querySelectorAll('.athing');
let lastStory;
for (const story of stories) {
await agent.waitForMillis(200);
const extraElem = await story.nextElementSibling;
await agent.interact({
move: story,
});
const record = Observable({} as any);
agent.output.push(record);
const titleElem = await story.querySelector('a.storylink');
record.score = parseInt(
await extraElem.querySelector('.score').textContent.catch(() => '0'),
10,
);
record.id = await story.getAttribute('id');
record.age = await extraElem.querySelector('.age a').textContent;
record.title = await titleElem.textContent;
const contributor = await extraElem.querySelector('.hnuser').textContent.catch(() => '');
record.contributor = { id: contributor, username: contributor };
const links = [...(await extraElem.querySelectorAll('.subtext > a'))];
const commentsLink = links[links.length - 1];
const commentText = await commentsLink.textContent;
record.commentCount = commentText.includes('comment')
? parseInt(commentText.trim().match(/(\d+)\s/)[0], 10)
: 0;
lastStory = commentsLink;
record.url = await titleElem.getAttribute('href');
}
if (lastStory) {
await agent.click(lastStory);
await agent.waitForLocation('change');
await agent.waitForElement(agent.document.querySelector('textarea'));
await agent.click(agent.document.querySelector('textarea'));
await agent.type('Hackernews!');
const comments = [...(await agent.document.querySelectorAll('.commtext'))];
await agent.interact({
move: comments[comments.length - 1],
});
}
console.log('-- PRINTING extracted results ---------------');
console.log(agent.output);
console.log('-------------------------------------');
console.log('DONE');
await agent.close();
}
run().catch(error => console.log(error));