Skip to content

Commit 81d134b

Browse files
SAKMZclaude
andcommitted
Modernize as user-agents-ng 3.0.0
- Migrate packaging from setup.py to pyproject.toml (name: user-agents-ng) - Drop Python 2 support (remove compat.py and __unicode__); require Python 3.9+ - Add type hints throughout and a py.typed marker - Replace dead Travis config with GitHub Actions CI (Linux + Windows, Python 3.9-3.14) - Rewrite README as a maintained drop-in successor to user-agents - Bump version to 3.0.0 All 55 tests pass on Python 3.14 with ua-parser 1.0.2. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5f2d80a commit 81d134b

13 files changed

Lines changed: 198 additions & 219 deletions

File tree

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest, windows-latest]
15+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Install package
26+
run: |
27+
python -m pip install --upgrade pip
28+
python -m pip install -e .
29+
30+
- name: Run tests
31+
run: python -m unittest -v user_agents.tests

.travis.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

MANIFEST

Lines changed: 0 additions & 8 deletions
This file was deleted.

MANIFEST.in

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 52 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
1-
Python User Agents
2-
==================
1+
# user-agents-ng
32

4-
`user_agents` is a Python library that provides an easy way to identify/detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether:
3+
[![CI](https://github.com/SAKMZ/python-user-agents/actions/workflows/ci.yml/badge.svg)](https://github.com/SAKMZ/python-user-agents/actions/workflows/ci.yml)
4+
[![PyPI](https://img.shields.io/pypi/v/user-agents-ng.svg)](https://pypi.org/project/user-agents-ng/)
5+
6+
> **A maintained, drop-in successor to [`user-agents`](https://pypi.org/project/user-agents/).**
7+
> The original [`selwin/python-user-agents`](https://github.com/selwin/python-user-agents)
8+
> has had no release since 2020. `user-agents-ng` picks up where it left off:
9+
> modern packaging, type hints, and tested support for Python 3.9–3.14.
10+
> **The import stays the same (`from user_agents import parse`) — just change what you `pip install`.**
11+
12+
`user_agents` is a Python library that provides an easy way to identify/detect
13+
devices like mobile phones, tablets and their capabilities by parsing
14+
(browser/HTTP) user agent strings. The goal is to reliably detect whether:
515

616
* User agent is a mobile, tablet or PC based device
717
* User agent has touch capabilities (has touch screen)
818

9-
`user_agents` relies on the excellent [ua-parser](https://github.com/ua-parser/uap-python) to do the actual parsing of the raw user agent string.
19+
`user_agents` relies on the excellent [ua-parser](https://github.com/ua-parser/uap-python)
20+
to do the actual parsing of the raw user agent string.
21+
22+
## Installation
1023

11-
Installation
12-
------------
24+
`user-agents-ng` is hosted on [PyPI](https://pypi.org/project/user-agents-ng/) and can be installed with:
1325

14-
![Build status](https://secure.travis-ci.org/selwin/python-user-agents.png)
26+
pip install user-agents-ng
1527

16-
`user-agents` is hosted on [PyPI](http://pypi.python.org/pypi/user-agents/) and can be installed as such:
28+
### Migrating from `user-agents`
1729

18-
pip install pyyaml ua-parser user-agents
30+
`user-agents-ng` is a drop-in replacement. Swap the dependency and your code
31+
keeps working unchanged:
1932

20-
Alternatively, you can also get the latest source code from [Github](https://github.com/selwin/python-user-agents) and install it manually.
33+
pip uninstall user-agents
34+
pip install user-agents-ng
2135

22-
Usage
23-
-----
36+
```python
37+
from user_agents import parse # <- unchanged
38+
```
2439

25-
Various basic information that can help you identify visitors can be accessed `browser`, `device` and `os` attributes. For example:
40+
## Usage
41+
42+
Various basic information that can help you identify visitors can be accessed via the
43+
`browser`, `device` and `os` attributes. For example:
2644

2745
```python
2846
from user_agents import parse
@@ -32,19 +50,19 @@ ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/5
3250
user_agent = parse(ua_string)
3351

3452
# Accessing user agent's browser attributes
35-
user_agent.browser # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
53+
user_agent.browser # returns Browser(family='Mobile Safari', version=(5, 1), version_string='5.1')
3654
user_agent.browser.family # returns 'Mobile Safari'
3755
user_agent.browser.version # returns (5, 1)
3856
user_agent.browser.version_string # returns '5.1'
3957

4058
# Accessing user agent's operating system properties
41-
user_agent.os # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
59+
user_agent.os # returns OperatingSystem(family='iOS', version=(5, 1), version_string='5.1')
4260
user_agent.os.family # returns 'iOS'
4361
user_agent.os.version # returns (5, 1)
4462
user_agent.os.version_string # returns '5.1'
4563

4664
# Accessing user agent's device properties
47-
user_agent.device # returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')
65+
user_agent.device # returns Device(family='iPhone', brand='Apple', model='iPhone')
4866
user_agent.device.family # returns 'iPhone'
4967
user_agent.device.brand # returns 'Apple'
5068
user_agent.device.model # returns 'iPhone'
@@ -53,7 +71,10 @@ user_agent.device.model # returns 'iPhone'
5371
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"
5472
```
5573

56-
`user_agents` also expose a few other more "sophisticated" attributes that are derived from one or more basic attributes defined above. As for now, these attributes should correctly identify popular platforms/devices, pull requests to support smaller ones are always welcome.
74+
`user_agents` also exposes a few other more "sophisticated" attributes that are
75+
derived from one or more basic attributes defined above. These attributes should
76+
correctly identify popular platforms/devices; pull requests to support smaller
77+
ones are always welcome.
5778

5879
Currently these attributes are supported:
5980

@@ -88,26 +109,6 @@ user_agent.is_pc # returns False
88109
user_agent.is_bot # returns False
89110
str(user_agent) # returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"
90111

91-
# iPad's user agent string
92-
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'
93-
user_agent = parse(ua_string)
94-
user_agent.is_mobile # returns False
95-
user_agent.is_tablet # returns True
96-
user_agent.is_touch_capable # returns True
97-
user_agent.is_pc # returns False
98-
user_agent.is_bot # returns False
99-
str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"
100-
101-
# Kindle Fire's user agent string
102-
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'
103-
user_agent = parse(ua_string)
104-
user_agent.is_mobile # returns False
105-
user_agent.is_tablet # returns True
106-
user_agent.is_touch_capable # returns True
107-
user_agent.is_pc # returns False
108-
user_agent.is_bot # returns False
109-
str(user_agent) # returns "Kindle / Android / Amazon Silk 1.1.0-80"
110-
111112
# Touch capable Windows 8 device
112113
ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'
113114
user_agent = parse(ua_string)
@@ -119,61 +120,24 @@ user_agent.is_bot # returns False
119120
str(user_agent) # returns "PC / Windows 8 / IE 10"
120121
```
121122

122-
Running Tests
123-
-------------
124-
125-
python -m unittest discover
126-
127-
Changelog
128-
---------
129-
### Version 2.2.0 (2020-08-23)
130-
* `ua-parser` >= 0.10.0 is required. Thanks @jnozsc!
131-
* Added `get_device()`, `get_os()` and `get_browser()` instance methods
132-
to `UserAgent`. Thanks @rodrigondec!
133-
134-
### Version 2.1 (2020-02-08)
135-
136-
* `python-user-agents` now require `ua-parser>=0.9.0`. Thanks @jnozsc!
137-
* Properly detect Chrome Mobile browser families. Thanks @jnozsc!
138-
139-
### Version 2.0 (2019-04-07)
140-
141-
* `python-user-agents` now require `ua-parser>=0.8.0`. Thanks @IMDagger!
142-
143-
### Version 1.1
144-
145-
* Fixes packaging issue
146-
147-
### Version 1.0
148-
149-
* Adds compatibility with `ua-parser` 0.4.0
150-
* Access to more device information in `user_agent.device.brand` and `user_agent.device.model`
151-
152-
### Version 0.3.2
153-
154-
* Better mobile detection
155-
* Better PC detection
156-
157-
### Version 0.3.1
158-
159-
* user\_agent.is\_mobile returns True when mobile spider is detected
160-
161-
### Version 0.3.0
162-
163-
* Added **str**/**unicode** methods for convenience of pretty string
123+
## Running Tests
164124

165-
### Version 0.2.0
125+
python -m unittest user_agents.tests
166126

167-
* Fixed errors when running against newer versions if ua-parser
168-
* Support for Python 3
127+
## Changelog
169128

170-
### Version 0.1.1
129+
### Version 3.0.0
171130

172-
* Added `is_bot` property
173-
* Symbian OS devices are now detected as a mobile device
131+
* Released as `user-agents-ng`, a maintained successor to `user-agents`.
132+
* Tested support for Python 3.9–3.14; dropped Python 2.
133+
* Migrated packaging to `pyproject.toml`; added `py.typed` and type hints.
134+
* Replaced Travis CI with GitHub Actions (Linux + Windows).
174135

175-
### Version 0.1
136+
For the pre-3.0 history, see the original
137+
[project changelog](https://github.com/selwin/python-user-agents).
176138

177-
* Initial release
139+
## Credits
178140

179-
Developed by the cool guys at [Stamps](http://stamps.co.id).
141+
Originally created by Selwin Ong and the team at
142+
[Stamps](http://stamps.co.id). Maintained as `user-agents-ng` by
143+
[Saif Ali](https://github.com/SAKMZ). Licensed under the MIT License.

pyproject.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "user-agents-ng"
7+
version = "3.0.0"
8+
description = "A maintained successor to user-agents: identify devices (phones, tablets) and their capabilities by parsing browser user agent strings."
9+
readme = "README.md"
10+
requires-python = ">=3.9"
11+
license = "MIT"
12+
license-files = ["LICENSE.txt"]
13+
authors = [
14+
{ name = "Selwin Ong" },
15+
]
16+
maintainers = [
17+
{ name = "Saif Ali", email = "saifalimz@gmail.com" },
18+
]
19+
keywords = ["user agent", "user-agents", "browser", "device", "detection", "parser", "mobile", "tablet", "bot"]
20+
dependencies = [
21+
"ua-parser>=0.10.0",
22+
]
23+
classifiers = [
24+
"Development Status :: 5 - Production/Stable",
25+
"Environment :: Web Environment",
26+
"Intended Audience :: Developers",
27+
"Operating System :: OS Independent",
28+
"Programming Language :: Python",
29+
"Programming Language :: Python :: 3",
30+
"Programming Language :: Python :: 3.9",
31+
"Programming Language :: Python :: 3.10",
32+
"Programming Language :: Python :: 3.11",
33+
"Programming Language :: Python :: 3.12",
34+
"Programming Language :: Python :: 3.13",
35+
"Programming Language :: Python :: 3.14",
36+
"Programming Language :: Python :: 3 :: Only",
37+
"Topic :: Internet :: WWW/HTTP",
38+
"Topic :: Software Development :: Libraries :: Python Modules",
39+
]
40+
41+
[project.urls]
42+
Homepage = "https://github.com/SAKMZ/python-user-agents"
43+
Repository = "https://github.com/SAKMZ/python-user-agents"
44+
Issues = "https://github.com/SAKMZ/python-user-agents/issues"
45+
46+
[tool.setuptools]
47+
packages = ["user_agents"]
48+
49+
[tool.setuptools.package-data]
50+
user_agents = ["devices.json", "py.typed"]

requirements.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

setup.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

user_agents/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
VERSION = (2, 2, 0)
1+
VERSION = (3, 0, 0)
22

3-
from .parsers import parse
3+
from .parsers import UserAgent, parse
4+
5+
__all__ = ['UserAgent', 'parse']

user_agents/compat.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)