Skip to content
A multi-language port of Browserscope's user agent parser.
Java Perl PHP C# Python C++ Other
Pull request Compare This branch is 766 commits behind master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
d
haskell
java
js
php
py/ua_parser
test_resources
tests
.gitignore
.npmignore
README.markdown
package.json
regexes.yaml
setup.py

README.markdown

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.

Usage :: node.js

var r = require('ua-parser').parse(navigator.userAgent);

console.log(r.ua.toString());        // -> "Safari 5.0.1"
console.log(r.ua.toVersionString()); // -> "5.0.1"
console.log(r.ua.family)             // -> "Safari"
console.log(r.ua.major);             // -> "5"
console.log(r.ua.minor);             // -> "0"
console.log(r.ua.patch);             // -> "1"

console.log(r.os.toString());        // -> "iOS 5.1"
console.log(r.os.toVersionString()); // -> "5.1"
console.log(r.os.family)             // -> "iOS"
console.log(r.os.major);             // -> "5"
console.log(r.os.minor);             // -> "1"
console.log(r.os.patch);             // -> null

console.log(r.device.toString());    // -> "iPhone"
console.log(r.device.family);        // -> "iPhone"
console.log(r.device.isMobile);      // -> true
console.log(r.device.isSpider)       // -> false

Note if you're only interested in one of the ua, device or os objects, you will getter better performance by using the more specific methods (uaParser.parseUA, uaParser.parseOS and uaParser.parseDevice respectively), e.g.:

var p = require('ua-parser');

console.log(p.parseUA(navigator.userAgent).toString());
// -> "Safari 5.0.1"
console.log(p.parseOS(navigator.userAgent).toString());
// -> "iOS 5.1"
console.log(p.parseDevice(navigator.userAgent).toString());
// -> "iPhone"

Usage :: python

# Install this into python site_packages like so:
#
# python setup.py install
#
# Now you're good to go.

from ua_parser import user_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)

print result_dict['user_agent']
# {'major': '5', 'minor': '1', 'family': 'Mobile Safari', 'patch': None}

print result_dict['os']
# {'major': '5', 'patch_minor': None, 'minor': '1', 'family': 'iOS', 'patch': None}

print result_dict['device']
# {'is_spider': False, 'is_mobile': True, 'family': 'iPhone'}

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); // => "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);  // => true
  System.out.println(c.device.isSpider);  // => false

Usage :: php

require("UAParser.php");
$ua = UA::parse();

print $ua->family;        // Chrome (can also use $ua->browser)
print $ua->major;         // 16
print $ua->minor;         // 0
print $ua->patch;         // 912 (can also use $ua->build)
print $ua->browserFull;   // Chrome 16.0.912
print $ua->version;       // 16.0.912

print $ua->os;            // Mac OS X
print $ua->osMajor;       // 10
print $ua->osMinor;       // 6
print $ua->osPatch;       // 8 (can also use $ua->osBuild)
print $ua->osFull;        // Mac OS X 10.6.8
print $ua->osVersion;     // 10.6.8

print $ua->full;          // Chrome 16.0.912/Mac OS X 10.6.8

// in select cases the device information will also be captured

print $ua->device;        // Palm Pixi
print $ua->deviceMajor;   // 1
print $ua->deviceMinor;   // 0
print $ua->deviceFull;    // Palm Pixi 1.0
print $ua->deviceVersion; // 1.0

// some other generic boolean options

print $ua->isMobile;      // true or false
print $ua->isSpider;      // true or false
print $ua->isComputer;    // true or false

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.

Something went wrong with that request. Please try again.