Created
March 30, 2014 02:45
-
-
Save jasonrhodes/9866620 to your computer and use it in GitHub Desktop.
Demonstrating how to think in callbacks for someone in IRC
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 request = require('request'); | |
var cheerio = require('cheerio'); | |
var express = require('express'); | |
var app = express(); | |
app.use(express.logger()); | |
var leagues = { ambush: '506' }; | |
//request(forum_url(league, page), get_forum_threads); | |
//request(forum_url('ambush', 1), get_forum_threads); | |
function get_forum_threads(err, body, callback) { | |
if (err) { | |
callback(err); | |
} else { | |
var thread_ids = []; | |
var $ = cheerio.load(body); | |
var links = $('.thread .thread_title .title a'); | |
$(links).each(function(i, link) { | |
var l = $(link).attr('href').replace('/forum/view-thread/', ''); | |
thread_ids.push(l); | |
}); | |
callback(null, thread_ids); | |
} | |
} | |
function list_forum_threads(thread_ids) { | |
//for (i = 0; i < thread_ids.length; i++) { | |
//console.log(thread_ids[i]); | |
//} | |
//request(thread_url(thread_ids[0]), get_thread_data); | |
return thread_ids; | |
} | |
function get_thread_data(err, resp, body) { | |
if (!err) { | |
var $ = cheerio.load(body); | |
var items = $('body script').last(); | |
var item_data = JSON.parse($(items).text().replace(/(\r\n|\n|\r)/gm, '') | |
.replace(/.+new R\(/, '').replace(/\)\)\.run.+/, '')); | |
console.log(item_data); | |
} else console.log('Error!\n' + err); | |
} | |
function forum_url(league, page) { | |
return 'http://pathofexile.com/forum/view-forum/' + leagues[league] + '/page/' + page; | |
} | |
function thread_url(thread_id) { | |
return 'http://pathofexile.com/forum/view-thread/' + thread_id; | |
} | |
app.get('/:league/:page?', function(req, res, next) { | |
if (req.params.league in leagues) { | |
var page = req.params.page || '1'; | |
request(forum_url(req.params.league, page), function (err, resp, body) { | |
get_forum_threads(err, body, function (err, threads) { | |
// use res here, etc | |
}); | |
}); | |
res.send('Grabbing '+ req.params.league +' page '+ page, t); | |
} else res.end('League not found'); | |
}); | |
var server = app.listen(3000, function() { | |
console.log('Listening on port %d', server.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment