From ebca60f090a05e9327673328cc56ad4b62031d09 Mon Sep 17 00:00:00 2001 From: Barabazs <31799121+Barabazs@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:31:31 +0200 Subject: [PATCH] fix: replace regex url validation with validator library --- paper2remarkable/ui.py | 4 ++-- paper2remarkable/utils.py | 14 ++++++++------ setup.py | 1 + 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/paper2remarkable/ui.py b/paper2remarkable/ui.py index 065ac9f..eec2605 100644 --- a/paper2remarkable/ui.py +++ b/paper2remarkable/ui.py @@ -158,8 +158,8 @@ def choose_provider(cli_input): This function first tries to check if the input is a local file, by checking if the path exists. Next, it checks if the input is a "valid" url - using a regex test. If it is, the registered provider classes are checked - to see which provider can handle this url. + using the validators library. If it is, the registered provider classes are + checked to see which provider can handle this url. Returns ------- diff --git a/paper2remarkable/utils.py b/paper2remarkable/utils.py index 5f4905a..b7c040c 100644 --- a/paper2remarkable/utils.py +++ b/paper2remarkable/utils.py @@ -13,9 +13,9 @@ import subprocess import time -import regex import requests import unidecode +import validators from pikepdf import Pdf from pikepdf import PdfError @@ -189,11 +189,13 @@ def upload_to_remarkable(filepath, remarkable_dir="/", rmapi_path="rmapi"): def is_url(string): - # pattern adapted from CleverCSV - pattern = r"((https?|ftp):\/\/(?!\-))?(((([\p{L}\p{N}]*[\-\_]?[\p{L}\p{N}]+)+\.)+([a-z]{2,}|local)(\.[a-z]{2,3})?)|localhost|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\:\d{1,5})?))(\/[\p{L}\p{N}_\/()~?=&%\-\#\.:+]*)?(\.[a-z]+)?" - string = string.strip(" ") - match = regex.fullmatch(pattern, string) - return match is not None + """Check if the string is a valid URL + + Returns + ------- + bool: True if the string is a valid URL, False otherwise. + """ + return validators.url(string) def check_pdftool(pdftk_path, qpdf_path): diff --git a/setup.py b/setup.py index 81eb373..23ce9cd 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,7 @@ "requests>=2.21", "titlecase>=0.12", "unidecode>=1.1", + "validators>=0.34", "weasyprint>=51", ]