Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace regex url validation with validator library #152

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions paper2remarkable/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
14 changes: 8 additions & 6 deletions paper2remarkable/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import subprocess
import time

import regex
import requests
import unidecode
import validators

from pikepdf import Pdf
from pikepdf import PdfError
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"requests>=2.21",
"titlecase>=0.12",
"unidecode>=1.1",
"validators>=0.34",
"weasyprint>=51",
]

Expand Down
Loading