gpt4 book ai didi

javascript - 设备用户代理检测| Node.js | express 用户代理 |解析数据

转载 作者:行者123 更新时间:2023-12-03 07:35:27 24 4
gpt4 key购买 nike

我有一个看起来像这样的日志:

GET /nands-adblock-logs/a.png HTTP/1.1  http://www.ok.co.uk/celebrity-news/exclusive-stephanie-davis-jeremy-mcconnell-moving-in-together-marriage   Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1

我的代码如下所示,用于解析数据并返回用户代理:

var fs = require('fs'),
readline = require('readline'),
useragent = require('express-useragent');


function processFile(inputFile) {
console.log("INPUT FILE", inputFile);
instream = fs.createReadStream(inputFile),
outstream = new (require('stream'))(),
rl = readline.createInterface(instream, outstream);

rl.on('line', function (line) {
var ua = useragent.parse(line);
console.log("PARSED USER AGENT = ", ua);
});

rl.on('close', function (line) {
console.log(line);
console.log('done reading file.');
});
}

processFile(process.argv[2]);

输出如下所示:

PARSED USER AGENT =  { isMobile: true,
isTablet: false,
isiPad: false,
isiPod: false,
isiPhone: false,
isAndroid: true,
isBlackberry: false,
isOpera: false,
isIE: false,
isEdge: false,
isIECompatibilityMode: false,
isSafari: false,
isFirefox: false,
isWebkit: false,
isChrome: true,
isKonqueror: false,
isOmniWeb: false,
isSeaMonkey: false,
isFlock: false,
isAmaya: false,
isEpiphany: false,
isDesktop: false,
isWindows: false,
isLinux: true,
isLinux64: false,
isMac: false,
isChromeOS: false,
isBada: false,
isSamsung: false,
isRaspberry: false,
isBot: false,
isCurl: false,
isAndroidTablet: false,
isWinJs: false,
isKindleFire: false,
isSilk: false,
isCaptive: false,
isSmartTV: false,
silkAccelerated: false,
browser: 'Chrome',
version: '42.0.2311.133',
os: 'Linux',
platform: 'Android',
geoIp: {},
source: '"GET /nands-adblock-logs/a.png HTTP/1.1","http://www.ok.co.uk/","Mozilla/5.0 (Linux; Android 5.1.1; D2303 Build/18.6.A.0.175) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/42.0.2311.133 Mobile Safari/537.36"' }

我想要的是只返回以下属性:

设备类型,浏览器,操作系统

我怎样才能只检索这些值?我不太关心返回 false 的值。

最佳答案

我会使用lodash为此:

_.pick(data, function(value, key) {
return key === 'browser' || key === 'os' || value === true;
})

var result = _.pick(data, function(value, key) {
return key === 'browser' || key === 'os' || value === true;
});

document.write('<pre>' + JSON.stringify(result, null, 4) + '</pre>')
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script>
<script>
var data = {
isMobile: true,
isTablet: false,
isiPad: false,
isiPod: false,
isiPhone: false,
isAndroid: true,
isBlackberry: false,
isOpera: false,
isIE: false,
isEdge: false,
isIECompatibilityMode: false,
isSafari: false,
isFirefox: false,
isWebkit: false,
isChrome: true,
isKonqueror: false,
isOmniWeb: false,
isSeaMonkey: false,
isFlock: false,
isAmaya: false,
isEpiphany: false,
isDesktop: false,
isWindows: false,
isLinux: true,
isLinux64: false,
isMac: false,
isChromeOS: false,
isBada: false,
isSamsung: false,
isRaspberry: false,
isBot: false,
isCurl: false,
isAndroidTablet: false,
isWinJs: false,
isKindleFire: false,
isSilk: false,
isCaptive: false,
isSmartTV: false,
silkAccelerated: false,
browser: 'Chrome',
version: '42.0.2311.133',
os: 'Linux',
platform: 'Android',
geoIp: {},
source: '"GET /nands-adblock-logs/a.png HTTP/1.1","http://www.ok.co.uk/","Mozilla/5.0 (Linux; Android 5.1.1; D2303 Build/18.6.A.0.175) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/42.0.2311.133 Mobile Safari/537.36"'
};
</script>

使用最新版本,您必须将 pick 替换为 pickBy,但 plnkr 只有 3.10 版本。

关于javascript - 设备用户代理检测| Node.js | express 用户代理 |解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com