Skip to content
forked from tobie/ua-parser

A JS port of Browserscope's user agent parser.

Notifications You must be signed in to change notification settings

sjiang/ua-parser

 
 

Repository files navigation

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 uaParser = require('ua-parser');
var ua = 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);
// -> 5

console.log(ua.minor);
// -> 0

console.log(ua.patch);
// -> 1

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

Please refer to the README in the php directory. 

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.

About

A JS port of Browserscope's user agent parser.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 69.4%
  • Java 17.0%
  • Python 11.8%
  • JavaScript 1.1%
  • Perl 0.7%