-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.ts
53 lines (42 loc) · 1.18 KB
/
api.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
import * as cheerio from "cheerio";
import { got } from "got";
import { CookieJar } from "tough-cookie";
import { config } from "./config";
import { DataItem, RListAPI } from "./types";
const cookieJar = new CookieJar();
const client = got.extend({
cookieJar,
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
},
});
export async function getToken() {
const html = await client("https://rent.591.com.tw").text();
const $ = cheerio.load(html);
const csrfToken = $('meta[name="csrf-token"]').attr("content");
if (!csrfToken) {
console.error(html);
throw "找不到 csrfToken";
}
return csrfToken;
}
export async function getHouseList(): Promise<DataItem[]> {
const csrfToken = await getToken();
const houseListURL = config.houseListURL;
try {
const data = await client({
url: houseListURL,
headers: {
"X-CSRF-TOKEN": csrfToken,
},
}).json();
const {
data: { data: houseList },
} = data as RListAPI;
return houseList;
} catch (error) {
console.error(error);
throw `Token 可能過期了`;
}
}