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.
Usage :: node.js
varuaParser=require('ua-parser');varua=uaParser.parse(navigator.userAgent);console.log(ua.tostring());// -> "Safari 5.0.1"console.log(ua.toVersionString());// -> "5.0.1"console.log(ua.family);// -> "Safari"console.log(ua.major);// -> 5console.log(ua.minor);// -> 0console.log(ua.patch);// -> 1# Install this into python site_packages like so:## python setup.py install## Now you're good to go.fromua_parserimportuser_agent_parser# On the server, you could use a WebOB request object.user_agent_string=request.META.get('HTTP_USER_AGENT')
# For demonstration purposes, though an iPhone ...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'# Get back a big dictionary of all the goodies.result_dict=user_agent_parser.Parse(user_agent_string)
printresult_dict['user_agent']
# {'major': '5', 'minor': '1', 'family': 'Mobile Safari', 'patch': None}printresult_dict['os']
# {'major': '5', 'patch_minor': None, 'minor': '1', 'family': 'iOS', 'patch': None}printresult_dict['device']
# {'is_spider': False, 'is_mobile': True, 'family': 'iPhone'}importua_parser.Parser;
importua_parser.Client;
...
StringuaString = "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";
ParseruaParser = newParser();
Clientc = uaParser.parse(uaString);
System.out.println(c.userAgent.family); // => "Mobile Safari"System.out.println(c.userAgent.major); // => "5"System.out.println(c.userAgent.minor); // => "1"System.out.println(c.os.family); // => "iOS"System.out.println(c.os.major); // => "5"System.out.println(c.os.minor); // => "1"System.out.println(c.device.family); // => "iPhone"System.out.println(c.device.isMobile); // => trueSystem.out.println(c.device.isSpider); // => falsePlease refer to the README in the php directory. 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.