ua-parser
ua-parser
is a multi-language port of BrowserScope's user agent string parser.
The crux of the original parser--the data collected by Steve Souders over the years--has been extracted into a separate YAML file so as to be reusable as is by implementations in other programming languages.
ua-parser
is just a small wrapper around this data.
var uaParser = require('ua-parser');
var ua = uaParser.parse(navigator.userAgent);
console.log(ua.tostring());
console.log(ua.toVersionString());
console.log(ua.family);
console.log(ua.major);
console.log(ua.minor);
console.log(ua.patch);
Usage :: python
from ua_parser import user_agent_parser
user_agent_string = request.META.get('HTTP_USER_AGENT')
user_agent_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
result_dict = user_agent_parser.Parse(user_agent_string)
print result_dict['user_agent']
print result_dict['os']
print result_dict['device']
Usage :: java
import ua_parser.Parser;
import ua_parser.Client;
...
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";
Parser uaParser = new Parser();
Client c = uaParser.parse(uaString);
System.out.println(c.userAgent.family);
System.out.println(c.userAgent.major);
System.out.println(c.userAgent.minor);
System.out.println(c.os.family);
System.out.println(c.os.major);
System.out.println(c.os.minor);
System.out.println(c.device.family);
System.out.println(c.device.isMobile);
System.out.println(c.device.isSpider);
Usage :: php
require("UAParser.php");
$ua = UA::parse();
print $ua->family;
print $ua->major;
print $ua->minor;
print $ua->patch;
print $ua->browserFull;
print $ua->version;
print $ua->os;
print $ua->osMajor;
print $ua->osMinor;
print $ua->osPatch;
print $ua->osFull;
print $ua->osVersion;
print $ua->full;
print $ua->device;
print $ua->deviceMajor;
print $ua->deviceMinor;
print $ua->deviceFull;
print $ua->deviceVersion;
print $ua->isMobile;
print $ua->isSpider;
print $ua->isComputer;
More information is available in the README in the PHP directory
Usage :: D
import UaParser;
import std.stdio;
void main() {
auto agent = UaParser.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3");
std.stdio.writeln(agent.browser.family); // Mobile Safari
std.stdio.writeln(agent.browser.major); // 5
std.stdio.writeln(agent.browser.minor); // 1
std.stdio.writeln(agent.browser.patch); // 0
std.stdio.writeln(agent.browser.toString); // Mobile Safari 5.1.0
std.stdio.writeln(agent.browser.toVersionString); // 5.1.0
std.stdio.writeln(agent.os.family); // iOS
std.stdio.writeln(agent.os.major); // 5
std.stdio.writeln(agent.os.minor); // 1
std.stdio.writeln(agent.os.patch); // 1
std.stdio.writeln(agent.os.toString); // iOS 5.1.1
std.stdio.writeln(agent.os.toVersionString); // 5.1.1
std.stdio.writeln(agent.toFullString); // Mobile Safari 5.1.0/iOS 5.1.1
std.stdio.writeln(agent.device.family); // iPhone
std.stdio.writeln(agent.isMobile); // true
std.stdio.writeln(agent.isSpider); // false
}
License
The data contained in regexes.yaml
is Copyright 2009 Google Inc. and available under the Apache License, Version 2.0.
The original python code is Copyright 2008 Google Inc. and is available under the Apache License, Version 2.0.
The JS port is Copyright 2010 Tobie Langel and is available under your choice of MIT or Apache Version 2.0 license.
The PHP port is Copyright (c) 2011-2012 Dave Olsen and is available under the MIT license.
The Java port is Copyright (c) 2012 Twitter, Inc and is available under the Apache License, Version 2.0.
The D port is Copyright (c) 2012 Shripad K and is available under the MIT license.