UNPKG

1.35 kBJavaScriptView Raw
1'use strict';
2var request = require('request');
3var parseString = require('xml2js').parseString;
4var fs = require('fs');
5
6var userAgentsOrgUrl = 'http://www.user-agents.org/allagents.xml';
7// Doesn't have hardly any bots. Boo.
8//var secondUrl = 'http://techpatterns.com/downloads/firefox/useragentswitcher.xml';
9
10var ua_list = {};
11var exports = module.exports = {};
12
13// This can be called manually and piped into botlist.json to update the botlist
14var updateXML = function() {
15 fetchData(userAgentsOrgUrl, function(err, result) {
16 var uas = result['user-agents']['user-agent'];
17 uas.forEach(function(ua) {
18 ua_list[ua.String[0]] = 1;
19 });
20
21 // Write the list out
22 fs.writeFile("botlist.json", JSON.stringify(ua_list), function(err) {
23 if (err) {
24 console.log("There was an error writing the list: "+err);
25 } else {
26 console.log("Wrote botlist");
27 }
28 });
29 });
30}
31
32var fetchData = function(url, callback) {
33 request(url, function (error, response, xml) {
34 if (error) {
35 callback(error);
36 } else if (response.statusCode != 200) {
37 callback("Server did not return a 200! Status code was "+response.statusCode);
38 } else if (!error && response.statusCode == 200) {
39 parseString(xml, function(err, result) {
40 callback(result);
41 });
42 }
43 });
44}
45
46
47updateXML();