/
utility.ts
246 lines (196 loc) · 6.55 KB
/
utility.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import 'array-unique-proposal';
import { outputFile } from 'fs-extra';
import { JSDOM } from 'jsdom';
import { join } from 'path';
import { URL } from 'url';
import { promisify } from 'util';
export const logTime = <This, Args extends any[], Return>(
target: (this: This, ...args: Args) => Return,
context: ClassMethodDecoratorContext<
This,
(this: This, ...args: Args) => Return
>
) =>
function (...input: Args) {
const title = context.name.toString();
console.time(title);
const output = target.apply(this, input),
end = () => {
console.log();
console.timeEnd(title);
console.log();
};
if (output instanceof Promise) output.finally(end);
else end();
return output;
};
export function makeDate(raw: string) {
const date = new Date(
((raw || '') + '')
.replace(/\s+(\d+:)/, 'T$1')
.replace(/\.\d{3}Z?/, '')
.replace(/[^\d\-T:]+/g, '-')
.replace(/^-*|-*$/g, '')
);
if (!isNaN(+date)) return date;
}
export function stringifyCSV(list: Record<string, any>[]) {
const header: string[] = [];
const body = list.map(item => {
const row = [];
for (const [key, value] of Object.entries(item)) {
let index = header.indexOf(key);
if (index === -1) index += header.push(key);
row[index] = value;
}
return row;
});
return [header, ...body]
.map(row => row.map(value => JSON.stringify(value)).join(','))
.join('\n');
}
export async function saveFile(
data: string | NodeJS.ArrayBufferView,
...pathParts: string[]
) {
const path = join(...pathParts);
await outputFile(path, data);
console.log(`[save] ${path}`);
return path;
}
export enum CSSSelectorPrecision {
Low,
Medium,
High
}
export function getCSSSelector(
toElement: Element,
fromElement = toElement.getRootNode(),
precision = CSSSelectorPrecision.Low
) {
const selectors: string[] = [];
do {
const { tagName, className, parentNode } = toElement;
const selector =
tagName.toLowerCase() +
(className.trim()
? '.' + className.split(/\s+/).filter(Boolean).join('.')
: precision === CSSSelectorPrecision.High ||
(precision === CSSSelectorPrecision.Medium && !selectors[0])
? `:nth-child(${
[...parentNode.children].indexOf(toElement) + 1
})`
: '');
selectors.unshift(selector);
toElement = parentNode as Element;
} while (fromElement ? fromElement !== toElement : toElement);
return selectors.join(' > ');
}
export function sameParentOf(first: Element, second: Element) {
do {
const { parentNode } = first;
if (parentNode.contains(second)) return parentNode;
first = parentNode as Element;
} while (first);
}
export interface Event {
title: string;
start: Date;
end?: Date;
address?: string;
banner?: URL;
link?: URL;
tags?: string[];
}
/**
* @param source - Web URL or document
* @param list - CSS Selector of Event container
* @param title - CSS Selector of Event title
* @param start - CSS Selector of Event start date
* @param address - CSS Selector of Event address
* @param banner - CSS Selector of Event banner image
* @param link - CSS Selector of Event URL
* @param tags - CSS Selector of Event tags
*/
export async function* eventList(
source: string | JSDOM,
list: string,
title: string,
start: string,
address?: string,
banner?: string,
link?: string,
tags?: string
) {
type EventData = { [key in keyof Event]: string | string[] | URL };
const {
window: { document }
} = typeof source === 'string' ? await JSDOM.fromURL(source) : source;
const group = document.querySelectorAll(list);
if (!group[0]) return;
console.warn(document.URL);
for (const item of group) {
let data: EventData = {
title: item.querySelector(title).textContent.trim(),
start: item.querySelector(start).textContent.trim()
},
_banner_: HTMLImageElement,
_link_: HTMLAnchorElement;
if (address)
data.address = item.querySelector(address)?.textContent.trim();
if (banner && (_banner_ = item.querySelector(banner))) {
const { dataset, src } = _banner_;
for (const key in dataset)
if (dataset[key].startsWith('http')) {
data.banner = new URL(dataset[key]);
break;
}
if (!data.banner) data.banner = new URL(src);
}
if (link && (_link_ = item.querySelector(link))) {
const { searchParams } = (data.link = new URL(_link_.href));
for (const key of searchParams.keys())
if (key.startsWith('utm_')) searchParams.delete(key);
}
if (tags)
data.tags = Array.from(item.querySelectorAll(tags), item =>
item.textContent.trim()
);
yield data;
}
}
export function diffEvent(Old: Event, New: Event) {
const diff: Record<keyof Event, any> = {} as Record<keyof Event, any>;
for (const key of new Set([...Object.keys(Old), ...Object.keys(New)]))
if (['start', 'end'].includes(key)) {
if (new Date(Old[key]) < new Date(New[key])) diff[key] = New[key];
} else if (Old[key]?.length < New[key]?.length) diff[key] = New[key];
for (const key in diff) return diff;
}
export const delay = promisify(setTimeout);
/**
* @param list
* @param sorter - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters
* @param interval - Seconds
*
* @yield Data from `list` of Iterators
*/
export async function* mergeStream<T>(
list: AsyncIterator<T>[],
sorter: (A: T, B: T) => number,
interval?: number
): AsyncGenerator<T> {
const wait = Array(list.length);
while (true) {
for (let i = 0; i < wait.length; i++)
if (wait[i] === undefined) wait[i] = (await list[i].next()).value;
const [top] = wait.filter(item => item != null).sort(sorter);
if (top === undefined) break;
wait[wait.indexOf(top)] = undefined;
yield top;
await delay(interval * 1000);
}
}
export function descendDate({ start: A }: Event, { start: B }: Event) {
return +B - +A;
}