-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapeDiff.js
More file actions
59 lines (54 loc) · 1.96 KB
/
Copy pathscrapeDiff.js
File metadata and controls
59 lines (54 loc) · 1.96 KB
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
import { CSV } from "https://js.sabae.cc/CSV.js";
import { HTMLParser } from "https://js.sabae.cc/HTMLParser.js";
import { Day } from "https://js.sabae.cc/DateTime.js";
//import { Browser } from "./Browser.js";
//const browser = await new Browser().init();
// 原則として作成日の16時に公開します
export const scrapeDiff = async () => {
const url = "https://www.houjin-bangou.nta.go.jp/download/sabun/";
/*
const headers = new Headers({
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
});
const html = await (await fetch(url, { headers })).text();
*/
const html = await (await fetch(url)).text();
await Deno.mkdir("temp", { recursive: true });
//const html = await browser.fetchText(url, { sleepms: 3000 });
await Deno.writeTextFile("temp/list.html", html);
//const html = await Deno.readTextFile("list.html"); // for initial data
const dom = HTMLParser.parse(html);
const token = dom.querySelector(
'input[name="jp.go.nta.houjin_bangou.framework.web.common.CNSFWTokenProcessor.request.token"]',
)?.attributes.value;
console.log(token);
if (!token) {
console.log(html);
throw new Error("can't find token");
}
const trs = dom.querySelectorAll(".tbl03 tr");
const list = trs.map((tr) => {
const a = tr.querySelector("a");
if (!a) {
return null;
}
return {
date: new Day(tr.querySelector("th").text.trim()).toString(),
fileNo: a.attributes.onclick.match(/\((\d+)\)/)[1],
token,
};
}).filter((a) => a);
console.log(trs.length);
// update
const data = CSV.toJSON(await CSV.fetch("data/diff.csv"));
list.forEach((l) => {
if (!data.find((d) => d.date == l.date)) {
data.push(l);
}
});
data.sort((a, b) => a.date.localeCompare(b.date));
await Deno.writeTextFile("data/diff.csv", CSV.stringify(data));
};
if (Deno.mainModule.endsWith("/scrapeDiff.js")) {
await scrapeDiff();
}