-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetDataAndSave.ts
62 lines (55 loc) · 1.81 KB
/
GetDataAndSave.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
import { z } from "zod";
import { GetJsonResponse, UrlParams } from "../Domains/Api/GetJsonResponse";
import { ApiJson } from "../Domains/Save/ApiJson";
import { GetMetaData } from "../Domains/Scraping/GetMetaData";
import { AnimeLibrary, AnimeLibraryResponse } from "../Models/Api/AnimeLibrary";
export class GetDataAndSave {
constructor() {}
protected static async getDataAndsave(urlParams: UrlParams): Promise<void> {
// shangriLaApiからjsonを取得
let getJsonResponse: GetJsonResponse;
try {
getJsonResponse = await GetJsonResponse.getJsonResponse(urlParams);
} catch (e) {
console.log(e);
throw new Error();
//{message: "no_data"} jsonを保存
const saveRes = await ApiJson.save(urlParams, { message: "no_data" });
return saveRes;
}
// スクレイピング
const shangriLaApiResponse = getJsonResponse.jsonPerse();
const animeLibraryResponse = await Promise.all(
shangriLaApiResponse.map(
async (animeData): Promise<z.infer<typeof AnimeLibrary>> => {
try {
const metaData = await GetMetaData.getSuperagentResponse(
animeData.public_url
);
return {
...animeData,
...{
ogp_description: metaData.description(),
ogp_image_url: metaData.image(),
},
};
} catch (e) {
return {
...animeData,
...{
ogp_description: "",
ogp_image_url: "",
},
};
}
}
)
);
// jsonを保存
const saveRes = await ApiJson.save(
urlParams,
AnimeLibraryResponse.parse(animeLibraryResponse)
);
return saveRes;
}
}