var cheerio = require('cheerio'), request = require('request'), _ = require('underscore'); var config = require('./config'); function makeOpts(url) { var opts = { url: url, headers: {} }; var core = require('./core'); if (core.isLogin()) { var user = core.getUserSync(); opts.headers['Cookie'] = 'PHPSESSID='+user.session_id+';csrftoken='+user.session_csrf+';'; } return opts; } var leetcode_client = {}; leetcode_client.getProblems = function(cb) { request(makeOpts(config.PROBLEMS_URL), function(e, resp, body) { if (e) return cb(e); if (resp.statusCode != 200) return cb('HTTP failed:' + resp.statusCode); var $ = cheerio.load(body); var problems = $('#problemList tbody tr').map(function(){ var tds = $(this).children(); var problem = { state: $(tds[0]).children('span').attr('class'), id: $(tds[1]).text(), name: $(tds[2]).children('a').text(), link: $(tds[2]).children('a').attr('href'), percent: $(tds[3]).text(), level: $(tds[6]).text() }; // fixup problem attributes problem.key = _.last(_.compact(problem.link.split('/'))); problem.link = config.BASE_URL + problem.link; return problem; }).get(); return cb(null, problems); }); }; leetcode_client.getProblem = function(problem, cb) { request(problem.link, function(e, resp, body){ if (e) return cb(e); if (resp.statusCode != 200) return cb('HTTP failed:' + resp.statusCode); var $ = cheerio.load(body); var info = $('div[class="question-info text-info"] ul li strong'); problem.desc = $('meta[property="og:description"]').attr('content'); problem.total_ac = $(info[0]).text(); problem.total_submit = $(info[1]).text(); return cb(null, problem); }); }; function getCookie(resp, name) { var cookies = resp.headers['set-cookie']; if (!cookies) return null; for (var i=0; i