Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import re
# Some mobile browsers which look like desktop browsers.
RE_MOBILE = re.compile(r"(iphone|ipod|blackberry|android|palm|windows\s+ce)", re.I)
RE_DESKTOP = re.compile(r"(windows|linux|os\s+[x9]|solaris|bsd)", re.I)
RE_BOT = re.compile(r"(spider|crawl|slurp|bot)")
def is_desktop(user_agent):
"""
Anything that looks like a phone isn't a desktop.
Anything that looks like a desktop probably is.
Anything that looks like a bot should default to desktop.
"""
return not bool(RE_MOBILE.search(user_agent)) and \
bool(RE_DESKTOP.search(user_agent)) or \
bool(RE_BOT.search(user_agent))
def get_user_agent(request):
# Some mobile browsers put the User-Agent in a HTTP-X header
return request.headers.get('HTTP_X_OPERAMINI_PHONE_UA') or \
request.headers.get('HTTP_X_SKYFIRE_PHONE') or \
request.headers.get('HTTP_USER_AGENT', '')
@EmmanuelOga

This comment has been minimized.

Copy link

EmmanuelOga commented Jun 7, 2011

ruby version for the lazy:

module UADetect
  RE_MOBILE = /mobile|iphone|ipod|blackberry|android|palm|windows\s+ce/i
  RE_DESKTOP = /windows|linux|os\s+[x9]|solaris|bsd/i
  RE_BOT = /spider|crawl|slurp|bot/i

  # Anything that looks like a phone isn't a desktop.
  # Anything that looks like a desktop probably is.
  # Anything that looks like a bot should default to desktop.
  def is_desktop?(user_agent)
    (user_agent !~ RE_MOBILE && user_agent =~ RE_DESKTOP) || user_agent =~ RE_BOT
  end

  def is_mobile?(user_agent)
    !is_desktop(user_agent)
  end

  # Some mobile browsers put the User-Agent in a HTTP-X header
  def get_user_agent(request)
    request.headers['HTTP_X_OPERAMINI_PHONE_UA'].presence ||
    request.headers['HTTP_X_SKYFIRE_PHONE'].presence ||
    request.headers['HTTP_USER_AGENT'].presence ||
    ""
  end
end
@cdceng

This comment has been minimized.

Copy link

cdceng commented Sep 20, 2012

Ruby version converted into a gem, for the lazier:

https://github.com/galori/simple-useragent

@mlohbihler

This comment has been minimized.

Copy link

mlohbihler commented Feb 26, 2013

The Java version:

import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

public class UserAgentUtils {
    private static final String HTTP_X_OPERAMINI_PHONE_UA = "X-Operamini-Phone-UA";
    private static final String HTTP_X_SKYFIRE_PHONE = "X-Skyfire-Phone";
    private static final String HTTP_USER_AGENT = "User-Agent";

    private static final Pattern mobile = Pattern.compile("(iphone|ipod|ipad|blackberry|android|palm|windows\\s+ce)",
            Pattern.CASE_INSENSITIVE);
    private static final Pattern desktop = Pattern.compile("(windows|linux|os\\s+[x9]|solaris|bsd)",
            Pattern.CASE_INSENSITIVE);
    private static final Pattern bot = Pattern.compile("(spider|crawl|slurp|bot)", Pattern.CASE_INSENSITIVE);

    public static String getUserAgent(HttpServletRequest request) {
        String ua = request.getHeader(HTTP_X_OPERAMINI_PHONE_UA);
        if (StringUtils.isBlank(ua))
            ua = request.getHeader(HTTP_X_SKYFIRE_PHONE);
        if (StringUtils.isBlank(ua))
            ua = request.getHeader(HTTP_USER_AGENT);
        if (StringUtils.isBlank(ua))
            ua = "";
        return ua;
    }

    public static boolean isDesktop(HttpServletRequest request) {
        return isDesktop(getUserAgent(request));
    }

    public static boolean isDesktop(String userAgent) {
        //
        // Anything that looks like a phone isn't a desktop.
        // Anything that looks like a desktop probably is.
        // Anything that looks like a bot should default to desktop.
        //
        return (!matches(mobile, userAgent) && matches(desktop, userAgent)) || matches(bot, userAgent);
    }

    private static boolean matches(Pattern p, String s) {
        return p.matcher(s).find();
    }

    public static boolean isMobile(HttpServletRequest request) {
        return isMobile(getUserAgent(request));
    }

    public static boolean isMobile(String userAgent) {
        return !isDesktop(userAgent);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.