-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsakashiLibraryFetcher.ts
142 lines (126 loc) · 4.66 KB
/
OsakashiLibraryFetcher.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
import puppeteer, { Browser, Page } from "puppeteer";
import { JSDOM } from "jsdom";
type FetchProps = {
id: string;
pw: string;
name: string;
};
export class OsakashiLibraryFetcher {
#browser: Browser | null = null;
#page: Page | null = null;
async #launchBrowser() {
console.log(`#launchBrowser: start`);
this.#browser = await puppeteer.launch({
args: ['--no-sandbox'],
});
this.#page = await this.#browser.newPage();
//画像、css、フォントファイルを拒否する
await this.#page.setRequestInterception(true);
this.#page.on("request", (req) => {
if (["image", "stylesheet", "font"].indexOf(req.resourceType()) !== -1) {
req.abort();
} else {
req.continue();
}
});
}
async Fetch(props: FetchProps) {
const { id, name, pw } = props;
if (!id || !pw || !name) throw new Error("id,pw,nameのいずれかが間違っています");
await this.#launchBrowser();
if (!this.#browser || !this.#page) throw new Error("pageとbrowserが初期化されていません");
console.log(`${name}のfetchを開始: `);
//ログインページに行く
await this.#page.goto(
"https://web.oml.city.osaka.lg.jp/webopac_i_ja/login.do?url=ufisnd.do%3Fredirect_page_id%3D13",
{ waitUntil: "networkidle0" }
);
console.log(`ログインページに移動完了: `);
//IDとPWを入力する
await this.#page.type("input[type=text]", id);
await this.#page.type("input[type=password]", pw);
//ログインボタンをクリックして、目的の要素が出現するまで待つ
await Promise.all([
this.#page.waitForSelector('a[title="ログアウト"]'),
this.#page.click("a.btn"),
]);
console.log(`ログイン完了: `);
//マイページを開き、目的の要素が出現するまで待つ
await Promise.all([
this.#page.waitForSelector("iframe[id='usepopup_frm']"),
this.#page.goto("https://www.oml.city.osaka.lg.jp/?page_id=113", {
waitUntil: "networkidle0",
}),
]);
console.log(`マイページの表示完了: `);
//フレームを取得する
const frameHandle = await this.#page.$("iframe[id='usepopup_frm']");
if (!frameHandle) throw new Error("frameHandleが見つかりませんでした");
const frame = await frameHandle.contentFrame();
if (!frame) throw new Error("frameが見つかりませんでした");
await frame.waitForSelector('*[name="askuseform"]');
//予約一覧ページを開く
await Promise.all([
frame.waitForSelector('form[name="askrsvform"]'),
frame.evaluate(() => {
// @ts-ignore
opacSendActionPopup("_7695", "rsvlst.do", document.tmpActForm);
return false;
}),
]);
console.log(`予約一覧ページの表示完了: `);
// 予約一覧を取得する
const reservedBookListDom = new JSDOM(
await (await (await frame.$('form[name="askrsvform"]'))!.getProperty("outerHTML")).jsonValue()
);
//再度マイページへ
await Promise.all([
frame.waitForSelector('form[name="askuseform"]'),
frame.evaluate(() => {
// @ts-ignore
opacSendActionPopup("_7695", "asklst.do", document.tmpActForm);
return false;
}),
]);
//貸し出し数が0冊だとボタンが表示されないので処理丸ごとスキップする
const rentalBooks = await (
await (await frame.$$(".opac_block_body_big"))[0].getProperty("textContent")
).jsonValue();
const isExitsRentalBooks = rentalBooks!.match(/貸出されている資料\s0点/) === null ? false : true;
if (isExitsRentalBooks) {
console.log("貸出数が0")
await this.#closeBrowser();
return {
reservedBookListDom,
borrowedBookListDom: new JSDOM(""),
};
} else {
console.log("貸出数が1以上")
//貸し出し一覧ページへ
await Promise.all([
frame.waitForSelector('form[name="asklenform"]'),
frame.evaluate(() => {
// @ts-ignore
opacSendActionPopup("_7695", "lenlst.do", document.tmpActForm);
return false;
}),
]);
//貸し出し一覧を取得する
const borrowedBookListDom = new JSDOM(
await (
await (await frame.$('form[name="asklenform"]'))!.getProperty("outerHTML")
).jsonValue()
);
await this.#closeBrowser();
return {
reservedBookListDom,
borrowedBookListDom,
};
}
}
async #closeBrowser() {
if (!this.#browser || !this.#page) throw new Error("pageとbrowserが初期化されていません");
await this.#page.close();
await this.#browser.close();
}
}