Skip to content

Instantly share code, notes, and snippets.

@juwoong
Created December 11, 2014 05:10
Show Gist options
  • Save juwoong/f45d73e3fb776a0e862e to your computer and use it in GitHub Desktop.
Save juwoong/f45d73e3fb776a0e862e to your computer and use it in GitHub Desktop.
var cheerio = require('cheerio');
var mysql = require('mysql');
var request = require('request');
var key = "mprvOxIo4u5PCVIVStlnRI6rMDBmJGRvC6%2BNurrGAkl0Ctsmt7UJxU9XwMwP4IOAuaRxjScQ2hGKaDm1n1z%2BgA%3D%3D";
var baseurl = {
seoul : "http://m.bus.go.kr"
};
var connection = mysql.createConnection({
host : 'localhost',
user : 'cpd',
password : '12341234',
database : 'compath',
});
connection.connect(function (err) {
if (err) {
console.log('Mysql Connection ERROR');
console.log(err);
throw err;
}
});
function getcallback (err, stNm, arsid) {
query = "INSERT INTO BusStation (stName, stId) VALUES ";
q_toValue = "('" + stNm + "', '" + arsid + "')";
query += q_toValue;
console.log(query);
connection.query(query , function(err) {
if(err) throw err;
else {
console.log("Success : " + stNm + " (" + arsid + ")");
}
});
console.log('END!');
}
function parsing (arsid, callback){
console.log("call /getRouteByStation/");
var option = {
host : baseurl.seoul,
port : 80,
path : '/mBus/bus.bms?search=' + arsid,
};
//url = option.host + option.path;
url = "http://m.gbis.go.kr/jsp/stationList.jsp?method=num&stationKeyValue="+arsid+"&stationKey="+arsid;
console.log(url);
//url = 'http://blog.saltfactory.net';
request(url, function (err, res, body){
if(err) throw err;
var $ = cheerio.load(body);
var value = $('span').text();
console.log(value);
if(value != "") {
callback(err, value, arsid);
} else {
console.log("ERR : NO STATION (" + arsid + ")");
}
});
}
for (i=1; i<2; i++) {
for (j=1; j<1000; j++) {
value = '0' + i.toString();
if(j / 100 > 1) value = value + j.toString();
else if (j / 10 > 1) value = value + '0' + j.toString();
else value = value + '00' + j.toString();
console.log('Station NUM : ' + value);
process.nextTick(parsing(value, getcallback));
}
}
console.log("END");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment