Last active
October 23, 2016 21:15
-
-
Save belozerskiy/5f5695bb70677c41a1768449d6745d35 to your computer and use it in GitHub Desktop.
spbkit lessons parser
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
| var express = require('express'); | |
| var request = require('request'); | |
| var iconv = require('iconv-lite'); | |
| var cheerio = require('cheerio'); | |
| var lessons = []; | |
| var app = express(); | |
| app.get('/', function(req, res){ | |
| request({ | |
| uri: "http://www.spbkit.edu.ru/index.php?option=com_timetable&Itemid=82", | |
| method: 'GET', | |
| encoding: 'binary' | |
| }, function(error, response, body) { | |
| if (!error && response.statusCode == 200) { | |
| var result = iconv.encode (iconv.decode (new Buffer (body, 'binary'), 'win1251'), 'utf8'); | |
| var $ = cheerio.load(result.toString()); | |
| var courses = []; | |
| $($('#menus .ui-tabs-nav').html()).each(function(i, val){ | |
| if($(val).html()) | |
| courses.push($($(val).html()).attr('href')); | |
| }); | |
| courses.forEach(function(v, k){ | |
| console.log(v); | |
| $(v + ' .ui-tabs-nav li').each(function(i, elem){ | |
| var obj = {}; | |
| var id = $($(elem).html()).attr('href') || ""; | |
| var grp = $($(elem).html()).text(); | |
| if(id.indexOf("#gruppi") != -1 && !Number.isNaN(grp * 1)){ | |
| obj.grp = grp; | |
| obj.week = []; | |
| $(v + ' ' + id + ' .timetablediv li a').each(function(i, elem){ | |
| var day = $(elem).attr('href'); | |
| var week = []; | |
| $(day + ' i').each(function(i, el){ | |
| var table = {}; | |
| var time = $(el).text(); | |
| table.time = time; | |
| week.push(table); | |
| }); | |
| $(day + ' b').each(function(i, el){ | |
| var lesson = $(el).text(); | |
| var cls = lesson.slice(lesson.indexOf("(")+1, lesson.indexOf(")")); | |
| week[i].lesson = lesson; | |
| week[i].class = cls; | |
| }); | |
| obj.week.push(week); | |
| }); | |
| lessons.push(obj); | |
| } | |
| }); | |
| }); | |
| res.send(lessons); | |
| } | |
| }); | |
| }); | |
| app.listen(8080); | |
| console.log("Listening 8080"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment