Skip to content

kvspb/browscap-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Browscap Library

Detect browser

import redis
import browscap
from browscap.cache.redis import RedisCache

ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
redis_cache = redis.Redis(db=5)
bc = browscap.Browscap(cache=RedisCache(redis_cache))
browser=bc.get_browser(ua)

Detect browser with asyncio

from pprint import pprint
import asyncio
import asyncio_redis
from browscap.aio import BrowscapAsync
from browscap.aio.cache.redis import RedisPoolAioCache

loop = asyncio.get_event_loop()

@asyncio.coroutine
def get_browser():
    ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"

    redis_cache = await aioredis.create_pool(('localhost', 6379), minsize=5, maxsize=100)
    bc = BrowscapAsync(cache=RedisPoolAioCache(redis_cache))
    browser = yield from bc.get_browser(ua)

    cache.close_connection()
    return browser


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    browser = loop.run_until_complete(get_browser())
    pprint(browser)

Update base

import redis
import browscap
from browscap.cache.redis import RedisCache
from browscap import IniLoader

redis_cache = redis.Redis(db=5)
bc = browscap.Browscap(cache=RedisCache(redis_cache))
bc.update(type=IniLoader.PHP_INI_FULL)