Skip to content

Commit

Permalink
fix: replace regex url validation with validator library
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabazs committed Oct 15, 2024
1 parent 5cdf0b0 commit f38b5bd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
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

0 comments on commit f38b5bd

Please sign in to comment.