BusInfo.js 4.21 KB
let request = require('request');
let cheerio = require('cheerio');

const bus_url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
const bus_key = 'RwxSWXH88b2bKOAT6Ot3FHorPZQW9omma0xYIjtJe0JIKe4DC7TjX7Uj6E1ArzYi2AvVETmPrAIYyY8FlL%2BfAA%3D%3D';
const stationID = '228000708'; // 사색의광장 들어오는 방향
const gateStationID = '203000125'  

const BusArrivalUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + stationID;// 사색의광장 정류장 버스 도착 정보 조회용
//console.log(BusArrivalUrl);

var routeID = [];
var Bus = [];
request(BusArrivalUrl, (err, res, body) => {
    var $ = cheerio.load(body, {decodeEntities: false});

    $('busArrivalList').each(function(idx){
        let route = $(this).find('routeId').text();
        routeID.push(route);
    })
    //console.log(routeID);
})

const route_url = 'http://apis.data.go.kr/6410000/busrouteservice/getBusRouteInfoItem';
const route_key = 'RwxSWXH88b2bKOAT6Ot3FHorPZQW9omma0xYIjtJe0JIKe4DC7TjX7Uj6E1ArzYi2AvVETmPrAIYyY8FlL%2BfAA%3D%3D';
var index = 0;
function getBusNum(){
    var BusRouteUrl = route_url + '?servicekey=' + route_key + '&routeId='; // 각 버스 정보 조회용
    BusRouteUrl += routeID[index++];
    //console.log(BusRouteUrl);

    request(BusRouteUrl, (err, res, body) => {
        var $ = cheerio.load(body, {decodeEntities: false});

        $('busRouteInfoItem').each(function(idx){
            var id = $(this).find('routeId').text();            //버스 노선 id
            var num = $(this).find('routeName').text();         //버스 번호
            var firsttime = $(this).find('upFirstTime').text(); //평일 기점 첫차시간
            var lasttime = $(this).find('upLastTime').text();   //평일 기점 막차 시간
            var mintime = $(this).find('peekAlloc').text();     //평일 최소 배차시간
            var maxtime = $(this).find('nPeekAlloc').text();    //평일 최대 배차시간

            //var idx = Bus.findIndex((item, idx) => { return item.routeId = id})

            var newBus = new Object();

            newBus.routeId = id;
            newBus.BusNum = num;
            newBus.FirstTime = firsttime;
            newBus.LastTime = lasttime;
            newBus.MinTime = mintime;
            newBus.MaxTime = maxtime;

            console.log(newBus);
            Bus.push(newBus);
        })
    })
}

function useFor(){
    for(var i=0; i<routeID.length; i++){
        setTimeout(getBusNum, 500);
    }
}
setTimeout(useFor, 500);

const GateBusUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + gateStationID; //국제캠 정문 정류장
let date = new Date();
let curHour = date.getHours(); // 현재 시간
let curMinute = date.getMinutes(); //현재 분

function predict(){
    request(GateBusUrl, (err, res, body) => {
        var $ = cheerio.load(body, {decodeEntities: false});

        $('busArrivalList').each(function(idx){
            let route = $(this).find('routeId').text();

            var index = Bus.findIndex(function(e, idx){ return e.routeId == route;});
            if(index > -1){
                //var num = $(this).find('plateNo1').text(); // 버스 차량번호 ex) 70사 1290
                var time = $(this).find('predictTime1').text(); // 버스 예상 도착시간
                var predictHour1 = date.getHours();
                var predictMinute1 = date.getMinutes() + Bus[index].mintime - (3 - (time*1)); // 사색의 광장 -> 경희대학교 3분정도 소요
                var predictHour2 = date.getHours();
                var predictMinute2 = date.getMinutes() + Bus[index].maxtime - (3 - (time*1)); // time*1 : string to integer 형변환

                if(predictMinute1 >= 60){
                    predictHour1 += 1;
                    predictMinute1 -= 60;
                }
                if(predictMinute2 >= 60){
                    predictHour2 += 1;
                    predictMinute2 -= 60;
                }

                Bus[index]["MinPredictTime"] = predictHour1 + ":" + predictMinute1;
                Bus[index]["MaxPredictTime"] = predictHour2 + ":" + predictMinute2;
            }
        })
        console.log(Bus);
    })
}

function useWhile(){
    while(true){
        setTimeout(predict, 6000);
    }
}

setTimeout(useWhile, 1000);