-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchecker.js
98 lines (86 loc) · 2.53 KB
/
checker.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
var http = require('http');
var fs = require('fs');
eval(fs.readFileSync('./src/parser.js', 'utf8'));
eval(fs.readFileSync('./src/matcher.js', 'utf8'));
eval(fs.readFileSync('./src/scorer.js', 'utf8'));
eval(fs.readFileSync('./src/bestmatch.js', 'utf8'));
console.log("SemiDemi Checker");
function downloadFile (options, succ, err) {
http.get(options).on('response', function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
succ(body);
});
}).on('error', function(e) {
err(e.message);
});;
}
var matchers = SemiDemi.parse(fs.readFileSync('./tvs.demi', 'utf8'));
var normaliseDemiValue = function (v) {
return v.toLowerCase().replace(/[^a-z0-9]/g, "_");
}
function semidemi (ua) {
var result = SemiDemi.bestMatch(matchers, ua);
if (!result) { return; }
return result[0].brand+"-"+result[0].model;
}
function parseDemi (response) {
var brandMatch = response.match(/<dt>brand<\/dt>\s*<dd><span class=\"string\">([^<]+)<\/span><\/dd>/);
var modelMatch = response.match(/<dt>model<\/dt>\s*<dd><span class=\"string\">([^<]+)<\/span><\/dd>/);
if (!brandMatch || !modelMatch) {
return;
}
return normaliseDemiValue(brandMatch[1]) + "-" + normaliseDemiValue(modelMatch[1]);
}
function demi (ua, succ, err) {
var options = {
host: 'www.test.bbc.co.uk',
path: '/frameworks/test/demi/php',
headers: {'user-agent': ua}
};
downloadFile(options, function (data) {
succ(parseDemi(data.toString()));
}, err);
}
function testUA (ua, idx, done) {
demi(ua, function (demi) {
if (semidemi(ua) === demi) {
process.stdout.write(".");
} else {
process.stdout.write("x");
console.log("\n"+idx+": FAILED: " + ua + "\nSemiDemi: " + semidemi(ua) + "\nDemi : " + demi);
}
done();
}, function (err) {
process.stdout.write("e");
console.log("\n"+idx+": ERROR: " + ua + "\nDemi error: " + err);
done();
});
}
function runTests (uas) {
var start = 270;//process.argv[2] || 0;
var end = 275;//process.argv[3] || lines.length;
var lines = uas.split(/[\r\n]+/);
console.log("Num UAs: " + lines.length);
var i = 0;
var doNextTest = function () {
var next = function () {
i++;
if (i < lines.length) {
doNextTest();
} else {
console.log("Finished");
}
};
if (i >= start && i <= end) {
testUA(lines[i], i, next);
} else {
next();
}
};
doNextTest();
}
runTests(fs.readFileSync('testdata/checker_uas.txt', 'utf8'));