-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·231 lines (224 loc) · 7.45 KB
/
main.js
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
const superagent = require("superagent");
const { JSDOM } = require("jsdom");
const dayjs = require("dayjs");
const { Feed } = require("feed");
const fs = require("fs");
var expandTilde = require("expand-tilde");
var customParseFormat = require("dayjs/plugin/customParseFormat");
var utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
dayjs.extend(customParseFormat);
require("superagent-cache")(superagent);
const showError = (e) => {
if (e) {
console.error(e);
}
};
const UA =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
superagent
.get("http://www.fanxinzhui.com/lastest")
.set("User-Agent", UA)
.set("Referer", "https://www.fanxinzhui.com/list")
.then(
(res) => {
const { document } = new JSDOM(res.text).window;
var metas = [];
document
.querySelectorAll(".lastest_update div.list a.la")
.forEach((a) => {
var url = a.href;
if (url.indexOf("//") == -1) {
url = "http://www.fanxinzhui.com" + url;
}
metas.push({
url: url,
season: a.querySelector(".season").textContent,
name: a.querySelector(".name").textContent,
time: dayjs
.utc(a.querySelector(".time").textContent, "YYYY-MM-DD HH:mm")
.toDate(),
});
});
Promise.all(metas.map(crawlDrama))
.then((updates) => {
console.log("all done");
const author = {
name: "追新番",
link: "http://www.fanxinzhui.com/",
email: "[email protected]",
};
const feed = new Feed({
title: "追新番",
description: "追新番最新更新",
id: "http://www.fanxinzhui.com/",
link: "http://www.fanxinzhui.com/",
image: "http://www.fanxinzhui.com/res/logo.png",
generator: "Zhuixinfan updater",
feedLinks: {
json: "https://etng.github.io/fanxinzhui_feed/fanxinzhui.json",
atom: "https://etng.github.io/fanxinzhui_feed/fanxinzhui.atom",
rss: "https://etng.github.io/fanxinzhui_feed/fanxinzhui.rss",
},
author: author,
});
updates.forEach((post) => {
feed.addItem({
title: post.title,
id: post.url,
link: post.url,
description: post.description,
content: post.content,
author: [author],
contributor: [author],
date: post.date,
image: post.image,
});
});
feed.addCategory("TV");
feed.addCategory("Jpanese");
feed.addContributor(author);
fs.mkdirSync(expandTilde("data/dist/"), {
recursive: true,
});
// RSS 2.0
fs.writeFile(
expandTilde("data/dist/fanxinzhui.rss"),
feed.rss2(),
showError
);
// Atom 1.0
fs.writeFile(
expandTilde("data/dist/fanxinzhui.atom"),
feed.atom1(),
showError
);
// JSON Feed 1.0
fs.writeFile(
expandTilde("data/dist/fanxinzhui.json"),
feed.json1(),
showError
);
fs.writeFile(
expandTilde("data/dist/index.htm"),
"<h1>404 Not Found!</h1>",
showError
);
fs.writeFile(
expandTilde("data/dist/README.md"),
"[Subscribe](https://etng.github.io/fanxinzhui_feed/fanxinzhui.rss)",
showError
);
})
.catch((failed_items) => {
console.log("some faileds", failed_items);
});
},
(err) => {
console.error(err);
}
);
function nl2p(lines) {
return (
"<p>" +
lines
.trim()
.replace(/\n{2,}/g, "</p><p>")
.replace(/\n/g, "<br>") +
"</p>"
);
}
function crawlDrama(meta) {
return new Promise((resolve, reject) => {
superagent
.get(meta.url)
.set("User-Agent", UA)
.then(
(res) => {
const { document } = new JSDOM(res.text).window;
var drama = {
title: document.querySelector(".resource_title h2").textContent,
cover: document
.querySelector(".resource_content div.image a")
.getAttribute("href"),
meta: {},
intro: document.querySelector(".resource_intro .intro_text")
.textContent,
episodes: [],
};
document
.querySelectorAll(".resource_content div.info ul li")
.forEach((li) => {
var mtm = li.querySelector("span");
var mt = "cast";
if (mtm) {
mt = mtm.textContent;
mtm.remove();
}
mt = mt.replace(/:+$/g, "");
drama.meta[mt] = li.textContent;
});
var metaLines = Object.keys(drama.meta).map((k) => {
return `${k}: ${drama.meta[k]}`;
});
metaLines.push(drama.intro);
var description = nl2p(metaLines.join("\n\n"));
document
.querySelectorAll(".resource_item ul.item_list li")
.forEach((item) => {
var ep = { links: [] };
var p = item.querySelector("p:first-child");
ep.id = p.querySelector(".season").textContent;
ep.title = p.querySelector(".item").textContent;
item.querySelectorAll("p.way span").forEach((span) => {
ep.links.push({
url: span.querySelector("a").getAttribute("href"),
password: span.querySelector("a:nth-child(2)").textContent,
});
});
if (meta.season == ep.id && meta.name == ep.title) {
ep.time = meta.time;
ep.url = meta.url;
var contentLines = ep.links.map((line) => {
var l = "";
var href = line.url;
if (
line.url.indexOf("pan.baidu.com") > 0 &&
line.url.indexOf("?pwd=") < 0
) {
if (line.password) {
href = line.url + "?pwd=" + line.password;
}
}
if (line.url) {
l += `<a href="${href}" target="_blank">${line.url}</a>\r\n`;
}
if (line.password) {
l += `<span> 密码: </span><span>${line.password}</span>`;
}
return l;
});
contentLines.push("\n\n\n\n" + description);
// contentLines.push("generated by FanXinZhui updater")
var img = "";
if (drama.cover) {
img = `<img src="${drama.cover}" referrerpolicy="no-referrer" />`;
}
resolve({
title: `${drama.title} ${meta.season}`,
date: meta.time,
url: `${meta.url}#${meta.season}`,
description: description,
image: drama.cover,
content: img + nl2p(contentLines.join("\r\n").trim()),
});
}
drama.episodes.push(ep);
});
},
(err) => {
reject(err);
}
);
});
}