홍용민

필요 정보 저장 형태 json으로 변환

Showing 1 changed file with 54 additions and 37 deletions
...@@ -7,7 +7,7 @@ const stationID = '228000708'; // 사색의광장 들어오는 방향 ...@@ -7,7 +7,7 @@ const stationID = '228000708'; // 사색의광장 들어오는 방향
7 const gateStationID = '203000125' 7 const gateStationID = '203000125'
8 8
9 const BusArrivalUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + stationID;// 사색의광장 정류장 버스 도착 정보 조회용 9 const BusArrivalUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + stationID;// 사색의광장 정류장 버스 도착 정보 조회용
10 -console.log(BusArrivalUrl); 10 +//console.log(BusArrivalUrl);
11 11
12 var routeID = []; 12 var routeID = [];
13 var Bus = []; // 버스 차 번호( ex) 경기 70바 3713 ) / 도착 여부 확인용 13 var Bus = []; // 버스 차 번호( ex) 경기 70바 3713 ) / 도착 여부 확인용
...@@ -19,61 +19,78 @@ request(BusArrivalUrl, (err, res, body) => { ...@@ -19,61 +19,78 @@ request(BusArrivalUrl, (err, res, body) => {
19 let route = $(this).find('routeId').text(); 19 let route = $(this).find('routeId').text();
20 routeID.push(route); 20 routeID.push(route);
21 }) 21 })
22 - console.log(routeID); 22 + //console.log(routeID);
23 }) 23 })
24 24
25 const route_url = 'http://apis.data.go.kr/6410000/busrouteservice/getBusRouteInfoItem'; 25 const route_url = 'http://apis.data.go.kr/6410000/busrouteservice/getBusRouteInfoItem';
26 const route_key = 'RwxSWXH88b2bKOAT6Ot3FHorPZQW9omma0xYIjtJe0JIKe4DC7TjX7Uj6E1ArzYi2AvVETmPrAIYyY8FlL%2BfAA%3D%3D'; 26 const route_key = 'RwxSWXH88b2bKOAT6Ot3FHorPZQW9omma0xYIjtJe0JIKe4DC7TjX7Uj6E1ArzYi2AvVETmPrAIYyY8FlL%2BfAA%3D%3D';
27 - 27 +var index = 0;
28 -var j = 0;
29 function getBusNum(){ 28 function getBusNum(){
30 - let BusRouteUrl = route_url + '?servicekey=' + route_key + '&routeId='; // 각 버스 정보 조회용 29 + var BusRouteUrl = route_url + '?servicekey=' + route_key + '&routeId='; // 각 버스 정보 조회용
31 - BusRouteUrl += routeID[j++]; 30 + BusRouteUrl += routeID[index++];
32 //console.log(BusRouteUrl); 31 //console.log(BusRouteUrl);
33 32
34 request(BusRouteUrl, (err, res, body) => { 33 request(BusRouteUrl, (err, res, body) => {
35 var $ = cheerio.load(body, {decodeEntities: false}); 34 var $ = cheerio.load(body, {decodeEntities: false});
35 +
36 $('busRouteInfoItem').each(function(idx){ 36 $('busRouteInfoItem').each(function(idx){
37 - let num = $(this).find('routeName').text(); 37 + var id = $(this).find('routeId').text(); //버스 노선 id
38 - BusNum.push(num); 38 + var num = $(this).find('routeName').text(); //버스 번호
39 - console.log(num); 39 + var firsttime = $(this).find('upFirstTime').text(); //평일 기점 첫차시간
40 + var lasttime = $(this).find('upLastTime').text(); //평일 기점 막차 시간
41 + var mintime = $(this).find('peekAlloc').text(); //평일 최소 배차시간
42 + var maxtime = $(this).find('nPeekAlloc').text(); //평일 최대 배차시간
43 +
44 + //var idx = Bus.findIndex((item, idx) => { return item.routeId = id})
45 +
46 + var newBus = new Object();
47 +
48 + newBus.routeId = id;
49 + newBus.BusNum = num;
50 + newBus.FirstTime = firsttime;
51 + newBus.LastTime = lasttime;
52 + newBus.MinTime = mintime;
53 + newBus.MaxTime = maxtime;
54 +
55 + console.log(newBus);
56 + Bus.push(newBus);
40 }) 57 })
41 }) 58 })
42 } 59 }
43 60
44 function useFor(){ 61 function useFor(){
45 for(var i=0; i<routeID.length; i++){ 62 for(var i=0; i<routeID.length; i++){
46 - setTimeout(getBusNum, 1000); 63 + setTimeout(getBusNum, 500);
47 } 64 }
48 } 65 }
49 setTimeout(useFor, 500); 66 setTimeout(useFor, 500);
50 67
51 -const GateBusUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + gateStationID; 68 +// const GateBusUrl = bus_url + '?servicekey=' + bus_key + '&stationId=' + gateStationID;
52 69
53 -request(GateBusUrl, (err, res, body) => { 70 +// request(GateBusUrl, (err, res, body) => {
54 - var $ = cheerio.load(body, {decodeEntities: false}); 71 +// var $ = cheerio.load(body, {decodeEntities: false});
55 72
56 - $('busArrivalList').each(function(idx){
57 - let route = $(this).find('routeId').text();
58 - let num = $(this).find('plateNo1').text();
59 -
60 - var index = routeID.indexOf(route);
61 - if(index > -1){
62 - for(var i=0; i<Bus.length; i++){
63 - if(Bus[i]['route'] == route){
64 - if(Bus[i]['num'] != num){
65 - Bus[i]['pass'] = true;
66 - Bus[i]['num'] == num;
67 - }
68 - }
69 - }
70 - var info = new Object();
71 - info.route = route;
72 - info.num = num;
73 - info.pass = false; // json 형태로 저장
74 - Bus.push(info);
75 - }
76 - })
77 - console.log(routeID);
78 - console.log(Bus);
79 -})
...\ No newline at end of file ...\ No newline at end of file
73 +// $('busArrivalList').each(function(idx){
74 +// let route = $(this).find('routeId').text();
75 +// let num = $(this).find('plateNo1').text();
76 +
77 +// var index = routeID.indexOf(route);
78 +// if(index > -1){
79 +// for(var i=0; i<Bus.length; i++){
80 +// if(Bus[i]['route'] == route){
81 +// if(Bus[i]['num'] != num){
82 +// Bus[i]['pass'] = true;
83 +// Bus[i]['num'] == num;
84 +// }
85 +// }
86 +// }
87 +// var info = new Object();
88 +// info.route = route;
89 +// info.num = num;
90 +// info.pass = false; // json 형태로 저장
91 +// Bus.push(info);
92 +// }
93 +// })
94 +// //console.log(routeID);
95 +// //console.log(Bus);
96 +// })
...\ No newline at end of file ...\ No newline at end of file
......