- .gitattributes +19 -0
- .gitignore +2 -0
- headless.js +164 -0
- package-lock.json +3 -0
- package.json +3 -0
- utils.js +124 -0
.gitattributes
CHANGED
@@ -51,5 +51,24 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
51 |
*.tiff filter=lfs diff=lfs merge=lfs -text
|
52 |
# Image files - compressed
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
*.tiff filter=lfs diff=lfs merge=lfs -text
|
52 |
# Image files - compressed
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
+
*.JPG filter=lfs diff=lfs merge=lfs -text
|
55 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
56 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
57 |
+
*. filter=lfs diff=lfs merge=lfs -text
|
58 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
59 |
+
*.FPT filter=lfs diff=lfs merge=lfs -text
|
60 |
+
*.dbf filter=lfs diff=lfs merge=lfs -text
|
61 |
+
*.cdx filter=lfs diff=lfs merge=lfs -text
|
62 |
+
*.CDX filter=lfs diff=lfs merge=lfs -text
|
63 |
+
*.epub filter=lfs diff=lfs merge=lfs -text
|
64 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
65 |
+
*.html filter=lfs diff=lfs merge=lfs -text
|
66 |
+
*.htm filter=lfs diff=lfs merge=lfs -text
|
67 |
+
*.mht filter=lfs diff=lfs merge=lfs -text
|
68 |
+
*.mhtml filter=lfs diff=lfs merge=lfs -text
|
69 |
+
*.doc filter=lfs diff=lfs merge=lfs -text
|
70 |
+
*.docm filter=lfs diff=lfs merge=lfs -text
|
71 |
+
*.docx filter=lfs diff=lfs merge=lfs -text
|
72 |
+
*.TIF filter=lfs diff=lfs merge=lfs -text
|
73 |
+
*.chm filter=lfs diff=lfs merge=lfs -text
|
74 |
+
*.csv filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
node_modules
|
2 |
+
cookies.json
|
headless.js
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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%A4%A7%E5%85%AC%E5%A0%B1&_search_WAR_mmisportalportlet_hsf=%E5%A4%A7%E5%85%AC%E5%A0%B1&p_r_p_-1078056564_actual_q=%28%20all_dc.title%3A%28%22%E5%A4%A7%E5%85%AC%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_mode=Advanced&p_r_p_-1078056564_curr_page=${page}&_search_WAR_mmisportalportlet__advancedsearch_WAR_mmisportalportlet_formDate=1389840730864&p_r_p_-1078056564_new_search=false&p_r_p_-1078056564_sort_order=desc&_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 (
|
21 |
+
await fs.exists(f) &&
|
22 |
+
(await fs
|
23 |
+
.readFile(f))
|
24 |
+
.toString()
|
25 |
+
.indexOf("The system is busy. Please retry later") != -1
|
26 |
+
) {
|
27 |
+
await fs.unlink(f);
|
28 |
+
}
|
29 |
+
if (await fs.exists(f)) {
|
30 |
+
console.log("exists", f);
|
31 |
+
html = (await fs.readFile(f)).toString();
|
32 |
+
} else {
|
33 |
+
while (true) {
|
34 |
+
const { data } = await get(url);
|
35 |
+
if (data.indexOf("The system is busy. Please retry later") != -1) {
|
36 |
+
continue;
|
37 |
+
}
|
38 |
+
html = data;
|
39 |
+
const a = html.indexOf("<body");
|
40 |
+
const b = html.indexOf("</body>");
|
41 |
+
html = html.substring(a, b) + "</body>";
|
42 |
+
const links = load(html)(".result-title a");
|
43 |
+
await fs.ensureDir(dir);
|
44 |
+
if (!links.length) {
|
45 |
+
return [];
|
46 |
+
}
|
47 |
+
await fs.writeFile(f, html);
|
48 |
+
break;
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
const links = load(html)(".result-title a");
|
53 |
+
const res = [];
|
54 |
+
for (const link of links) {
|
55 |
+
res.push({
|
56 |
+
href: link.attribs.href,
|
57 |
+
text: link.children[0].data,
|
58 |
+
});
|
59 |
+
}
|
60 |
+
return res;
|
61 |
+
}
|
62 |
+
|
63 |
+
async function crawl_by_page(page) {
|
64 |
+
const links = await get_list(page);
|
65 |
+
for (const link of links) {
|
66 |
+
const date_str = link.text.split(" ")[1];
|
67 |
+
let [year, month, day] = date_str.split("-");
|
68 |
+
month = parseInt(month);
|
69 |
+
day = parseInt(day);
|
70 |
+
year = parseInt(year);
|
71 |
+
const dir = join(`./html/newspaper/${year}/${month}/${day}`);
|
72 |
+
const f = join(dir, "index.html");
|
73 |
+
let detail_html = "";
|
74 |
+
if ((await fs.exists(f)) && (await fs.readFile(f)).toString().indexOf('The system is busy. Please retry later') != -1) {
|
75 |
+
await fs.unlink(f);
|
76 |
+
}
|
77 |
+
if (await fs.exists(f)) {
|
78 |
+
console.log("exists", f);
|
79 |
+
detail_html = (await fs.readFile(f)).toString();
|
80 |
+
} else {
|
81 |
+
await fs.ensureDir(dir);
|
82 |
+
while (true) {
|
83 |
+
const { data: html } = await get(`${host}${link.href}`);
|
84 |
+
if (html.indexOf("The system is busy. Please retry later") != -1) continue;
|
85 |
+
await fs.writeFile(f, html);
|
86 |
+
detail_html = html;
|
87 |
+
break;
|
88 |
+
}
|
89 |
+
}
|
90 |
+
|
91 |
+
const key = 'htmlURL="';
|
92 |
+
const pivot = detail_html.indexOf(key);
|
93 |
+
const viewer_url = `${host}${detail_html.substring(
|
94 |
+
pivot + key.length,
|
95 |
+
detail_html.indexOf('"', pivot + key.length)
|
96 |
+
)}`;
|
97 |
+
const key2 = "initPageLinks(null,";
|
98 |
+
const pivot2 = detail_html.indexOf(key2);
|
99 |
+
const total_page = parseInt(
|
100 |
+
detail_html.substring(
|
101 |
+
pivot2 + key2.length,
|
102 |
+
detail_html.indexOf(",", pivot2 + key2.length)
|
103 |
+
)
|
104 |
+
);
|
105 |
+
|
106 |
+
const search_params = new URLSearchParams("?" + viewer_url.split("?")[1]);
|
107 |
+
|
108 |
+
const month_pad_start = month.toString().padStart(2, "0");
|
109 |
+
const day_pad_start = day.toString().padStart(2, "0");
|
110 |
+
for (let i = 1; i <= total_page; ++i) {
|
111 |
+
// console.log("download img", year, month, day, i, "/", total_page);
|
112 |
+
const img_path = join(dir, i + ".png");
|
113 |
+
if (await fs.exists(img_path)) {
|
114 |
+
if ((await fs.readFile(img_path)).toString().indexOf('The system is busy. Please retry later') != -1) {
|
115 |
+
await fs.unlink(img_path);
|
116 |
+
} else {
|
117 |
+
// console.log("skip");
|
118 |
+
continue;
|
119 |
+
}
|
120 |
+
}
|
121 |
+
|
122 |
+
// const encToken = search_params.get("encToken");
|
123 |
+
const encToken =
|
124 |
+
'...';
|
125 |
+
const img_url =
|
126 |
+
`https://mmis.hkpl.gov.hk/ebook/viewer?resource=page` +
|
127 |
+
`&item=${search_params.get("item")}` +
|
128 |
+
`&type=` +
|
129 |
+
`&ref=%2FMULTI_IMAGE_INDEX%2F${
|
130 |
+
month_pad_start + day_pad_start
|
131 |
+
}%2FNPTKP${
|
132 |
+
year.toString() + month_pad_start + day_pad_start
|
133 |
+
}%2Fimage%2FNPTKP${
|
134 |
+
year.toString() + month_pad_start + day_pad_start
|
135 |
+
}_${i.toString().padStart(2, "0")}.png` +
|
136 |
+
`&encToken=${encodeURIComponent(encToken)}`;
|
137 |
+
// console.log(img_url);
|
138 |
+
await downloadFile(img_url, img_path, Infinity);
|
139 |
+
}
|
140 |
+
// const {data: viewer_html} = await get(viewer_url);
|
141 |
+
// console.log(viewer_html);
|
142 |
+
// return;
|
143 |
+
}
|
144 |
+
}
|
145 |
+
|
146 |
+
|
147 |
+
(async () => {
|
148 |
+
const pages = Math.floor(16865 / 10);
|
149 |
+
const arr = (new Array(pages)).fill(0).map((_, i) => i+1);//.sort(() => Math.random() > 0.5 ? 1 : -1);
|
150 |
+
/*
|
151 |
+
for (let i = 1; i <= pages; ++i) {
|
152 |
+
// console.log(i + "/" + pages);
|
153 |
+
crawl_by_page(i);
|
154 |
+
}
|
155 |
+
*/
|
156 |
+
await Promise.all[
|
157 |
+
new Array(10).fill(0).map(async (i) => {
|
158 |
+
while (arr.length) {
|
159 |
+
console.log(arr.length);
|
160 |
+
await crawl_by_page(arr.pop());
|
161 |
+
}
|
162 |
+
})
|
163 |
+
];
|
164 |
+
})();
|
package-lock.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0a217ba73de053d220700bb1bcaa62c1a685a7642ce1bc6bb18ec782e5151f67
|
3 |
+
size 80975
|
package.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:feb3a49ea4cf6a20daf759a9f47f7f33233589cf3537ccca7c84316cd3525638
|
3 |
+
size 803
|
utils.js
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import axios from 'axios';
|
2 |
+
import fetch from 'node-fetch';
|
3 |
+
import iconv from 'iconv-lite';
|
4 |
+
import fs from 'fs-extra';
|
5 |
+
import { wrapper } from 'axios-cookiejar-support';
|
6 |
+
import { CookieJar, Cookie } from 'tough-cookie';
|
7 |
+
import {SocksProxyAgent} from 'socks-proxy-agent';
|
8 |
+
const proxyAgent = new SocksProxyAgent(`socks5://127.0.0.1:9909`);
|
9 |
+
|
10 |
+
export const jar = fs.existsSync("./cookies.json")
|
11 |
+
? CookieJar.fromJSON(JSON.parse(fs.readFileSync("./cookies.json").toString()))
|
12 |
+
: new CookieJar();
|
13 |
+
jar.rejectPublicSuffixes = false;
|
14 |
+
jar.looseMode = true;
|
15 |
+
jar.prefixSecurity ='unsafe-disabled';
|
16 |
+
jar.allowSpecialUseDomain = true;
|
17 |
+
|
18 |
+
const client = wrapper(axios.create({ jar }));
|
19 |
+
|
20 |
+
export function reset_cookie_jar() {
|
21 |
+
jar.removeAllCookiesSync();
|
22 |
+
}
|
23 |
+
|
24 |
+
client.interceptors.request.use(function (config) {
|
25 |
+
if (config.method === 'get') {
|
26 |
+
console.log('Get ', config.url);
|
27 |
+
} else if (config.method === 'post') {
|
28 |
+
console.log('Post ', config.url, config.data);
|
29 |
+
}
|
30 |
+
config.headers['User-Agent']='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';
|
31 |
+
config.maxRedirects = 5;
|
32 |
+
|
33 |
+
return config;
|
34 |
+
}, function (error) {
|
35 |
+
return Promise.reject(error);
|
36 |
+
});
|
37 |
+
|
38 |
+
export function gb2312UrlEncode(str) {
|
39 |
+
const encoded = iconv.encode(str, 'GB2312');
|
40 |
+
let result = '';
|
41 |
+
for (let i = 0; i < encoded.length; i++) {
|
42 |
+
result += `%${encoded[i].toString(16)}`;
|
43 |
+
}
|
44 |
+
return result.toUpperCase();
|
45 |
+
|
46 |
+
}
|
47 |
+
|
48 |
+
export function chineseToUnicode(str, toUpper = false) {
|
49 |
+
let result = "";
|
50 |
+
for (let i = 0; i < str.length; i++) {
|
51 |
+
let charCode = str.charCodeAt(i).toString(16).padStart(4, '0');
|
52 |
+
result += "%u" + (toUpper ? charCode.toUpperCase() : charCode);
|
53 |
+
}
|
54 |
+
return result;
|
55 |
+
}
|
56 |
+
|
57 |
+
export function unicodeToChinese(str) {
|
58 |
+
return unescape(str);
|
59 |
+
}
|
60 |
+
|
61 |
+
|
62 |
+
export const get = async (url, from_gb2312 = false) => {
|
63 |
+
const res = await axios.get(url, {
|
64 |
+
httpAgent: proxyAgent,
|
65 |
+
httpsAgent: proxyAgent,
|
66 |
+
});
|
67 |
+
return res;
|
68 |
+
}
|
69 |
+
|
70 |
+
export const post = async (url, data) => {
|
71 |
+
const res = await axios.post(url, data, {
|
72 |
+
httpAgent: proxyAgent,
|
73 |
+
httpsAgent: proxyAgent,
|
74 |
+
}).catch((e) => {
|
75 |
+
debugger;
|
76 |
+
});
|
77 |
+
return res;
|
78 |
+
};
|
79 |
+
|
80 |
+
export const downloadFile = async (url, filePath, maxRetries) => {
|
81 |
+
let t = 0;
|
82 |
+
while (t < maxRetries) {
|
83 |
+
try {
|
84 |
+
const res = await axios({
|
85 |
+
httpAgent: proxyAgent,
|
86 |
+
httpsAgent: proxyAgent,
|
87 |
+
method: "get",
|
88 |
+
url: url,
|
89 |
+
responseType: "arraybuffer",
|
90 |
+
});
|
91 |
+
if (!res.data.length) {
|
92 |
+
throw Error("0byte");
|
93 |
+
}
|
94 |
+
if (res.status !== 200) {
|
95 |
+
throw Error("error status");
|
96 |
+
}
|
97 |
+
if (res.data.toString().indexOf('The system is busy. Please retry later') != -1) {
|
98 |
+
throw Error("busy");
|
99 |
+
}
|
100 |
+
fs.writeFileSync(filePath, res.data);
|
101 |
+
return;
|
102 |
+
} catch (e) {
|
103 |
+
console.error("Error downloading file:", e, url);
|
104 |
+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
|
105 |
+
}
|
106 |
+
++t;
|
107 |
+
console.log('retry', t);
|
108 |
+
await sleep(3000);
|
109 |
+
}
|
110 |
+
throw new Error('max retries exceeded');
|
111 |
+
};
|
112 |
+
|
113 |
+
export function parseCookies(cookie_raw) {
|
114 |
+
return cookie_raw.split(";").reduce((m, i) => {
|
115 |
+
i = i.trim();
|
116 |
+
const idx = i.indexOf("=");
|
117 |
+
const name = i.substr(0, idx);
|
118 |
+
const value = i.substr(idx + 1);
|
119 |
+
m[name] = value;
|
120 |
+
return m;
|
121 |
+
}, {});
|
122 |
+
}
|
123 |
+
|
124 |
+
export const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t));
|