-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
87 lines (74 loc) · 2.08 KB
/
script.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
import { writeCSV } from "./csv/csv.service";
import { MyHoraScrapingService } from "./scraping/myhora.service";
import type {
ThaiLottoData,
ThaiLottoDataWithDate,
} from "./scraping/scraping.service";
import { ThaiTime } from "./utils/thai-time";
// getDates() จะ return ข้อมูลที่เป็น array ของ string ที่เป็นวันที่
const myhora = new MyHoraScrapingService();
const dates = await myhora.getDates();
const dataCSV1: Record<
string,
{
date: string;
}[]
> = {};
dataCSV1["all"] = [];
for (const date of dates) {
const thaiTime = new ThaiTime(date);
const year = thaiTime.format("YYYY");
const yearThai = thaiTime.format("BBBB");
dataCSV1["all"].push({
date: thaiTime.YYYY_MM_DD(),
});
if (!dataCSV1[year]) {
dataCSV1[year] = [];
}
dataCSV1[year].push({
date: thaiTime.YYYY_MM_DD(),
});
if (!dataCSV1[yearThai]) {
dataCSV1[yearThai] = [];
}
dataCSV1[yearThai].push({
date: thaiTime.YYYY_MM_DD(),
});
}
for (const key in dataCSV1) {
await writeCSV(dataCSV1[key], `./data/date/${key}.csv`);
await Bun.write(`./data/date/${key}.json`, JSON.stringify(dataCSV1[key]), {
createPath: true,
});
}
const list = await myhora.getAll();
const dataCSV2: Record<string, (ThaiLottoData & { date: string })[]> = {};
const dataJSON2: Record<string, string> = {};
dataCSV2["all"] = [];
for (const { date, ...data } of list) {
const thaiTime = new ThaiTime(date);
const year = thaiTime.format("YYYY");
const yearThai = thaiTime.format("BBBB");
const output = {
date: thaiTime.YYYY_MM_DD(),
...data,
};
dataCSV2["all"].push(output);
if (dataCSV2[year]) {
dataCSV2[year].push(output);
} else {
dataCSV2[year] = [output];
}
if (dataCSV2[yearThai]) {
dataCSV2[yearThai].push(output);
} else {
dataCSV2[yearThai] = [output];
}
}
for (const key in dataCSV2) {
const jsonData = [...dataCSV2[key]];
await writeCSV(dataCSV2[key], `./data/result/${key}.csv`);
await Bun.write(`./data/result/${key}.json`, JSON.stringify(jsonData), {
createPath: true,
});
}