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

Added support for urlparse functions #63

Merged
merged 2 commits into from
Oct 5, 2020
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
30 changes: 30 additions & 0 deletions shublang/shublang.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,36 @@ def identity(iterable, element):
""" Return the same element is passed as parameter."""
return (element)

@Pipe
def urlparse_netloc(iterable):
return (parse.urlparse(url).netloc for url in iterable)

@Pipe
def urlparse_params(iterable):
return (parse.urlparse(url).params for url in iterable)

@Pipe
def urlparse_path(iterable):
return (parse.urlparse(url).path for url in iterable)

@Pipe
def urlparse_query(iterable):
return (parse.urlparse(url).query for url in iterable)

@Pipe
def urlparse_scheme(iterable):
return (parse.urlparse(url).scheme for url in iterable)

@Pipe
def urlparse_fragment(iterable):
return (parse.urlparse(url).fragment for url in iterable)

@Pipe
def urlparse(iterable):
parsed_iterable = (parse.urlparse(url) for url in iterable)
parsed_iterable = ({"scheme": it.scheme, "netloc": it.netloc, "path": it.path,
"params": it.params, "query": it.query, "fragment": it.fragment} for it in parsed_iterable)
return parsed_iterable

filter = where
map = select
Expand Down
30 changes: 30 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,36 @@ def test_currency_2():
assert evaluate('extract_currency', data=['$1,199.00']) == ['$']


def test_urlparse_netloc():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert evaluate('urlparse_netloc', data=[url]) == ['scrapinghub.bamboohr.com']


def test_urlparse_path():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert evaluate('urlparse_path', data=[url]) == ['/employees/performance/index.php']


def test_urlparse_query():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert evaluate('urlparse_query', data=[url]) == ['page=2107&subpage=1']


def test_urlparse_scheme():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert evaluate('urlparse_scheme', data=[url]) == ['https']


def test_urlparse_fragment():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert evaluate('urlparse_fragment', data=[url]) == ['123']


def test_urlparse():
url = "https://scrapinghub.bamboohr.com/employees/performance/index.php?page=2107&subpage=1#123"
assert list(evaluate('urlparse', data=[url])[0].keys()) == ['scheme', 'netloc', 'path', 'params', 'query', 'fragment']


@pytest.mark.parametrize(
"test_input,expected",
[
Expand Down