Last active
August 29, 2015 14:10
-
-
Save heruoxin/b2f2c15664a3ff273c8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//成都理工课表转换为Google Claendar兼容的csv | |
//登录后选择「我的课表」并将html文件另存,然后用本脚本转换,即可导入Google Calendar。 | |
//2014秋测试有效。 | |
//依赖包就cheerio一个,用的时候请酌情修改filePath。 | |
//用法: node ./index.js > 课表.csv | |
var cheerio = require('cheerio'); | |
var fs = require('fs'); | |
var filePath = "./StuProductionSchedule.aspx.html"; | |
var oneDayTime = [ | |
//上午 | |
{start: "08:10 AM", stop: "08:55 AM"}, | |
{start: "09:00 AM", stop: "09:45 AM"}, | |
{start: "10:15 AM", stop: "11:00 AM"}, | |
{start: "11:05 AM", stop: "11:50 AM"}, | |
//中文 | |
{start: "12:30 PM", stop: "02:15 PM"}, | |
//下午 | |
{start: "02:30 PM", stop: "03:15 PM"}, | |
{start: "03:20 PM", stop: "04:05 PM"}, | |
{start: "04:15 PM", stop: "05:00 PM"}, | |
{start: "05:05 PM", stop: "05:50 PM"}, | |
//晚上 | |
{start: "07:10 PM", stop: "07:55 PM"}, | |
{start: "08:00 PM", stop: "08:45 PM"}, | |
{start: "08:50 PM", stop: "09:35 PM"}, | |
]; | |
fs.readFile(filePath, function (err, data) { | |
if (err) throw err; | |
$ = cheerio.load(data); | |
Json2CSV(parseSchedule($)); | |
}); | |
var Json2CSV = function(classList) { | |
console.log("Subject,Start Date,End Date,Start Time,End Time,Description,Location"); | |
classList.forEach(function(x){ | |
console.log([ | |
x.className, | |
x.date, | |
x.date, | |
x.startTime, | |
x.endTime, | |
x.detail, | |
x.location | |
].join(",")); | |
}); | |
}; | |
var parseSchedule = function($) { | |
var eachClasses = $('.fontcss'); | |
var classList = []; | |
var jumpLists = ParseClassName($); | |
var classNameJumpList = jumpLists[0]; | |
var detailJumpList = jumpLists[1]; | |
classNameJumpList[""] = ""; | |
for (var i =0; i < eachClasses.length; i++) { | |
var className = eachClasses[i].children[0].data || ""; | |
var location; | |
if (!eachClasses[i].children[2]) location = ""; | |
else location = eachClasses[i].children[2].children[0].data; | |
var length = Number(eachClasses[i].attribs.colspan || 1); | |
//console.log(className, location, length); | |
classList.push({ | |
className: classNameJumpList[className.substr(0, (className.length-2))], | |
detail: detailJumpList[className.substr(0, (className.length-2))], | |
location: location, | |
length: length | |
}); | |
} | |
return addTime(classList, getStartDate($)); | |
}; | |
var addTime = function(classList, startDate) { | |
var ManyClasses = 0; | |
for (var i=0; i<classList.length; i++) { | |
var today = new Date(startDate); | |
today.setDate(today.getDate() + Math.floor(ManyClasses/12)); | |
classList[i].date = (today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear(); | |
classList[i].startTime = oneDayTime[ManyClasses % 12].start; | |
ManyClasses += classList[i].length; | |
classList[i].endTime = oneDayTime[(ManyClasses - 1) % 12].stop; | |
} | |
return classList.filter(function(x){return x.className;}); | |
}; | |
var ParseClassName = function($) { | |
var eachClasses = $('.detail'); | |
var classNameJumpList = {}; | |
var detailJumpList = {}; | |
for (var i =0; i < eachClasses.length; i++) { | |
if (!eachClasses[i].children[0]) continue; | |
var description = eachClasses[i].children[0].children[0].data.replace('\n',''); | |
//console.log(description); | |
var shortName = description.split(')')[0].split('(')[1]; | |
var longName = description.split('(')[1].split(')')[1]; | |
classNameJumpList[shortName] = longName.replace(' ', ''); | |
detailJumpList[shortName] = description; | |
} | |
return [classNameJumpList, detailJumpList]; | |
}; | |
var getStartDate = function($) { | |
var year = $('td.title.nob')[0].children[0].data.split('-')[0].substr(-4, 4); | |
var monthDate = $('td.td1 br')[0].next.data.split('-')[0]; | |
return new Date(monthDate+'/'+year); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment