-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.ts
177 lines (155 loc) · 4.81 KB
/
lib.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
import * as R from "https://x.nest.land/rambda@7.1.4/mod.ts";
import {
HTMLDocument,
DOMParser,
Element,
} from "https://deno.land/x/deno_dom@v0.1.21-alpha/deno-dom-wasm.ts";
export { delay } from "https://deno.land/std@0.136.0/async/mod.ts";
export function random(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.random() * (max - min) + min;
}
const all = Promise.all.bind(Promise);
const parseDOM = (() => {
const it = new DOMParser();
return (source: string): HTMLDocument | null =>
it.parseFromString(source, "text/html");
})();
export async function fetchDOM(href: string) {
// fetch by href
const source = await fetch(href).then((res) => res.text());
if (!source) {
throw new Error(`fetch ${href} response is empty`);
}
// parser source string into DOM
const document = parseDOM(source);
if (!document) {
throw new Error(`failed to parse source into dom`);
}
return document;
}
const hasAttribute = (attribute: string) => (el: Element) =>
el.hasAttribute(attribute);
const getAttribute = (attribute: string) => (el: Element) =>
el.getAttribute(attribute);
const getSearchParam = (href: string, key: string) =>
new URLSearchParams(href).get(key) || undefined;
type HasQuerySelector = HTMLDocument | Element;
function DOM(el?: HasQuerySelector) {
return {
select: (query: string) => el?.querySelector(query) || undefined,
selectAll: (query: string) =>
Array.from(el?.querySelectorAll(query) || []) as Element[],
};
}
const selectText = (query: string) => (el: HTMLDocument) =>
DOM(el).select(query)?.textContent || undefined;
const selectAllText = (query: string) => (el: HTMLDocument) =>
DOM(el)
.selectAll(query)
.map((el) => el.textContent)
.filter(Boolean);
const selectHTML = (query: string) => (el: HTMLDocument) =>
DOM(el).select(query)?.innerHTML || undefined;
const selectHref = (query: string) => (el: HTMLDocument) =>
DOM(el).select(query)?.getAttribute("href") || undefined;
export interface Article {
href: string;
title: string;
content: string;
tags: string[];
genre: string;
publish_at: string;
author_href: string;
series_href: string;
series_no: string;
}
const extractArticle = (href: string) =>
R.applySpec({
href: R.always(href),
genre: selectText(".qa-header .group__badge"),
tags: selectAllText(".qa-header__tagGroup .tag"),
title: selectText(".qa-header .qa-header__title"),
publish_at: selectText(".qa-header .qa-header__info-time"),
content: selectHTML(".qa-markdown .markdown"),
series_no: selectText(
".qa-header .ir-article__topic > .ir-article__topic-count"
),
});
export interface Series {
href: string;
name: string;
}
const extractSeries = R.applySpec({
href: selectHref(".qa-header .ir-article__topic > a"),
name: selectText(".qa-header .ir-article__topic > a"),
});
export interface User {
href: string;
name: string;
}
const extractUser = R.applySpec({
name: selectText(
".ir-article-info .ir-article-info__content .ir-article-info__name"
),
href: selectHref(
".ir-article-info .ir-article-info__content .ir-article-info__name"
),
});
type InsertProxy = {
user: (user: Partial<User>) => Promise<unknown>;
series: (series: Partial<Series>) => Promise<unknown>;
article: (article: Partial<Article>) => Promise<unknown>;
};
export const extract = (document: HTMLDocument, insert: InsertProxy) =>
all(
DOM(document)
// get every href on list
.selectAll(".qa-list__title-link")
.map(getAttribute("href"))
.map((href) => {
if (!href) return;
// extract page information
return fetchDOM(href).then(
R.applySpec({
article: extractArticle(href),
user: extractUser,
series: extractSeries,
})
);
})
)
.then(
R.map(async (information) => {
if (!information) return;
// insert information into database
try {
await insert.user(information.user);
await insert.series(information.series);
await insert.article({
...information.article,
author_href: information.user.href,
series_href: information.series.href,
});
console.log(
`get information from ${information.article.href} successful...`
);
} catch (error) {
console.error(error);
}
})
)
.then(all);
export const getEndLink = (href: string) =>
fetchDOM(href).then((document) =>
DOM(document)
.selectAll(".pagination > li > *")
.filter(hasAttribute("href"))
.map(getAttribute("href"))
.map((href) => ({
href,
page: Number(getSearchParam(href!, "page")),
}))
.reduce((pre, cur) => (cur.page > pre.page ? cur : pre), { page: -1 })
);