-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
139 lines (109 loc) · 3.44 KB
/
index.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Browser } from 'puppeteer';
import { Platform, Release, ReleaseListItem, ReleaseType } from './types';
import TurndownService from 'turndown';
const REGEX_TITLE_CONTROLLER = /Omada (\w+) Controller_V([0-9.]+(?:\sBeta)?)[_|\s]?(\w+)?/i;
const REGEX_TITLE_RELEASE_DATE = /(?:Released|Updated) on ([A-Za-z0-9\s,]+)/i;
function getReleaseType(text: string): ReleaseType {
switch (text.trim().toLowerCase()) {
case 'sdn':
case 'software': {
return 'software';
}
case 'hardware': {
return 'hardware';
}
default: {
return 'unknown';
}
}
}
function getPlatform(text: string): Platform {
switch (text.trim().toLowerCase()) {
case 'windows': {
return 'windows';
}
case 'linux': {
return 'linux';
}
default: {
return 'unknown';
}
}
}
function parseDate(text: string) {
const parsedDate = Date.parse(text);
if (!isNaN(parsedDate)) {
return new Date(text).toISOString();
}
const textPatched = text.replace(/(\d+)(?:st|nd|rd|th)/, (match, p1) => {
return p1;
});
return new Date(textPatched).toISOString();
}
export default async function controller(browser: Browser): Promise<Release[]> {
const turndownService = new TurndownService();
const page = await browser.newPage();
await page.goto(
'https://community.tp-link.com/en/business/forum/582?tagId=684,854'
);
const releaseListItems = await page.evaluate(() => {
const results: ReleaseListItem[] = [];
const topicListItemElements = Array.from(
document.querySelectorAll('.topic-list .item-wrap')
);
for (const topicListItemElement of topicListItemElements) {
const title =
topicListItemElement.querySelector('.title-wrap a')?.textContent ??
null;
const link =
// @ts-ignore
topicListItemElement.querySelector('.title-wrap a')?.href ?? null;
const summary =
topicListItemElement.querySelector('.text-wrap')?.textContent ?? null;
if (title == null || link == null || summary == null) {
continue;
}
results.push({
title,
link,
summary,
});
}
return results;
});
const releases: Release[] = [];
for (const releaseListItem of releaseListItems) {
const { title, summary, link } = releaseListItem;
const releaseDateMatches = title.match(REGEX_TITLE_RELEASE_DATE);
if (releaseDateMatches == null || releaseDateMatches.length <= 1) {
console.error(`could not match release date for '${title}'`);
continue;
}
const controllerMatches = title.match(REGEX_TITLE_CONTROLLER);
if (controllerMatches == null || controllerMatches.length < 3) {
continue;
}
console.error(`working on '${title}'`);
await page.goto(link);
const postBody = await page.evaluate(() => {
return document.querySelector('.topic-content').innerHTML;
});
const release: Release = {
body: turndownService.turndown(postBody),
body_html: postBody,
link,
summary,
type: getReleaseType(controllerMatches[1]),
version: controllerMatches[2],
platform: getPlatform(controllerMatches[3] ?? ''),
date: parseDate(releaseDateMatches[1]),
};
releases.push(release);
await new Promise<void>((resolve) => {
const waitTime = Math.floor(Math.random() * 10000) + 1;
console.log('waiting for', waitTime, 'ms');
setTimeout(resolve, waitTime);
});
}
return releases;
}