-
Notifications
You must be signed in to change notification settings - Fork 45
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 #20 from asfadmin/feature-core-search
Core search features
- Loading branch information
Showing
32 changed files
with
329 additions
and
187 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Empty file.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
"""Various constants to be used in search and related functions, provided as a convenience to help ensure sensible values.""" | ||
|
||
from .DATASET import DATASET | ||
from .PLATFORM import PLATFORM | ||
from .POLARIZATION import POLARIZATION | ||
from .BEAMMODE import BEAMMODE | ||
from .FLIGHT_DIRECTION import FLIGHT_DIRECTION | ||
from .PRODUCT_TYPE import PRODUCT_TYPE | ||
from .INTERNAL import INTERNAL | ||
from .BEAMMODE import * | ||
from .FLIGHT_DIRECTION import * | ||
from .INSTRUMENT import * | ||
from .PLATFORM import * | ||
from .POLARIZATION import * | ||
from .PRODUCT_TYPE import * | ||
from .INTERNAL import * | ||
|
||
#TODO: add INSTRUMENT constants |
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 @@ | ||
from .download import download_url |
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,45 @@ | ||
import os.path | ||
import urllib.parse | ||
import requests | ||
from importlib.metadata import PackageNotFoundError, version | ||
from ..exceptions import ASFDownloadError | ||
|
||
|
||
def download_url(url: str, dir: str, filename: str = None, token: str = None) -> None: | ||
""" | ||
Downloads a product from the specified URL to the specified location and (optional) filename. | ||
:param url: URL from which to download | ||
:param dir: Directory in which to save the product | ||
:param filename: Optional filename to be used, extracted from the URL by default | ||
:param token: EDL Auth Token for authenticating downloads, see https://urs.earthdata.nasa.gov/user_tokens | ||
:return: | ||
""" | ||
if filename is None: | ||
filename = os.path.split(urllib.parse.urlparse(url).path)[1] | ||
|
||
if not os.path.isdir(dir): | ||
raise ASFDownloadError(f'Error downloading {url}: directory not found: {dir}') | ||
|
||
if os.path.isfile(os.path.join(dir, filename)): | ||
raise ASFDownloadError(f'File already exists: {os.path.join(dir, filename)}') | ||
|
||
try: | ||
pkg_version = version(__name__) | ||
except PackageNotFoundError: | ||
pkg_version = '0.0.0' | ||
headers = {'User-Agent': f'{__name__}.{pkg_version}'} | ||
if token is not None: | ||
headers['Authorization'] = f'Bearer {token}' | ||
|
||
response = requests.get(url, stream=True, allow_redirects=False) | ||
while 300 <= response.status_code <= 399: | ||
new_url = response.headers['location'] | ||
if 'aws.amazon.com' in urllib.parse.urlparse(new_url).netloc: | ||
response = requests.get(new_url, stream=True, allow_redirects=False) # S3 detests auth headers | ||
else: | ||
response = requests.get(new_url, stream=True, headers=headers, allow_redirects=False) | ||
response.raise_for_status() | ||
with open(os.path.join(dir, filename), 'wb') as f: | ||
for chunk in response.iter_content(chunk_size=8192): | ||
f.write(chunk) |
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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
class ASFSearchError(Exception): | ||
"""Base Exception for asf_search""" | ||
class ASFError(Exception): | ||
"""Base ASF Exception, not intended for direct use""" | ||
|
||
class ASFSearchError(ASFError): | ||
"""Base search-related Exception""" | ||
|
||
class ASFSearch4xxError(ASFSearchError): | ||
"""Raise when SearchAPI returns a 4xx error""" | ||
|
||
class ASFSearch5xxError(ASFSearchError): | ||
"""Raise when SearchAPI returns a 5xx error""" | ||
|
||
class ServerError(ASFSearchError): | ||
"""Raise when SearchAPI returns an unknown error""" | ||
class ASFServerError(ASFSearchError): | ||
"""Raise when SearchAPI returns an unknown error""" | ||
|
||
class ASFBaselineError(ASFSearchError): | ||
"""Raise when baseline related errors occur""" | ||
|
||
class ASFDownloadError(ASFError): | ||
"""Base download-related Exception""" |
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
Oops, something went wrong.