-
Notifications
You must be signed in to change notification settings - Fork 1
/
hjp-bookmarklet.server.js
executable file
·191 lines (166 loc) · 4.96 KB
/
hjp-bookmarklet.server.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var cheerio = require('cheerio'),
url = require('url'),
request = require('request'),
qs = require('querystring'),
fs = require('fs'),
http = require('http');
//TODO: move it to some better caching mechanism
var cacheFilename = __dirname+'/'+'cache.json',
cacheContent;
var renderResponse = function (data, jsonpCallback) {
if (jsonpCallback!==undefined) { //jsonp
return jsonpCallback+'('+JSON.stringify(data)+');';
} else {
return JSON.stringify(data);
}
};
//gets in format: [{definition: definition, id: id}]
var handleResponse = function (document, word, jsonpCallback, response) {
var $ = cheerio.load(document),
idAttribute = 'id',
$definition = $('#definicija_frame'),
href,
params = {},
$natLinks = $('.natuknica .natlink'),
definitions = [],
$tds,
results = [];
if ($definition.length !== 0) {
console.log('single found');
//get id
href = $('.natuknica a').eq(0).attr('href');
if (href !== undefined) {
href = url.parse(href);
params = qs.decode(href.query);
}
$tds = $definition.find('table');
if ($tds.length === 1) { //multiple definitions
$tds.find('tr').each(function () {
definitions.push(this.find('td').eq(1).html().trim());
});
} else {
$definition.find('br').eq(0).remove();
definitions.push($definition.html().trim());
}
//save
results.push({
'word': $('#natuknica_raster_frame b').html().trim(),
'definition': definitions,
'id': params[idAttribute]
});
} else if ($natLinks.length !== 0) {
console.log('ambigious found');
$natLinks.each(function () {
var href = this.attr('href'),
$definition = this.parent(),
$word = $definition.find('b').eq(0),
params = {};
//get id
if (href !== undefined) {
href = url.parse(href);
params = qs.decode(href.query);
}
//remove link from definition
this.remove();
$word.remove();
//save
results.push({
'word': $word.html().trim(),
'definition': [$definition.html().trim()],
'id': params[idAttribute]
});
});
} else {
console.log('not found');
}
//save to cache
cacheContent = (cacheContent !== undefined)?cacheContent:{};
cacheContent[word] = results;
fs.writeFile(cacheFilename, JSON.stringify(cacheContent), function (error) {
if (error) {
console.error('Could not write to cache file:', cacheFilename);
//ignore
}
});
if (results.length !== 0 ) {
console.log('results from', 'service');
response.statusCode = 200;
} else {
response.statusCode = 404;
}
response.write(renderResponse({'results': results}, jsonpCallback));
response.end();
};
var searchByWord = function (word, jsonpCallback, response) {
var serviceUrl = 'http://hjp.znanje.hr/index.php?show=search',
queryParams = {
'word': word,
'search': undefined,
'definicija': 'on',
'postano': 'true'
};
request.post({
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
url: serviceUrl,
body: qs.stringify(queryParams)
}, function(error, serviceResponse, body) {
if(!error && serviceResponse.statusCode == 200){
handleResponse.call(this, body, word, jsonpCallback, response);
} else {
console.error('Error while getting: ', word, 'status:', serviceResponse.statusCode);
response.statusCode = 503;
response.write(renderResponse({'error': 'Service not available'}, jsonpCallback));
response.end();
}
});
};
//finds in format: [{definition: definition, id: id}]
var getDefinition = function (word, jsonpCallback, response) {
console.log('Looking for', word);
if (word !== '' && word.split(/\s+/).length <= 4) { //no more than 4 words
fs.readFile(cacheFilename, function (error, data) {
var results;
if (!error) {
try {
cacheContent = JSON.parse(data.toString());
} catch (e) {
console.error('Could not parse cache file:', cacheFilename);
//ignore
}
} else {
console.error('Could not read cache file:', cacheFilename);
//ignore
}
results = cacheContent[word];
if (cacheContent !== undefined && results!== undefined) {
console.log('results from', 'cache');
response.statusCode = (results.length > 0)?200:404;
response.write(renderResponse({'results': results}, jsonpCallback));
response.end();
} else {
searchByWord.call(this, word, jsonpCallback, response);
}
});
} else {
response.statusCode = 400;
response.end('{"error": "Parameter \''+wordAttribute+'\' has too many words."}');
}
};
//http server
http.createServer(function (request, response) {
var params = qs.decode(url.parse(request.url).query),
wordAttribute = 'word';
if (params['callback'] !== undefined) {
response.setHeader("Content-Type", "application/javascript");
} else {
response.setHeader("Content-Type", "application/json");
}
if (params[wordAttribute] === undefined) {
response.statusCode = 400;
response.end('{"error": "Parameter \''+wordAttribute+'\' is required."}');
} else {
getDefinition.call(this, params[wordAttribute], params['callback'], response);
}
}).listen(9615);