This example shows how to use 51Degrees device detection to process
a file containing many User-Agents.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
This example require module 'n-readlines' to operate. Please install the module before running the example, by using the following command:
const events = require('events');
const LineReader = require('n-readlines');
require((process.env.directory || __dirname) +
'/../../../deviceDetectionOnPremisePipelineBuilder');
const ExampleUtils = require(__dirname +
'/../exampleUtils').ExampleUtils;
const LITE_V_4_1_HASH = '51Degrees-LiteV4.1.hash';
const UA_CSV = '20000 User Agents.csv';
const sliceLen = process.env.JEST_WORKER_ID === undefined ? 2 : process.argv.length;
const args = process.argv.slice(sliceLen);
const datafile = args.length > 0 ? args[0] :
ExampleUtils.
findFile(LITE_V_4_1_HASH);
const uafile = args.length > 1 ? args[1] :
ExampleUtils.findFile(UA_CSV);
const outputFile = (process.env.directory || __dirname) +
'/batch-processing-example-results.csv';
const fs = require('fs');
if (!fs.existsSync(datafile)) {
console.error('Failed to find a device detection ' +
'data file. Make sure the device-detection-data ' +
'submodule has been updated by running ' +
'`git submodule update --recursive`.');
throw ("No data file at '" + datafile + "'");
}
if (!fs.existsSync(uafile)) {
console.error('Failed to find a User-Agents ' +
'file. Make sure the device-detection-data ' +
'submodule has been updated by running ' +
'`git submodule update --recursive`.');
throw ("No User-Agents file at '" + datafile + "'");
}
const maxUserAgents = 20;
const writeStream = fs.createWriteStream(outputFile);
const eventEmitter = new events.EventEmitter();
eventEmitter.on('FinishProcessing', () => {
if (typeof deleteOutputFile !== 'undefined') {
if (fs.existsSync(outputFile)) {
fs.unlink(outputFile, function (err) {
if (err) throw err;
console.log('Delete output file ' + outputFile);
});
}
}
console.log('Output written to ' + outputFile);
});
let userAgentsProcessed = 0;
performanceProfile: 'MaxPerformance',
dataFile: datafile,
autoUpdate: false
}).build();
pipeline.on('error', console.error);
const processUA = async function (userAgent) {
const flowData = pipeline.createFlowData();
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
const ismobile = flowData.device.ismobile;
const platformname = flowData.device.platformname;
const platformversion = flowData.device.platformversion;
const outputLine = userAgent +
', ' + (ismobile.hasValue ? ismobile.value : '') +
', ' + (platformname.hasValue ? platformname.value : '') +
', ' + (platformversion.hasValue ? platformversion.value : '') +
'\n';
writeStream.write(outputLine);
if (++userAgentsProcessed === maxUserAgents) {
eventEmitter.emit('FinishProcessing');
}
};
const liner = new LineReader(uafile);
let line;
let linesCounter = 0;
while ((line = liner.next()) && linesCounter++ < maxUserAgents) {
processUA(line.toString('utf8').replace(/\r?\n|\r/g, ''));
}
liner.close();