|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var cheerio = require('cheerio'), |
| 4 | + fs = require('fs'), |
| 5 | + url = require('url'), |
| 6 | + get = require('get'), |
| 7 | + _ = require('underscore'), |
| 8 | + request = require('request'); |
| 9 | + |
| 10 | +function start() { |
| 11 | + var u = 'http://www.dcregs.dc.gov/Search/DCMRSearchByTitle.aspx'; |
| 12 | + request(u, function(err, reponse, body) { |
| 13 | + if (err) throw err; |
| 14 | + |
| 15 | + var $ = cheerio.load(body); |
| 16 | + var togo = []; |
| 17 | + var links = $('a').each(function(i, a) { |
| 18 | + if (a.attribs.href.match(/\/Gateway\/TitleHome\.aspx\?/g)) { |
| 19 | + togo.push(url.resolve(u, a.attribs.href)); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + togo.map(chapterhome); |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function chapterhome(u) { |
| 28 | + request(u, function(err, reponse, body) { |
| 29 | + if (err) throw err; |
| 30 | + console.log('to chapter step'); |
| 31 | + |
| 32 | + var $ = cheerio.load(body); |
| 33 | + var togo = []; |
| 34 | + var links = $('a').each(function(i, a) { |
| 35 | + if (a.attribs.href.match(/ChapterHome\.aspx\?/)) { |
| 36 | + togo.push(url.resolve(u, a.attribs.href)); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + togo.map(rulehome); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function rulehome(u) { |
| 45 | + request(u, function(err, reponse, body) { |
| 46 | + if (err) throw err; |
| 47 | + |
| 48 | + console.log('to rule step'); |
| 49 | + var $ = cheerio.load(body); |
| 50 | + var togo = []; |
| 51 | + var links = $('a').each(function(i, a) { |
| 52 | + if (a.attribs.href.match(/RuleHome\.aspx\?/)) { |
| 53 | + togo.push(url.resolve(u, a.attribs.href)); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + togo.map(download); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function download(u) { |
| 62 | + request(u, function(err, reponse, body) { |
| 63 | + if (err) throw err; |
| 64 | + |
| 65 | + console.log('to dl step'); |
| 66 | + var $ = cheerio.load(body); |
| 67 | + var togo = []; |
| 68 | + var links = $('a').each(function(i, a) { |
| 69 | + // LOL |
| 70 | + if (a.attribs.href && a.attribs.href.match(/Download\.aspx\?/)) { |
| 71 | + var title = $('title').text().replace(/\r\n/, ''); |
| 72 | + togo.push([title, url.resolve(u, a.attribs.href)]); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + togo.map(dl); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +var i = 0; |
| 81 | + |
| 82 | +function dl(u) { |
| 83 | + var id = i++; |
| 84 | + get(u[1]).toDisk('docs/' + i + '.doc', function(err) { |
| 85 | + if (err) console.log(err); |
| 86 | + }); |
| 87 | + fs.writeFileSync('docs/' + i + '.title', u[0]); |
| 88 | +} |
| 89 | + |
| 90 | +start(); |
0 commit comments