trustlist-badge 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trustlist_badge-0.1.0/.gitignore +12 -0
- trustlist_badge-0.1.0/LICENSE +21 -0
- trustlist_badge-0.1.0/PKG-INFO +46 -0
- trustlist_badge-0.1.0/README.md +29 -0
- trustlist_badge-0.1.0/pyproject.toml +27 -0
- trustlist_badge-0.1.0/src/trustlist_badge/__init__.py +82 -0
- trustlist_badge-0.1.0/tests/badge-vectors.json +79 -0
- trustlist_badge-0.1.0/tests/test_vectors.py +31 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TrustList
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: trustlist-badge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Show your TrustList rank badge on your website: the badge image, linked to the ranking it cites.
|
|
5
|
+
Project-URL: Homepage, https://trustlist.uk/developers
|
|
6
|
+
Project-URL: Source, https://github.com/eharain/trustlist-public/tree/main/packages/python
|
|
7
|
+
Project-URL: Specification, https://github.com/eharain/trustlist-public/blob/main/spec/badge.md
|
|
8
|
+
Author: TrustList
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: badge,ranking,reviews,trustlist
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# trustlist-badge
|
|
19
|
+
|
|
20
|
+
Show your TrustList rank badge on your website from Python. If your business is ranked on
|
|
21
|
+
[TrustList](https://trustlist.uk), this package builds the badge snippet: the badge image, linked to the
|
|
22
|
+
ranking it cites, with a small Verify link. Print it in your page's HTML so every visitor and search
|
|
23
|
+
engine sees the link.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
pip install trustlist-badge
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from trustlist_badge import badge_html
|
|
31
|
+
|
|
32
|
+
html = badge_html(
|
|
33
|
+
type="company", # or "product"
|
|
34
|
+
slug="your-company", # from your TrustList address
|
|
35
|
+
page="/best/web-design-agency/london", # the ranking's path
|
|
36
|
+
label="TrustList: Top Web Design Agencies in London",
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
In Django or Jinja templates, mark it safe (`mark_safe(html)` or `{{ html|safe }}`): the function
|
|
41
|
+
escapes every value itself. `badge_url`, `ranking_url` and `verify_url` return the separate links;
|
|
42
|
+
invalid input raises `BadgeError`.
|
|
43
|
+
|
|
44
|
+
The badge is built by TrustList from the published edition; it updates itself with each edition and
|
|
45
|
+
reads "Not ranked" if the rank it cites is gone. Same behaviour in every language: see the
|
|
46
|
+
[specification](https://github.com/eharain/trustlist-public/blob/main/spec/badge.md). MIT licence.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# trustlist-badge
|
|
2
|
+
|
|
3
|
+
Show your TrustList rank badge on your website from Python. If your business is ranked on
|
|
4
|
+
[TrustList](https://trustlist.uk), this package builds the badge snippet: the badge image, linked to the
|
|
5
|
+
ranking it cites, with a small Verify link. Print it in your page's HTML so every visitor and search
|
|
6
|
+
engine sees the link.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pip install trustlist-badge
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from trustlist_badge import badge_html
|
|
14
|
+
|
|
15
|
+
html = badge_html(
|
|
16
|
+
type="company", # or "product"
|
|
17
|
+
slug="your-company", # from your TrustList address
|
|
18
|
+
page="/best/web-design-agency/london", # the ranking's path
|
|
19
|
+
label="TrustList: Top Web Design Agencies in London",
|
|
20
|
+
)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
In Django or Jinja templates, mark it safe (`mark_safe(html)` or `{{ html|safe }}`): the function
|
|
24
|
+
escapes every value itself. `badge_url`, `ranking_url` and `verify_url` return the separate links;
|
|
25
|
+
invalid input raises `BadgeError`.
|
|
26
|
+
|
|
27
|
+
The badge is built by TrustList from the published edition; it updates itself with each edition and
|
|
28
|
+
reads "Not ranked" if the rank it cites is gone. Same behaviour in every language: see the
|
|
29
|
+
[specification](https://github.com/eharain/trustlist-public/blob/main/spec/badge.md). MIT licence.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "trustlist-badge"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Show your TrustList rank badge on your website: the badge image, linked to the ranking it cites."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
authors = [{ name = "TrustList" }]
|
|
14
|
+
keywords = ["trustlist", "badge", "ranking", "reviews"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://trustlist.uk/developers"
|
|
23
|
+
Source = "https://github.com/eharain/trustlist-public/tree/main/packages/python"
|
|
24
|
+
Specification = "https://github.com/eharain/trustlist-public/blob/main/spec/badge.md"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["src/trustlist_badge"]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Build TrustList rank badge links: the badge image URL, the ranking it links to, the verify URL,
|
|
2
|
+
and a ready-to-print HTML snippet. See spec/badge.md in https://github.com/eharain/trustlist-public.
|
|
3
|
+
|
|
4
|
+
from trustlist_badge import badge_html
|
|
5
|
+
html = badge_html(type="company", slug="your-company", page="/best/web-design-agency/london")
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from urllib.parse import quote
|
|
10
|
+
|
|
11
|
+
__all__ = ["ranking_url", "badge_url", "verify_url", "badge_html", "BadgeError"]
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
|
|
14
|
+
_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
|
15
|
+
_SIZES = {"regular": (320, 64), "small": (240, 48)}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BadgeError(ValueError):
|
|
19
|
+
"""Raised for an unknown type, a malformed slug, a page outside /best/ or an unknown size."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _check(type, slug, page, size):
|
|
23
|
+
if type not in ("company", "product"):
|
|
24
|
+
raise BadgeError('type must be "company" or "product"')
|
|
25
|
+
if not slug or not _SLUG.match(slug):
|
|
26
|
+
raise BadgeError("slug must be lowercase letters, digits and hyphens")
|
|
27
|
+
if not page or not page.startswith("/best/"):
|
|
28
|
+
raise BadgeError("page must start with /best/")
|
|
29
|
+
if size not in _SIZES:
|
|
30
|
+
raise BadgeError('size must be "regular" or "small"')
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _base(base):
|
|
34
|
+
return (base or "https://trustlist.uk").rstrip("/")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _enc(value):
|
|
38
|
+
# The same characters as JavaScript's encodeURIComponent leaves alone.
|
|
39
|
+
return quote(value, safe="-_.!~*'()")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _query(type, slug, page, edition):
|
|
43
|
+
parts = [("type", type), ("slug", slug), ("page", page)]
|
|
44
|
+
if edition:
|
|
45
|
+
parts.append(("edition", edition))
|
|
46
|
+
return "&".join(f"{k}={_enc(v)}" for k, v in parts)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def ranking_url(type, slug, page, edition=None, base=None, size="regular"):
|
|
50
|
+
"""The ranking page the badge cites."""
|
|
51
|
+
_check(type, slug, page, size)
|
|
52
|
+
return _base(base) + page
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def badge_url(type, slug, page, edition=None, base=None, size="regular"):
|
|
56
|
+
"""The badge image, built by TrustList from the published edition."""
|
|
57
|
+
_check(type, slug, page, size)
|
|
58
|
+
return f"{_base(base)}/badges/rank?{_query(type, slug, page, edition)}"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def verify_url(type, slug, page, edition=None, base=None, size="regular"):
|
|
62
|
+
"""TrustList's record of the rank the badge cites."""
|
|
63
|
+
_check(type, slug, page, size)
|
|
64
|
+
return f"{_base(base)}/badges/verify?{_query(type, slug, page, edition)}"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _esc(s):
|
|
68
|
+
return (s.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
69
|
+
.replace('"', """).replace("'", "'"))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def badge_html(type, slug, page, edition=None, base=None, size="regular", label=None):
|
|
73
|
+
"""The snippet to print: the badge linking to its ranking, and a small Verify link."""
|
|
74
|
+
_check(type, slug, page, size)
|
|
75
|
+
w, h = _SIZES[size]
|
|
76
|
+
args = dict(type=type, slug=slug, page=page, edition=edition, base=base, size=size)
|
|
77
|
+
return (
|
|
78
|
+
f'<a href="{_esc(ranking_url(**args))}" title="See this ranking on TrustList">'
|
|
79
|
+
f'<img src="{_esc(badge_url(**args))}" width="{w}" height="{h}" alt="{_esc(label or "TrustList rank badge")}" '
|
|
80
|
+
f'loading="lazy" style="border:0;max-width:100%;height:auto"></a> '
|
|
81
|
+
f'<a href="{_esc(verify_url(**args))}" style="font:11px system-ui,sans-serif;color:#64748b">Verify</a>'
|
|
82
|
+
)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"valid": [
|
|
3
|
+
{
|
|
4
|
+
"input": {
|
|
5
|
+
"type": "company",
|
|
6
|
+
"slug": "acme-ltd",
|
|
7
|
+
"page": "/best/web-design-agency/london"
|
|
8
|
+
},
|
|
9
|
+
"rankingUrl": "https://trustlist.uk/best/web-design-agency/london",
|
|
10
|
+
"badgeUrl": "https://trustlist.uk/badges/rank?type=company&slug=acme-ltd&page=%2Fbest%2Fweb-design-agency%2Flondon",
|
|
11
|
+
"verifyUrl": "https://trustlist.uk/badges/verify?type=company&slug=acme-ltd&page=%2Fbest%2Fweb-design-agency%2Flondon",
|
|
12
|
+
"badgeHtml": "<a href=\"https://trustlist.uk/best/web-design-agency/london\" title=\"See this ranking on TrustList\"><img src=\"https://trustlist.uk/badges/rank?type=company&slug=acme-ltd&page=%2Fbest%2Fweb-design-agency%2Flondon\" width=\"320\" height=\"64\" alt=\"TrustList rank badge\" loading=\"lazy\" style=\"border:0;max-width:100%;height:auto\"></a> <a href=\"https://trustlist.uk/badges/verify?type=company&slug=acme-ltd&page=%2Fbest%2Fweb-design-agency%2Flondon\" style=\"font:11px system-ui,sans-serif;color:#64748b\">Verify</a>"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"input": {
|
|
16
|
+
"type": "product",
|
|
17
|
+
"slug": "postgresql",
|
|
18
|
+
"page": "/best/rdbms-software/sql",
|
|
19
|
+
"edition": "2026-Q4"
|
|
20
|
+
},
|
|
21
|
+
"rankingUrl": "https://trustlist.uk/best/rdbms-software/sql",
|
|
22
|
+
"badgeUrl": "https://trustlist.uk/badges/rank?type=product&slug=postgresql&page=%2Fbest%2Frdbms-software%2Fsql&edition=2026-Q4",
|
|
23
|
+
"verifyUrl": "https://trustlist.uk/badges/verify?type=product&slug=postgresql&page=%2Fbest%2Frdbms-software%2Fsql&edition=2026-Q4",
|
|
24
|
+
"badgeHtml": "<a href=\"https://trustlist.uk/best/rdbms-software/sql\" title=\"See this ranking on TrustList\"><img src=\"https://trustlist.uk/badges/rank?type=product&slug=postgresql&page=%2Fbest%2Frdbms-software%2Fsql&edition=2026-Q4\" width=\"320\" height=\"64\" alt=\"TrustList rank badge\" loading=\"lazy\" style=\"border:0;max-width:100%;height:auto\"></a> <a href=\"https://trustlist.uk/badges/verify?type=product&slug=postgresql&page=%2Fbest%2Frdbms-software%2Fsql&edition=2026-Q4\" style=\"font:11px system-ui,sans-serif;color:#64748b\">Verify</a>"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"input": {
|
|
28
|
+
"type": "company",
|
|
29
|
+
"slug": "acme-ltd",
|
|
30
|
+
"page": "/best/accounting-firm/united-kingdom",
|
|
31
|
+
"size": "small",
|
|
32
|
+
"base": "https://example.test/",
|
|
33
|
+
"label": "Top Accounting Firms in the UK & \"more\" <test>"
|
|
34
|
+
},
|
|
35
|
+
"rankingUrl": "https://example.test/best/accounting-firm/united-kingdom",
|
|
36
|
+
"badgeUrl": "https://example.test/badges/rank?type=company&slug=acme-ltd&page=%2Fbest%2Faccounting-firm%2Funited-kingdom",
|
|
37
|
+
"verifyUrl": "https://example.test/badges/verify?type=company&slug=acme-ltd&page=%2Fbest%2Faccounting-firm%2Funited-kingdom",
|
|
38
|
+
"badgeHtml": "<a href=\"https://example.test/best/accounting-firm/united-kingdom\" title=\"See this ranking on TrustList\"><img src=\"https://example.test/badges/rank?type=company&slug=acme-ltd&page=%2Fbest%2Faccounting-firm%2Funited-kingdom\" width=\"240\" height=\"48\" alt=\"Top Accounting Firms in the UK & "more" <test>\" loading=\"lazy\" style=\"border:0;max-width:100%;height:auto\"></a> <a href=\"https://example.test/badges/verify?type=company&slug=acme-ltd&page=%2Fbest%2Faccounting-firm%2Funited-kingdom\" style=\"font:11px system-ui,sans-serif;color:#64748b\">Verify</a>"
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
"invalid": [
|
|
42
|
+
{
|
|
43
|
+
"input": {
|
|
44
|
+
"type": "person",
|
|
45
|
+
"slug": "acme-ltd",
|
|
46
|
+
"page": "/best/x"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"input": {
|
|
51
|
+
"type": "company",
|
|
52
|
+
"slug": "Acme Ltd",
|
|
53
|
+
"page": "/best/x"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"input": {
|
|
58
|
+
"type": "company",
|
|
59
|
+
"slug": "",
|
|
60
|
+
"page": "/best/x"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"input": {
|
|
65
|
+
"type": "company",
|
|
66
|
+
"slug": "acme-ltd",
|
|
67
|
+
"page": "/top/x"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"input": {
|
|
72
|
+
"type": "company",
|
|
73
|
+
"slug": "acme-ltd",
|
|
74
|
+
"page": "/best/x",
|
|
75
|
+
"size": "huge"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import pathlib
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
from trustlist_badge import BadgeError, badge_html, badge_url, ranking_url, verify_url
|
|
6
|
+
|
|
7
|
+
VECTORS = json.loads((pathlib.Path(__file__).parent / "badge-vectors.json").read_text(encoding="utf-8"))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def args(inp):
|
|
11
|
+
return dict(type=inp.get("type"), slug=inp.get("slug"), page=inp.get("page"), edition=inp.get("edition"),
|
|
12
|
+
base=inp.get("base"), size=inp.get("size", "regular"))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class VectorTests(unittest.TestCase):
|
|
16
|
+
def test_valid(self):
|
|
17
|
+
for case in VECTORS["valid"]:
|
|
18
|
+
a = args(case["input"])
|
|
19
|
+
self.assertEqual(ranking_url(**a), case["rankingUrl"])
|
|
20
|
+
self.assertEqual(badge_url(**a), case["badgeUrl"])
|
|
21
|
+
self.assertEqual(verify_url(**a), case["verifyUrl"])
|
|
22
|
+
self.assertEqual(badge_html(**a, label=case["input"].get("label")), case["badgeHtml"])
|
|
23
|
+
|
|
24
|
+
def test_invalid(self):
|
|
25
|
+
for case in VECTORS["invalid"]:
|
|
26
|
+
with self.assertRaises(BadgeError):
|
|
27
|
+
badge_html(**args(case["input"]))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
unittest.main()
|