Ayogiakbar

KRL COMMUTE

Jul 27th, 2025
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  9. const axios = require('axios');
  10.  
  11. const API_URL = 'https://api-partner.krl.co.id';
  12. const 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.  
  14. const 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.  */
  26. async function getAllStations() {
  27.   try {
  28.     const response = await axios.get(`${API_URL}/krl-webs/v1/krl-station`, { headers: HEADERS });
  29.     return response.data.data || [];
  30.   } catch (error) {
  31.     console.error("❌ Gagal mendapatkan daftar stasiun:", error.response?.data?.message || error.message);
  32.     return [];
  33.   }
  34. }
  35.  
  36. /**
  37.  * Menampilkan informasi rute dan daftar stasiun
  38.  */
  39. async function infoRoute() {
  40.   const stations = await getAllStations();
  41.  
  42.   if (!stations.length) {
  43.     console.log("âš ī¸ Tidak ada data stasiun yang tersedia");
  44.     return;
  45.   }
  46.  
  47.   console.log("🚉 Daftar Stasiun KRL:");
  48.   console.log("----------------------");
  49.  
  50.   stations.forEach((station, index) => {
  51.     console.log(`${index + 1}. ${station.sta_name}`);
  52.   });
  53. }
  54.  
  55. /**
  56.  * Mencari stasiun berdasarkan nama
  57.  * @param {string} stationName Nama stasiun
  58.  * @returns {Promise<Object|null>} Data stasiun jika ditemukan
  59.  */
  60. async function getStationId(stationName) {
  61.   if (!stationName) {
  62.     console.log("âš ī¸ Nama stasiun tidak boleh kosong");
  63.     return null;
  64.   }
  65.  
  66.   const stations = await getAllStations();
  67.   return stations.find(station =>
  68.     station.sta_name.toLowerCase().includes(stationName.toLowerCase())
  69.   );
  70. }
  71.  
  72. /**
  73.  * Menampilkan tarif perjalanan antar stasiun
  74.  * @param {string} origin Stasiun asal
  75.  * @param {string} destination Stasiun tujuan
  76.  */
  77. async function tarifKereta(origin, destination) {
  78.   try {
  79.     const [originData, destinationData] = await Promise.all([
  80.       getStationId(origin),
  81.       getStationId(destination)
  82.     ]);
  83.  
  84.     if (!originData || !destinationData) {
  85.       console.log("❌ Stasiun tidak ditemukan. Pastikan nama stasiun benar.");
  86.       return;
  87.     }
  88.  
  89.     const response = await axios.get(
  90.       `${API_URL}/krl-webs/v1/fare?stationfrom=${originData.sta_id}&stationto=${destinationData.sta_id}`,
  91.       { headers: HEADERS }
  92.     );
  93.  
  94.     const fareData = response.data.data?.[0];
  95.     if (fareData) {
  96.       console.log(`💰 Tarif ${originData.sta_name} → ${destinationData.sta_name}:`);
  97.       console.log(`   - Harga: Rp ${fareData.fare}`);
  98.       console.log(`   - Jarak: ${fareData.distance} km`);
  99.     } else {
  100.       console.log("â„šī¸ Tarif tidak tersedia untuk rute ini");
  101.     }
  102.   } catch (error) {
  103.     console.error("❌ Gagal mengambil tarif:", error.response?.data?.message || error.message);
  104.   }
  105. }
  106.  
  107. /**
  108.  * Menampilkan jadwal kereta di stasiun tertentu
  109.  * @param {string} stationName Nama stasiun
  110.  * @param {string} startTime Waktu mulai (HH:MM)
  111.  * @param {string} endTime Waktu selesai (HH:MM)
  112.  */
  113. async function jadwalKereta(stationName, startTime, endTime) {
  114.   try {
  115.     const stationData = await getStationId(stationName);
  116.    
  117.     if (!stationData) {
  118.       console.log("❌ Stasiun tidak ditemukan");
  119.       return;
  120.     }
  121.  
  122.     const response = await axios.get(
  123.       `${API_URL}/krl-webs/v1/schedule?stationid=${stationData.sta_id}&timefrom=${startTime}&timeto=${endTime}`,
  124.       { headers: HEADERS }
  125.     );
  126.  
  127.     const schedules = response.data.data;
  128.     if (schedules?.length) {
  129.       console.log(`🚉 Jadwal KRL di ${stationData.sta_name} (${startTime} - ${endTime}):`);
  130.       console.log("--------------------------------------------------");
  131.      
  132.       schedules.forEach((schedule, index) => {
  133.         console.log(`${index + 1}. ${schedule.ka_name} → ${schedule.dest}`);
  134.         console.log(`   🕒 Berangkat: ${schedule.time_est}`);
  135.         console.log(`   🕒 Tiba: ${schedule.dest_time}`);
  136.         console.log("--------------------------------------------------");
  137.       });
  138.     } else {
  139.       console.log("â„šī¸ Tidak ada jadwal kereta pada waktu yang diminta");
  140.     }
  141.   } catch (error) {
  142.     console.error("❌ Gagal mengambil jadwal:", error.response?.data?.message || error.message);
  143.   }
  144. }
  145.  
  146. //infoRoute();
  147. //tarifKereta('Bogor', 'Bekasi');
  148. //jadwalKereta('Bogor', '07:00', '09:00');
  149.  
  150. module.exports = {
  151.   infoRoute,
  152.   tarifKereta,
  153.   jadwalKereta
  154. };
Tags: Krl
Advertisement
Add Comment
Please, Sign In to add comment