Skip to content

Commit 186cd73

Browse files
committed
🦾 New function to check URLS with command-line.
1 parent 8dbdca3 commit 186cd73

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/weboptout/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
__version__ = "dev"
44

55
from .types import rsv, Reservation, Status
6-
from .web import check_domain_reservation
6+
from .web import check_domain_reservation, check_url_reservation

src/weboptout/__main__.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
## Copyright © 2023, Alex J. Champandard. Licensed under MIT; see LICENSE! ⚘
22

3+
import re
34
import textwrap
45

56
import click
67

7-
from weboptout import check_domain_reservation, rsv
8+
from weboptout import check_domain_reservation, check_url_reservation, rsv
89
from weboptout.types import Status
910

1011

@@ -13,17 +14,13 @@ def main():
1314
pass
1415

1516

16-
@main.command()
17-
@click.argument('domains', nargs=-1)
18-
def check(domains):
19-
print(f"\n\033[1;97m{'Domain':22} Opt-Out\033[0m\n")
20-
21-
for domain in domains:
22-
res = check_domain_reservation(domain, use_database=True)
17+
def _run_checks(check_fn, sources):
18+
for source in sources:
19+
res = check_fn(source)
2320
if res == rsv.YES:
24-
print(f" {domain:24}✓")
21+
print(f" {source:23} ✓")
2522
else:
26-
print(f" {domain:24}?")
23+
print(f" {source:23} ?")
2724

2825
for record in res.process:
2926
print(
@@ -38,5 +35,29 @@ def check(domains):
3835
print()
3936

4037

38+
@main.command()
39+
@click.argument('domains', nargs=-1)
40+
def check_domain(domains):
41+
print(f"\n\033[1;97m{'Domain':22} Opt-Out\033[0m\n")
42+
return _run_checks(check_domain_reservation, domains)
43+
44+
45+
@main.command()
46+
@click.argument('urls', nargs=-1)
47+
def check_url(urls):
48+
print(f"\n\033[1;97m{'Link':22} Opt-Out\033[0m\n")
49+
return _run_checks(check_url_reservation, urls)
50+
51+
52+
@main.command()
53+
@click.argument('sources', nargs=-1)
54+
def check(sources):
55+
if all(re.match("^https?://", src) for src in sources):
56+
return check_url(sources)
57+
if all(not re.match("^https?://", src) for src in sources):
58+
return check_domain(sources)
59+
raise NotImplementedError
60+
61+
4162
if __name__ == "__main__":
4263
main()

src/weboptout/web.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## Copyright © 2023, Alex J. Champandard. Licensed under MIT; see LICENSE! ⚘
22

3+
from urllib.parse import urlparse
4+
35
from .types import rsv, Reservation, Status
46
from .client import ClientSession
5-
from .utils import allow_sync_calls, retrieve_result_from_cache
7+
from .utils import allow_sync_calls
68
from .http import search_tos_for_domain
79
from .html import check_tos_reservation
810

@@ -11,7 +13,6 @@
1113

1214

1315
@allow_sync_calls
14-
# @retrieve_result_from_cache("cache/rsv.pkl", key="domain")
1516
async def check_domain_reservation(domain: str) -> Reservation:
1617
assert not any(domain.startswith(k) for k in ("https://", "http://"))
1718

@@ -40,3 +41,8 @@ async def check_domain_reservation(domain: str) -> Reservation:
4041

4142
# This happens when none of the domains can be looked up.
4243
return rsv.ERROR(url=None, process=client._steps, outcome=client._output)
44+
45+
46+
def check_url_reservation(url: str) -> Reservation:
47+
domain = urlparse(url).netloc
48+
return check_domain_reservation(domain)

0 commit comments

Comments
 (0)