|
1 | 1 | var cheerio = require('cheerio'), |
2 | | - request = require('request'); |
| 2 | + request = require('request'), |
| 3 | + _ = require('underscore'); |
3 | 4 |
|
4 | 5 | var cache = require('./cache'), |
5 | 6 | config = require('./config'); |
6 | 7 |
|
7 | 8 | var problems = {}; |
8 | 9 |
|
9 | 10 | problems.getAll = function(cb) { |
10 | | - var items = cache.get('all'); |
11 | | - if (items) return cb(null, items); |
| 11 | + var cached = cache.get('all'); |
| 12 | + if (cached) return cb(null, cached); |
12 | 13 |
|
13 | 14 | // cache miss, retrieve it now |
14 | 15 | request(config.PROBLEMS_URL, function(err, resp, body) { |
15 | 16 | if (err) return cb(err); |
16 | 17 | if (resp.statusCode != 200) return cb('Invalid HTTP response: ' + resp.statusCode); |
17 | 18 |
|
18 | 19 | var $ = cheerio.load(body); |
19 | | - items = $('#problemList tbody tr').map(function(){ |
| 20 | + var items = $('#problemList tbody tr').map(function(){ |
20 | 21 | var tds = $(this).children(); |
21 | | - return { |
| 22 | + var item = { |
22 | 23 | id: $(tds[1]).text(), |
23 | 24 | name: $(tds[2]).children('a').text(), |
24 | | - link: $(tds[2]).children('a').attr('href'), |
| 25 | + link: config.BASE_URL + $(tds[2]).children('a').attr('href'), |
25 | 26 | percent: $(tds[3]).text(), |
26 | 27 | level: $(tds[6]).text() |
27 | 28 | }; |
| 29 | + item.key = _.last(_.compact(item.link.split('/'))) |
| 30 | + return item; |
28 | 31 | }).get(); |
29 | 32 |
|
30 | 33 | cache.set('all', items); |
31 | | - |
32 | 34 | return cb(null, items); |
33 | 35 | }); |
34 | 36 | }; |
35 | 37 |
|
| 38 | +problems.getOne = function(item, cb) { |
| 39 | + var cached = cache.get(item.key); |
| 40 | + if (cached) return cb(null, cached); |
| 41 | + |
| 42 | + request(item.link, function(err, resp, body){ |
| 43 | + if (err) return cb(err); |
| 44 | + if (resp.statusCode != 200) return cb('Invalid HTTP response: ' + resp.statusCode); |
| 45 | + |
| 46 | + var $ = cheerio.load(body); |
| 47 | + item.desc = $('meta[property="og:description"]').attr('content'); |
| 48 | + var info = $('div[class="question-info text-info"] ul li strong'); |
| 49 | + item.total_ac = $(info[0]).text(); |
| 50 | + item.total_submit = $(info[1]).text(); |
| 51 | + |
| 52 | + cache.set(item.key, item); |
| 53 | + return cb(null, item); |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +problems.searchOne = function(key, cb) { |
| 58 | + var self = this; |
| 59 | + |
| 60 | + this.getAll(function(err, items){ |
| 61 | + if (err) return cb(err); |
| 62 | + |
| 63 | + var item = _.find(items, function(x){ |
| 64 | + return x.id == key || x.name == key || x.key == key; |
| 65 | + }); |
| 66 | + return self.getOne(item, cb); |
| 67 | + }); |
| 68 | +}; |
| 69 | + |
36 | 70 | module.exports = problems; |
0 commit comments