GithubHelp home page GithubHelp logo

pombredanne / django-browsecap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ella/django-browsecap

1.0 1.0 0.0 210 KB

Browsecap is a simple library for django for detecting browser type. It can detect mobile browsers and crawlers.

License: BSD 3-Clause "New" or "Revised" License

Python 98.69% Ruby 1.31%

django-browsecap's Introduction

Django browsecap

Browsecap is a simple library for django for detecting browser type.

The main interface consists of two function in browsecap.browser:

is_mobile: returns True if the given user agent is a known mobile browser

is_crawler: returns True if the given user agent is a known crawler

MobileRedirectMiddleware

For your convenience there is also a middleware that automatically redirects all mobile users to alternate domain.

To use just add browsecap.middleware.MobileRedirectMiddleware to your settings.MIDDLEWARE_CLASSES and define a MOBILE_DOMAIN that you want your mobile users redirected to. Note that the value must contain full path including the protocol (http://)

The middleware sets ismobile cookie to value 1 and can be overriden by deleting that cookie setting isbrowser cookie to 1.

Internals

Browsecap works by parsing the browscap.ini file and storing a list of browsers as regexps in memory. Each user agent to be checked is then matched against the set of regexps until we run out (False) or a match is found (True). The result is stored in a dictionary to speedup further processing of the same user agent (in our experience, 200k users only have around 8k distinct user agents, so caching works).

Performance of the matchig is adequate and shouldn't slow down the request processing even if used every time (middleware), the only thing that is somewhat slow (under a second on a laptop) is parsing the browscap.ini file. This is done only when the module is first loaded and stores it's results in cache so that start of the next thread/process should not be hindered.

You can provide your own browscap.ini file by setting BROWSCAP_DIR in django settings pointing to a directory containing the file.

Credits

It is based on henning's snippet #267 - http://www.djangosnippets.org/snippets/267/

Thank you very much for the great work!

django-browsecap's People

Contributors

almad avatar honzakral avatar kvbik avatar pombredanne avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.