-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from nansencenter/issue135_http_provider
Make an HTTP provider
- Loading branch information
Showing
5 changed files
with
169 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Code for searching FTP repositories""" | ||
from urllib.parse import urljoin | ||
|
||
from .base import Provider, TimeFilterMixin | ||
from ..arguments import PathArgument, StringArgument | ||
from ..crawlers import HTMLDirectoryCrawler | ||
|
||
|
||
class HTTPProvider(TimeFilterMixin, Provider): | ||
"""Generic HTTP directory provider""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.search_parameters_parser.add_arguments([ | ||
StringArgument('url', required=True), | ||
StringArgument('include', default='.'), | ||
]) | ||
|
||
def _make_crawler(self, parameters): | ||
return HTMLDirectoryCrawler( | ||
parameters['url'], | ||
time_range=(parameters['start_time'], parameters['end_time']), | ||
username=self.username, | ||
password=self.password, | ||
include=parameters['include']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# pylint: disable=protected-access | ||
"""Tests for the generic FTP provider""" | ||
import unittest | ||
import unittest.mock as mock | ||
from datetime import datetime, timezone | ||
|
||
import geospaas_harvesting.crawlers as crawlers | ||
from geospaas_harvesting.providers.http import HTTPProvider | ||
|
||
|
||
class HTTPProviderTestCase(unittest.TestCase): | ||
"""Tests for HTTPProvider""" | ||
|
||
def test_make_crawler(self): | ||
"""Test creating a crawler from parameters""" | ||
provider = HTTPProvider(name='test', username='user', password='pass') | ||
parameters = { | ||
'start_time': datetime(2023, 1, 1, tzinfo=timezone.utc), | ||
'end_time': datetime(2023, 1, 2, tzinfo=timezone.utc), | ||
'url': 'http://foo/bar', | ||
'include': '.*' | ||
} | ||
with mock.patch('ftplib.FTP'): | ||
self.assertEqual( | ||
provider._make_crawler(parameters), | ||
crawlers.HTMLDirectoryCrawler( | ||
'http://foo/bar', | ||
include='.*', | ||
time_range=(datetime(2023, 1, 1, tzinfo=timezone.utc), | ||
datetime(2023, 1, 2, tzinfo=timezone.utc)), | ||
username='user', | ||
password='pass')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters