Last active
October 8, 2016 01:49
-
-
Save Juribiyan/a48754f88e3f05e8d116 to your computer and use it in GitHub Desktop.
This file contains 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 node_ip = '127.0.0.1', node_port = 9001; | |
var express = require('express'), app = express(); | |
var server = require('http').createServer(app); | |
var cheerio = require('cheerio'); | |
var _ = require('lodash'); | |
app.get('/boards/:chid', function (req, res) { | |
if(getBoards.hasOwnProperty(req.params.chid)) | |
getBoards[req.params.chid](res); | |
else res.send("Unknown chan"); | |
}); | |
var getBoards = { | |
'0-chan': function(res) { | |
request('http://www.0-chan.ru/menu.php', function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
processHTML.kusaba(cheerio.load(body), res, '0-chan.ru'); | |
} | |
else res.send("Unable to make request"); | |
}) | |
} | |
} | |
var processHTML = { | |
kusaba: function($, res, splitURL) { | |
try { | |
var cats = []; | |
var catHeaders = $('h2'); | |
_.each(catHeaders, function(header, index) { | |
var cat = { | |
name: header.children[2].data.trim(), | |
boards: [] | |
}; | |
var boards = $('#'+(index+1)+' .boardlink'); | |
_.each(boards, function(boardlink) { | |
var boardURL = boardlink.attribs.href; | |
var boardDir = (typeof splitURL !== undefined) ? boardURL.split(splitURL)[1] : boardURL; | |
cat.boards.push({ | |
dir: boardDir, | |
desc: boardlink.children[0].data.trim() | |
}) | |
}) | |
cats.push(cat); | |
}) | |
res.json(cats); | |
} | |
catch(e) { | |
res.send('Error parsing HTML'); | |
} | |
} | |
} | |
server.listen(node_port, node_ip); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ACA ESTOY KERES MI DIRECCION MI TELEFONO K CARAJO KERES ???AHHHHHHHHHHHHHHHHH