- headless.js +157 -0
headless.js
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import puppeteer from 'puppeteer';
|
2 |
+
import tough from 'tough-cookie';
|
3 |
+
import iconv from 'iconv-lite';
|
4 |
+
import {load} from 'cheerio';
|
5 |
+
import path from 'path';
|
6 |
+
import fs from 'fs-extra';
|
7 |
+
import dotenv from 'dotenv';
|
8 |
+
import { execSync } from 'child_process';
|
9 |
+
import {join} from 'path';
|
10 |
+
import {jar, chineseToUnicode, downloadFile, gb2312UrlEncode, get, post, parseCookies, reset_cookie_jar} from './utils.js';
|
11 |
+
|
12 |
+
dotenv.config();
|
13 |
+
|
14 |
+
const host = 'https://mmis.hkpl.gov.hk'
|
15 |
+
async function get_list(page) {
|
16 |
+
const url = `${host}/search-result?p_p_id=search_WAR_mmisportalportlet&p_p_lifecycle=0&p_p_state=normal&_search_WAR_mmisportalportlet_keywords=%E5%B7%A5%E5%95%86%E6%99%9A%E5%A0%B1&_search_WAR_mmisportalportlet_hsf=%E5%B7%A5%E5%95%86%E6%99%9A%E5%A0%B1&p_r_p_-1078056564_actual_q=%28%20all_dc.title%3A%28%22%E5%B7%A5%E5%95%86%E6%99%9A%E5%A0%B1%22%29%29%20AND+%28%20verbatim_dc.collection%3A%28%22Old%5C%20HK%5C%20Newspapers%22%29%29&p_r_p_-1078056564_sort_field=dc.publicationdate_bsort&_search_WAR_mmisportalportlet_formDate=1448775486359&_search_WAR_mmisportalportlet_mode=Advanced&p_r_p_-1078056564_curr_page=${page}&p_r_p_-1078056564_new_search=true&p_r_p_-1078056564_sort_order=desc&p_r_p_-1078056564_filter=&_search_WAR_mmisportalportlet_jspPage=%2Fjsp%2Fsearch%2Fcnsc05.jsp`;
|
17 |
+
let html = "";
|
18 |
+
const dir = `./html/list`;
|
19 |
+
const f = join(dir, page + ".html");
|
20 |
+
if (await fs.exists(f)) {
|
21 |
+
console.log("exists", f);
|
22 |
+
html = (await fs.readFile(f)).toString();
|
23 |
+
} else {
|
24 |
+
while (true) {
|
25 |
+
const { data } = await get(url);
|
26 |
+
if (data.indexOf("The system is busy. Please retry later") != -1) {
|
27 |
+
continue;
|
28 |
+
}
|
29 |
+
html = data;
|
30 |
+
const a = html.indexOf("<body");
|
31 |
+
const b = html.indexOf("</body>");
|
32 |
+
html = html.substring(a, b) + "</body>";
|
33 |
+
await fs.writeFile(f, html);
|
34 |
+
break;
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
const links = load(html)(".result-title a");
|
39 |
+
const inputs = load(html)(".aui-button-input.mmis-button-full");
|
40 |
+
await fs.ensureDir(dir);
|
41 |
+
const res = [];
|
42 |
+
let x = 0;
|
43 |
+
for (const link of links) {
|
44 |
+
res.push({
|
45 |
+
href: link.attribs.href,
|
46 |
+
text: link.children[0].data,
|
47 |
+
id: inputs[x].attribs.onclick.split('ITEM_ID=')[1].split('&')[0],
|
48 |
+
});
|
49 |
+
++x;
|
50 |
+
}
|
51 |
+
return res;
|
52 |
+
}
|
53 |
+
|
54 |
+
function page_transform(year, month, day, page) {
|
55 |
+
if (year == 1984 && month == 10 && day == 9 && page == 12)
|
56 |
+
return page.toString().padStart(3, "0")
|
57 |
+
return page.toString().padStart(2, "0")
|
58 |
+
}
|
59 |
+
async function crawl_by_page(page) {
|
60 |
+
const links = await get_list(page);
|
61 |
+
for (const link of links) {
|
62 |
+
const date_str = link.text.split(" ")[1];
|
63 |
+
let [year, month, day] = date_str.split("-");
|
64 |
+
month = parseInt(month);
|
65 |
+
day = parseInt(day);
|
66 |
+
year = parseInt(year);
|
67 |
+
const dir = join(`./html/newspaper/${year}/${month}/${day}`);
|
68 |
+
const f = join(dir, "index.html");
|
69 |
+
let detail_html = "";
|
70 |
+
if (await fs.exists(f)) {
|
71 |
+
console.log("exists", f);
|
72 |
+
detail_html = (await fs.readFile(f)).toString();
|
73 |
+
} else {
|
74 |
+
await fs.ensureDir(dir);
|
75 |
+
while (true) {
|
76 |
+
const { data: html } = await get(`${host}${link.href}`);
|
77 |
+
if (html.indexOf("The system is busy. Please retry later") != -1) continue;
|
78 |
+
await fs.writeFile(f, html);
|
79 |
+
detail_html = html;
|
80 |
+
break;
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
const key = 'htmlURL="';
|
85 |
+
const pivot = detail_html.indexOf(key);
|
86 |
+
const viewer_url = `${host}${detail_html.substring(
|
87 |
+
pivot + key.length,
|
88 |
+
detail_html.indexOf('"', pivot + key.length)
|
89 |
+
)}`;
|
90 |
+
const key2 = "initPageLinks(null,";
|
91 |
+
const pivot2 = detail_html.indexOf(key2);
|
92 |
+
const total_page = parseInt(
|
93 |
+
detail_html.substring(
|
94 |
+
pivot2 + key2.length,
|
95 |
+
detail_html.indexOf(",", pivot2 + key2.length)
|
96 |
+
)
|
97 |
+
);
|
98 |
+
|
99 |
+
const search_params = new URLSearchParams("?" + viewer_url.split("?")[1]);
|
100 |
+
|
101 |
+
// fid may mismatch year_month_day. fid is correct.
|
102 |
+
const fid = detail_html.substring(detail_html.indexOf('Bib ID')).split('value-content " > <span>')[1].split('<')[0];
|
103 |
+
|
104 |
+
for (let i = 1; i <= total_page; ++i) {
|
105 |
+
console.log("download img", year, month, day, i, "/", total_page);
|
106 |
+
const img_path = join(dir, i + ".png");
|
107 |
+
if (await fs.exists(img_path)) {
|
108 |
+
console.log("skip");
|
109 |
+
continue;
|
110 |
+
}
|
111 |
+
|
112 |
+
// const encToken = search_params.get("encToken");
|
113 |
+
const encToken =
|
114 |
+
'';
|
115 |
+
const img_url =
|
116 |
+
`https://mmis.hkpl.gov.hk/ebook/viewer?resource=page` +
|
117 |
+
`&item=${search_params.get("item")}` +
|
118 |
+
// `&item=${link.id}` +
|
119 |
+
`&type=` +
|
120 |
+
`&ref=%2FMULTI_IMAGE_INDEX%2F${
|
121 |
+
fid.substring(fid.length - 4)
|
122 |
+
}%2F${
|
123 |
+
fid
|
124 |
+
}%2Fimage%2F${
|
125 |
+
fid
|
126 |
+
}_${
|
127 |
+
page_transform(year, month, day, i)
|
128 |
+
}.png` +
|
129 |
+
`&encToken=${encodeURIComponent(encToken)}`;
|
130 |
+
// console.log(img_url);
|
131 |
+
await downloadFile(img_url, img_path, Infinity);
|
132 |
+
}
|
133 |
+
// const {data: viewer_html} = await get(viewer_url);
|
134 |
+
// console.log(viewer_html);
|
135 |
+
// return;
|
136 |
+
}
|
137 |
+
}
|
138 |
+
|
139 |
+
|
140 |
+
(async () => {
|
141 |
+
const pages = Math.floor(16191 / 10);
|
142 |
+
const arr = (new Array(pages)).fill(0).map((_, i) => i+1);//.sort(() => Math.random() > 0.5 ? 1 : -1);
|
143 |
+
/*
|
144 |
+
for (let i = 1; i <= pages; ++i) {
|
145 |
+
// console.log(i + "/" + pages);
|
146 |
+
crawl_by_page(i);
|
147 |
+
}
|
148 |
+
*/
|
149 |
+
await Promise.all[
|
150 |
+
new Array(10).fill(0).map(async (i) => {
|
151 |
+
while (arr.length) {
|
152 |
+
console.log(arr.length);
|
153 |
+
await crawl_by_page(arr.pop());
|
154 |
+
}
|
155 |
+
})
|
156 |
+
];
|
157 |
+
})();
|