-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (97 loc) · 2.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*!
* uitmuntend
* Copyright(c) 2014 Felix Fischer <[email protected]>
* MIT Licensed
*/
"use strict"
var _ = require('lodash')
, http = require('follow-redirects').http
, ds_cache = require('ds-cache')
, cheerio = require('cheerio')
, iconv = require('iconv-lite')
var cache = new ds_cache({
limit_bytes: '5M',
auto_save: true
})
/**
* `search()` will query uitmuntend.de or use cached results
*
* @param {String} query
* @param {Function} callback
*/
function search(query, callback){
var date = new Date()
var options = {
host: 'www.uitmuntend.de',
path: '/search.html?search=' + escape(query),
agent: false,
info: {query: query, timestamp: date.toUTCString()}
}
var cached = cache.get(query)
if (cached) callback(cached)
else
load(options, function(body){
var res = parse(body)
res.options = options
cache.set(query, res)
callback(res)
})
}
/**
* `load()` is a wrapper for http.get
*
* @param {Object} options
* @param {Function} callback
*/
function load(options, callback){
var req = http.get(options, function(res){
var bodyChunks = []
res.on('data', function(chunk) {
bodyChunks.push(chunk)
}).on('end', function() {
callback(Buffer.concat(bodyChunks))
})
})
req.on('error', function(e) {callback(e, options)})
}
/**
* `parse()` extracts results from html and returns a nice object
*
* example:
*
* { results: [ {nl: woord, de: Wort, type: 'direkte Treffer'}, ... ] }
*
* @param {String} body
* @returns {Object}
*/
function parse(body) {
var $ = cheerio.load(iconv.decode(body, 'iso-8859-1'))
var out = { results: [], translations: [] }
var heading = null
$('.t2 tbody tr').each(function(i, el){
var cells = $(this).children()
var rowType = _.pluck(cells, 'name').toString()
switch (rowType) {
case 'th': // section header
heading = $(cells).text()
break
case 'td,td': // actual result
out.results.push({
nl: $(cells[0]).text(),
de: $(cells[1]).text(),
type: heading
})
// for backwards compatibility
out.translations.push({
original: $(cells[0]).text(),
translation: $(cells[1]).text()
})
break
}
})
return out
}
// expose functions
exports.search = exports.query = search
exports.parse = parse
exports.load = load