-
Notifications
You must be signed in to change notification settings - Fork 1
/
brreg-regnr-search.js
81 lines (65 loc) · 2.34 KB
/
brreg-regnr-search.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
var http = require('http');
var jsdom = require('jsdom');
var personSearch = function(text){
var personSearch = new RegExp('\ +([a-zA-ZæøåÆØÅ]+)([a-zA-ZæøåÆØÅ\ ]+)\n\ +([0-9a-zA-ZæøåÆØÅ\ ]*)\n* \ +([0-9][0-9][0-9][0-9])\ ([a-zA-ZæøåÆØÅ]+)', 'g');
var lienSearch = new RegExp('NOK ([0-9.,]*)\n*.*\n\ +([0-9a-zA-ZæøåÆØÅ\ -]+)Org.nr\ ([0-9\ ]+)', 'g');
var matches = [];
var liensMatches = [];
var currentMatch = personSearch.exec(text);
while (currentMatch !== null){
matches.push(currentMatch);
currentMatch = personSearch.exec(text);
}
var currentLienMatch = lienSearch.exec(text);
while (currentLienMatch !== null){
liensMatches.push(currentLienMatch);
currentLienMatch = lienSearch.exec(text);
}
var liens = liensMatches.map(function(match){
return {
amount: match[1].trim().replace('.', '').replace(',', '.'),
bankName: match[2].trim(),
bankOrgNr: match[3].replace(/ /g, '')
}
});
var persons = matches.map(function(match){
return {
firstName: match[2].trim(),
lastName: match[1].trim(),
address: match[3].trim(),
zip: match[4].trim(),
city: match[5].trim()
}
});
var liens
return {
persons: persons,
liens: liens
};
}
var extractLog = function(logUrl, callback){
var baseUrl = 'http://w2.brreg.no/motorvogn/';
var result = null;
jsdom.env(baseUrl + logUrl, ["https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"], function (errors, window) {
var text = window.$('pre').html();
var htmlLineEndings = new RegExp('<br>', 'g');
text = text.replace(htmlLineEndings, '\n');
text = text.substr(text.indexOf("foretak:") + 8);
result = personSearch(text);
if(typeof(callback) == "function") callback(result);
});
}
var search = function(query, callback){
var url = 'http://w2.brreg.no/motorvogn/heftelser_motorvogn.jsp?regnr=' + query;
jsdom.env(url, ["https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"], function (errors, window) {
var logUrl = window.$('table tr a').attr('href');
if(logUrl.indexOf('dagbokutskrift') === -1){
if(typeof(callback) == "function") callback(null);
return;
}
extractLog(logUrl, function(result){
if(typeof(callback) == "function") callback(result);
});
});
}
module.exports = { search: search };