Skip to content

Fix repeated matches and wrong indices when hostname repeats the TLD - #174

Open
RClaudiuM wants to merge 1 commit into
lipoja:masterfrom
RClaudiuM:fix/155-repeated-tld-indices
Open

RClaudiuM wants to merge 1 commit into
lipoja:masterfrom
RClaudiuM:fix/155-repeated-tld-indices

Conversation

@RClaudiuM

Copy link
Copy Markdown

Fixes #155

When a hostname contains the TLD more than once, the first match can be one in the middle of the hostname, e.g. the first .com in my.example.com.example.com. gen_urls() then looked the TLD up in the extracted URL with _get_tld_pos(), which returns the last TLD of the hostname. Because of that, the cursor never moved past the URL: the same URL came back again for every later TLD match, and the indices were wrong (sometimes negative).

This PR adds _get_matched_tld_pos(). It uses the position of the matched TLD in the text to work out where that TLD sits in the extracted URL. If the URL can't be found around the match, it falls back to _get_tld_pos().

Before:

>>> URLExtract().find_urls("String bbb.aaa.bbb.aaa.aaa test string", get_indices=True)
[('bbb.aaa.bbb.aaa.aaa', (-5, 14)), ('bbb.aaa.bbb.aaa.aaa', (3, 22)), ('bbb.aaa.bbb.aaa.aaa', (7, 26))]
>>> URLExtract().find_urls("my.example.com.example.com")
['my.example.com.example.com', 'my.example.com.example.com']

After:

[('bbb.aaa.bbb.aaa.aaa', (7, 26))]
['my.example.com.example.com']

Tests: I added cases to tests/unit/test_find_urls.py (3 fail on master, pass with the fix).

  • pytest --ignore setup.py: 185 passed
  • pytest urlextract --doctest-modules: passes
  • black --check --skip-string-normalization and flake8: clean
  • mypy: same 5 errors as on master, none in the changed code

gen_urls() located the matched TLD in the extracted URL with
_get_tld_pos(), which returns the last TLD of the hostname. When the
hostname contains the TLD more than once (e.g. my.example.com.example.com)
and the first occurrence was matched, the cursor was not moved past the
URL. The URL was then returned again for every following TLD match,
with wrong (even negative) indices.

Use the position of the matched TLD in the text to find where it sits
inside the extracted URL.

Fixes lipoja#155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong indices and repeated matches when hostname contains the TLD

1 participant