Informasi Seputar kereta api
No comments yet. Be the first to comment!
1/**
2 * KRL COMMUTE - C-Access
3 * API Client untuk mengakses informasi KRL Commute
4 *
5 * @author gienetic
6 * @base https://play.google.com/store/apps/details?id=com.kci.access
7 */
8
9const axios = require('axios');
10
11const API_URL = 'https://api-partner.krl.co.id';
12const API_TOKEN = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIzIiwianRpIjoiMDYzNWIyOGMzYzg3YTY3ZTRjYWE4YTI0MjYxZGYwYzIxNjYzODA4NWM2NWU4ZjhiYzQ4OGNlM2JiZThmYWNmODU4YzY0YmI0MjgyM2EwOTUiLCJpYXQiOjE3MjI2MTc1MTQsIm5iZiI6MTcyMjYxNzUxNCwiZXhwIjoxNzU0MTUzNTE0LCJzdWIiOiI1Iiwic2NvcGVzIjpbXX0.Jz_sedcMtaZJ4dj0eWVc4_pr_wUQ3s1-UgpopFGhEmJt_iGzj6BdnOEEhcDDdIz-gydQL5ek0S_36v5h6P_X3OQyII3JmHp1SEDJMwrcy4FCY63-jGnhPBb4sprqUFruDRFSEIs1cNQ-3rv3qRDzJtGYc_bAkl2MfgZj85bvt2DDwBWPraZuCCkwz2fJvox-6qz6P7iK9YdQq8AjJfuNdl7t_1hMHixmtDG0KooVnfBV7PoChxvcWvs8FOmtYRdqD7RSEIoOXym2kcwqK-rmbWf9VuPQCN5gjLPimL4t2TbifBg5RWNIAAuHLcYzea48i3okbhkqGGlYTk3iVMU6Hf_Jruns1WJr3A961bd4rny62lNXyGPgNLRJJKedCs5lmtUTr4gZRec4Pz_MqDzlEYC3QzRAOZv0Ergp8-W1Vrv5gYyYNr-YQNdZ01mc7JH72N2dpU9G00K5kYxlcXDNVh8520-R-MrxYbmiFGVlNF2BzEH8qq6Ko9m0jT0NiKEOjetwegrbNdNq_oN4KmHvw2sHkGWY06rUeciYJMhBF1JZuRjj3JTwBUBVXcYZMFtwUAoikVByzKuaZZeTo1AtCiSjejSHNdpLxyKk_SFUzog5MOkUN1ktAhFnBFoz6SlWAJBJIS-lHYsdFLSug2YNiaNllkOUsDbYkiDtmPc9XWc';
13
14const HEADERS = {
15 'Authorization': API_TOKEN,
16 'Accept': 'application/json',
17 'User-Agent': 'Mozilla/5.0',
18 'Accept-Encoding': 'gzip, deflate',
19 'Accept-Language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7'
20};
21
22/**
23 * Mendapatkan daftar semua stasiun KRL
24 * @returns {Promise<Array>} Array berisi data stasiun
25 */
26async function getAllStations() {
27 try {
28 const response = await axios.get(`${API_URL}/krl-webs/v1/krl-station`, {
29 headers: HEADERS
30 });
31 return response.data.data || [];
32 } catch (error) {
33 console.error("❌ Gagal mendapatkan daftar stasiun:", error.response?.data?.message || error.message);
34 return [];
35 }
36}
37
38/**
39 * Menampilkan informasi rute dan daftar stasiun
40 */
41async function infoRoute() {
42 const stations = await getAllStations();
43
44 if (!stations.length) {
45 console.log("⚠️ Tidak ada data stasiun yang tersedia");
46 return;
47 }
48
49 console.log("🚉 Daftar Stasiun KRL:");
50 console.log("----------------------");
51
52 stations.forEach((station, index) => {
53 console.log(`${index + 1}. ${station.sta_name}`);
54 });
55}
56
57/**
58 * Mencari stasiun berdasarkan nama
59 * @param {string} stationName Nama stasiun
60 * @returns {Promise<Object|null>} Data stasiun jika ditemukan
61 */
62async function getStationId(stationName) {
63 if (!stationName) {
64 console.log("⚠️ Nama stasiun tidak boleh kosong");
65 return null;
66 }
67
68 const stations = await getAllStations();
69 return stations.find(station =>
70 station.sta_name.toLowerCase().includes(stationName.toLowerCase())
71 );
72}
73
74/**
75 * Menampilkan tarif perjalanan antar stasiun
76 * @param {string} origin Stasiun asal
77 * @param {string} destination Stasiun tujuan
78 */
79async function tarifKereta(origin, destination) {
80 try {
81 const [originData, destinationData] = await Promise.all([
82 getStationId(origin),
83 getStationId(destination)
84 ]);
85
86 if (!originData || !destinationData) {
87 console.log("❌ Stasiun tidak ditemukan. Pastikan nama stasiun benar.");
88 return;
89 }
90
91 const response = await axios.get(
92 `${API_URL}/krl-webs/v1/fare?stationfrom=${originData.sta_id}&stationto=${destinationData.sta_id}`, {
93 headers: HEADERS
94 }
95 );
96
97 const fareData = response.data.data?.[0];
98 if (fareData) {
99 console.log(`💰 Tarif ${originData.sta_name} → ${destinationData.sta_name}:`);
100 console.log(` - Harga: Rp ${fareData.fare}`);
101 console.log(` - Jarak: ${fareData.distance} km`);
102 } else {
103 console.log("ℹ️ Tarif tidak tersedia untuk rute ini");
104 }
105 } catch (error) {
106 console.error("❌ Gagal mengambil tarif:", error.response?.data?.message || error.message);
107 }
108}
109
110/**
111 * Menampilkan jadwal kereta di stasiun tertentu
112 * @param {string} stationName Nama stasiun
113 * @param {string} startTime Waktu mulai (HH:MM)
114 * @param {string} endTime Waktu selesai (HH:MM)
115 */
116async function jadwalKereta(stationName, startTime, endTime) {
117 try {
118 const stationData = await getStationId(stationName);
119
120 if (!stationData) {
121 console.log("❌ Stasiun tidak ditemukan");
122 return;
123 }
124
125 const response = await axios.get(
126 `${API_URL}/krl-webs/v1/schedule?stationid=${stationData.sta_id}&timefrom=${startTime}&timeto=${endTime}`, {
127 headers: HEADERS
128 }
129 );
130
131 const schedules = response.data.data;
132 if (schedules?.length) {
133 console.log(`🚉 Jadwal KRL di ${stationData.sta_name} (${startTime} - ${endTime}):`);
134 console.log("--------------------------------------------------");
135
136 schedules.forEach((schedule, index) => {
137 console.log(`${index + 1}. ${schedule.ka_name} → ${schedule.dest}`);
138 console.log(` 🕒 Berangkat: ${schedule.time_est}`);
139 console.log(` 🕒 Tiba: ${schedule.dest_time}`);
140 console.log("--------------------------------------------------");
141 });
142 } else {
143 console.log("ℹ️ Tidak ada jadwal kereta pada waktu yang diminta");
144 }
145 } catch (error) {
146 console.error("❌ Gagal mengambil jadwal:", error.response?.data?.message || error.message);
147 }
148}
149
150//infoRoute();
151//tarifKereta('Bogor', 'Bekasi');
152//jadwalKereta('Bogor', '07:00', '09:00');
153
154module.exports = {
155 infoRoute,
156 tarifKereta,
157 jadwalKereta
158};