Created
November 26, 2012 09:10
-
-
Save KazuyaHayashi/4147324 to your computer and use it in GitHub Desktop.
parse user-agent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class BrowserDetector(object): | |
def __init__(self): | |
self.patterns = [ | |
'.*(?P<browser>(MSIE|msie)) (?P<version>[0-9.]+).*', | |
'.* (VERSION|version)/(?P<version>[0-9.]+) (?P<browser>(SAFARI|safari))/.*', | |
'.*(?P<browser>(CHROME|chrome))/(?P<version>[0-9.]+).*', | |
'.*(?P<browser>(FIREFOX|firefox))/(?P<version>[0-9.]+).*', | |
] | |
def detect(self, user_agent): | |
res = None | |
browser_info = {'browser':'', 'version':'', 'plugin':[]} | |
for pattern in self.patterns: | |
res = re.match(pattern, user_agent.lower()) | |
if res: | |
browser_info.update(res.groupdict()) | |
if 'chromeframe' in user_agent: | |
browser_info['plugin'].append('chromeframe') | |
break | |
return browser_info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://tempdownloads.browserscap.com/ のファイルを使った方法だと、safari の細かいバージョン(5.1.2 とか)を取得できなかったので作った。