Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c99703e

Browse filesBrowse files
committed
Convert README to reStructuredText
PyPI renders reStructuredText for display but displays other text formates (including Markdown) as raw text. Conversion was performed automatically by pandoc: pandoc --from=markdown --to=rst --output=README.rst README.md (http://bfroehle.com/2013/04/26/converting-md-to-rst/)
1 parent 8fd709c commit c99703e
Copy full SHA for c99703e

File tree

Expand file treeCollapse file tree

3 files changed

+203
-177
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+203
-177
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
-175Lines changed: 0 additions & 175 deletions
This file was deleted.

‎README.rst

Copy file name to clipboard
+201Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
Python User Agents
2+
==================
3+
4+
``user_agents`` is a Python library that provides an easy way to
5+
identify/detect devices like mobile phones, tablets and their
6+
capabilities by parsing (browser/HTTP) user agent strings. The goal is
7+
to reliably detect whether:
8+
9+
- User agent is a mobile, tablet or PC based device
10+
- User agent has touch capabilities (has touch screen)
11+
12+
``user_agents`` relies on the excellent
13+
``ua-parser <https://github.com/tobie/ua-parser>``\ \_ to do the actual
14+
parsing of the raw user agent string.
15+
16+
Installation
17+
------------
18+
19+
.. figure:: https://secure.travis-ci.org/selwin/python-user-agents.png
20+
:alt: Build status
21+
22+
Build status
23+
``user-agents`` is hosted on
24+
`PyPI <http://pypi.python.org/pypi/user-agents/>`__ and can be installed
25+
as such:
26+
27+
::
28+
29+
pip install pyyaml ua-parser user-agents
30+
31+
Alternatively, you can also get the latest source code from
32+
``Github``\ \_ and install it manually.
33+
34+
Usage
35+
-----
36+
37+
Various basic information that can help you identify visitors can be
38+
accessed ``browser``, ``device`` and ``os`` attributes. For example:
39+
40+
.. code:: python
41+
42+
43+
from user_agents import parse
44+
45+
# iPhone's user agent string
46+
ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
47+
user_agent = parse(ua_string)
48+
49+
# Accessing user agent's browser attributes
50+
user_agent.browser # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
51+
user_agent.browser.family # returns 'Mobile Safari'
52+
user_agent.browser.version # returns (5, 1)
53+
user_agent.browser.version_string # returns '5.1'
54+
55+
# Accessing user agent's operating system properties
56+
user_agent.os # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
57+
user_agent.os.family # returns 'iOS'
58+
user_agent.os.version # returns (5, 1)
59+
user_agent.os.version_string # returns '5.1'
60+
61+
# Accessing user agent's device properties
62+
user_agent.device # returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')
63+
user_agent.device.family # returns 'iPhone'
64+
user_agent.device.brand # returns 'Apple'
65+
user_agent.device.model # returns 'iPhone'
66+
67+
# Viewing a pretty string version
68+
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"
69+
70+
``user_agents`` also expose a few other more "sophisticated" attributes
71+
that are derived from one or more basic attributes defined above. As for
72+
now, these attributes should correctly identify popular
73+
platforms/devices, pull requests to support smaller ones are always
74+
welcome.
75+
76+
Currently these attributes are supported:
77+
78+
- ``is_mobile``: whether user agent is identified as a mobile phone
79+
(iPhone, Android phones, Blackberry, Windows Phone devices etc)
80+
- ``is_tablet``: whether user agent is identified as a tablet device
81+
(iPad, Kindle Fire, Nexus 7 etc)
82+
- ``is_pc``: whether user agent is identified to be running a
83+
traditional "desktop" OS (Windows, OS X, Linux)
84+
- ``is_touch_capable``: whether user agent has touch capabilities
85+
- ``is_bot``: whether user agent is a search engine crawler/spider
86+
87+
For example:
88+
89+
.. code:: python
90+
91+
92+
from user_agents import parse
93+
94+
# Let's start from an old, non touch Blackberry device
95+
ua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba'
96+
user_agent = parse(ua_string)
97+
user_agent.is_mobile # returns True
98+
user_agent.is_tablet # returns False
99+
user_agent.is_touch_capable # returns False
100+
user_agent.is_pc # returns False
101+
user_agent.is_bot # returns False
102+
str(user_agent) # returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"
103+
104+
# Now a Samsung Galaxy S3
105+
ua_string = 'Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
106+
user_agent = parse(ua_string)
107+
user_agent.is_mobile # returns True
108+
user_agent.is_tablet # returns False
109+
user_agent.is_touch_capable # returns True
110+
user_agent.is_pc # returns False
111+
user_agent.is_bot # returns False
112+
str(user_agent) # returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"
113+
114+
# iPad's user agent string
115+
ua_string = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
116+
user_agent = parse(ua_string)
117+
user_agent.is_mobile # returns False
118+
user_agent.is_tablet # returns True
119+
user_agent.is_touch_capable # returns True
120+
user_agent.is_pc # returns False
121+
user_agent.is_bot # returns False
122+
str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"
123+
124+
# Kindle Fire's user agent string
125+
ua_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true'
126+
user_agent = parse(ua_string)
127+
user_agent.is_mobile # returns False
128+
user_agent.is_tablet # returns True
129+
user_agent.is_touch_capable # returns True
130+
user_agent.is_pc # returns False
131+
user_agent.is_bot # returns False
132+
str(user_agent) # returns "Kindle / Android / Amazon Silk 1.1.0-80"
133+
134+
# Touch capable Windows 8 device
135+
ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'
136+
user_agent = parse(ua_string)
137+
user_agent.is_mobile # returns False
138+
user_agent.is_tablet # returns False
139+
user_agent.is_touch_capable # returns True
140+
user_agent.is_pc # returns True
141+
user_agent.is_bot # returns False
142+
str(user_agent) # returns "PC / Windows 8 / IE 10"
143+
144+
Running Tests
145+
-------------
146+
147+
::
148+
149+
python -m unittest discover
150+
151+
Changelog
152+
---------
153+
154+
Version 1.0.0
155+
~~~~~~~~~~~~~
156+
157+
- Fixes packaging issue
158+
159+
Version 1.0
160+
~~~~~~~~~~~
161+
162+
- Adds compatibility with ``ua-parser`` 0.4.0
163+
- Access to more device information in ``user_agent.device.brand`` and
164+
``user_agent.device.model``
165+
166+
===
167+
168+
Version 0.3.2
169+
~~~~~~~~~~~~~
170+
171+
- Better mobile detection
172+
- Better PC detection
173+
174+
Version 0.3.1
175+
~~~~~~~~~~~~~
176+
177+
- user\_agent.is\_mobile returns True when mobile spider is detected
178+
179+
Version 0.3.0
180+
~~~~~~~~~~~~~
181+
182+
- Added **str**/**unicode** methods for convenience of pretty string
183+
184+
Version 0.2.0
185+
~~~~~~~~~~~~~
186+
187+
- Fixed errors when running against newer versions if ua-parser
188+
- Support for Python 3
189+
190+
Version 0.1.1
191+
~~~~~~~~~~~~~
192+
193+
- Added ``is_bot`` property
194+
- Symbian OS devices are now detected as a mobile device
195+
196+
Version 0.1
197+
~~~~~~~~~~~
198+
199+
- Initial release
200+
201+
Developed by the cool guys at `Stamps <http://stamps.co.id>`__.

‎setup.py

Copy file name to clipboardExpand all lines: setup.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
url='https://github.com/selwin/python-user-agents',
1111
license='MIT',
1212
description='A library to identify devices (phones, tablets) and their capabilities by parsing (browser/HTTP) user agent strings',
13-
long_description=open('README.md').read(),
13+
long_description=open('README.rst').read(),
1414
zip_safe=False,
1515
include_package_data=True,
16-
package_data={'': ['README.md']},
16+
package_data={'': ['README.rst']},
1717
install_requires=['ua-parser>=0.4.1'],
1818
classifiers=[
1919
'Development Status :: 5 - Production/Stable',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.