-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpray.js
113 lines (102 loc) · 1.78 KB
/
cpray.js
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
const axios = require('axios').default;
const getTimes = Symbol('getTimes');
const validZones = [
'KDH01',
'KDH02',
'KDH03',
'KDH04',
'KDH05',
'KDH06',
'KDH07',
'MLK01',
'NGS01',
'NGS02',
'PHG01',
'PHG02',
'PHG03',
'PHG04',
'PHG05',
'PHG06',
'PRK01',
'PRK02',
'PRK03',
'PRK04',
'PRK05',
'PRK06',
'PRK07',
'PLS01',
'PNG01',
'SGR01',
'SGR02',
'SGR03',
'TRG01',
'TRG02',
'TRG03',
'TRG04',
'JHR01',
'JHR02',
'JHR03',
'JHR04',
'KTN01',
'KTN03',
'SBH01',
'SBH02',
'SBH03',
'SBH04',
'SBH05',
'SBH06',
'SBH07',
'SBH08',
'SBH09',
'SWK01',
'SWK02',
'SWK03',
'SWK04',
'SWK05',
'SWK06',
'SWK07',
'SWK08',
'SWK09',
'WLY01',
'WLY02',
];
class Cpray {
constructor() {
this.baseUrl = 'https://www.e-solat.gov.my/index.php?r=esolatApi';
this.fallbackUrl = 'https://api.waktusolat.me/waktusolat';
}
async [getTimes](period, zone) {
zone = zone.toUpperCase();
if (!validZones.includes(zone)) {
throw new Error(`Invalid zone: ${zone}`);
}
const url = `${this.baseUrl}/takwimsolat`;
const params = { period, zone };
try {
const { data } = await axios.get(url, { params });
return data;
} catch (error) {
try {
const { data } = await axios.get(
`${this.fallbackUrl}/${period}/${zone}`
);
return data;
} catch (fallbackError) {
throw new Error('Server error. Sila cuba lagi.');
}
}
}
getTimesToday(zone) {
return this[getTimes]('today', zone);
}
getTimesbyWeek(zone) {
return this[getTimes]('week', zone);
}
getTimesbyMonth(zone) {
return this[getTimes]('month', zone);
}
getTimesbyYear(zone) {
return this[getTimes]('year', zone);
}
}
module.exports = Cpray;