-
-
Save jimmydo/1001205 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Some mobile browsers which look like desktop browsers. | |
RE_MOBILE = re.compile(r"(iphone|ipod|blackberry|android.+mobile|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', '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment