Library to strip http urls of tracking elements, and use shorthand variants of urls
Project description
Url Strip 0.2.1
A library for stripping urls of tracking and bloat
Non Pythonic
Usage was designed around typesafety and zero runtime surprises, so instead of raising exceptions, url_strip will return exceptions instead of results, this library works best with a typechecker because of this
Usage
Some basic usage. These examples are all runnable and meet typing standards.
Using is_instance and unwrap
"""
Uses is_instance and unwrap to parse a url
"""
from url_strip import Ok, strip_url
# Ok and Err provide the same 3 methods with fully typed outcomes for a Result[T, E]:
# is_instance will TypeGuard the result to the variant
# get will return an Optional[T] of the variant
# (and is not suitable for cases where T or E are None)
# unwrap with return the requested variant or raise an exception
if Ok.is_instance(
v := strip_url(
"https://youtube.com/watch?v=dQw4w9WgXcQ"
"&trackerinfo=youraddresshere&mldata=whattimeyouwokeupthismorning"
)
):
# using unwrap is runtime safe here as we just typeguarded it is an Ok variant
url = Ok.unwrap(v)
# strip_url returns a Result[HttpUrl, UrlError], and HttpUrl provides a into_str method
# to get what most people expect as a final output
value = url.into_str()
print(value) # -> https://youtu.be/dQw4w9WgXcQ
else:
... # Insert error handling here
Using get
"""
Uses get and a None check to parse an invalid url
"""
from url_strip import strip_url, Ok, Err
if (v := Ok.get(result := strip_url("foo"))) is not None:
v.into_str()
else:
raise Err.unwrap(result)
Writing extra domain rules
"""
Uses register to add foo.com as a checked domain
"""
from url_strip import UrlError, Ok, Err, StripFuncResult, HttpUrl, register
@register(domain="foo.com")
def foo_com_strip( # pyright: ignore[reportUnusedFunction]
url: HttpUrl
) -> StripFuncResult: # ('ok', HttpUrl) | ('err', UrlError)
if url.path == "/failure":
return Err(UrlError("This is an awful failure state"))
return Ok(url) # dont change url at all, its perfect as is
Popular Websites Supported
Some popular websites have pre-register'd rules, while any non registered websites will go to a default query stripper
The pre registered domains are as follows:
| Site | Domains |
|---|---|
| amazon | www.amazon.com, www.amazon.co.uk |
| ebay | ebay.com, www.ebay.com, www.ebay.co.uk, www.ebay.it, www.ebay.de |
www.reddit.com |
|
| tiktok | www.tiktok.com, vm.tiktok.com |
twitter.com |
|
| youtube | youtube.com, www.youtube.com |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file url_strip-0.2.1.tar.gz.
File metadata
- Download URL: url_strip-0.2.1.tar.gz
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf79171e491ced6e419f6e7ab8b36f7c249cb6d0f0f7074460f373af8e878934
|
|
| MD5 |
5b92676bfb055eb85edb3d981ab1330d
|
|
| BLAKE2b-256 |
3945875a174edcf3b05218f75d270fa0e666130e13d85cebaf61dddec23c542c
|
File details
Details for the file url_strip-0.2.1-py3-none-any.whl.
File metadata
- Download URL: url_strip-0.2.1-py3-none-any.whl
- Upload date:
- Size: 22.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d05b9616a71f676da9e3c901420922d28d17c66178dab1679398d06acee9c0f
|
|
| MD5 |
5142d9c83855e265c8e3966a757f68d6
|
|
| BLAKE2b-256 |
59d077c026db4434f2a8d2b6327a31f0f367b9d651347d639d3cde79b6f18eb9
|