|
| 1 | +var request = require('request'), |
| 2 | + cheerio = require('cheerio'); |
| 3 | + |
| 4 | +var SEARCH_URL = 'http://parts.igem.org/wiki/index.php?title=Special:Search&ns0=1&redirs=0&search=' |
| 5 | +var SUFFIX_URL = '&limit=500&offset=0' |
| 6 | + |
| 7 | +function queryRegistry(keyword, cb) { |
| 8 | + request({ |
| 9 | + url: SEARCH_URL + keyword + SUFFIX_URL |
| 10 | + }, function(err, res, body) { |
| 11 | + if (!err && res.statusCode === 200) { |
| 12 | + $ = cheerio.load(body) |
| 13 | + // Now we basically have core jQuery through cheerio |
| 14 | + |
| 15 | + // Experiment with inspect element and console logging |
| 16 | + links = $('li') |
| 17 | + |
| 18 | + //Will obviously be an object in good version |
| 19 | + data = new Array() |
| 20 | + Object.keys(links).forEach(function(key) { |
| 21 | + |
| 22 | + // for (var i = 0; i< links[key].children.length; i++){ |
| 23 | + // if href |
| 24 | + // } |
| 25 | + // if (links[key] && links[key].children[0] && links[key].children[0].attribs && links[key].children[0].attribs.title) |
| 26 | + // console.log(links[key].children[0].attribs.title) |
| 27 | + // data.push(links[key].children[0].attribs.title) |
| 28 | + if (links[key] !== undefined && links[key].children !== undefined && links[key].children[0] !== undefined && links[key].children[0].attribs){ |
| 29 | + s = links[key].children[0].attribs.title |
| 30 | + index = s.indexOf("BBa") |
| 31 | + if (index >= 0){ |
| 32 | + s = s.slice(index) |
| 33 | + splitted = s.split(" ") |
| 34 | + s = splitted[0] + "_" + splitted[1] |
| 35 | + |
| 36 | + if(s.indexOf(":")>=0) |
| 37 | + s = s.slice(0, s.indexOf(":")) |
| 38 | + data.push(s) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + // console.log(links[key].children[0]) |
| 45 | + // console.log("--------\n") |
| 46 | + // if (links[key].attribs) |
| 47 | + // data.push(links[key].attribs.href) |
| 48 | + }) |
| 49 | + |
| 50 | + |
| 51 | + // Give nicely formatted json to callback |
| 52 | + cb(data); |
| 53 | + } else { |
| 54 | + console.log(err); |
| 55 | + console.log(res.statusCode); |
| 56 | + } |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// ==== MAIN ==== |
| 61 | + |
| 62 | +var keyword = process.argv[2] |
| 63 | +queryRegistry(keyword, function(data) { |
| 64 | + console.log(data.length) |
| 65 | +}) |
0 commit comments