def test_download_all(tmpdir): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) # From https://scihub.copernicus.eu/apihub/odata/v1/Products?$top=5&$orderby=ContentLength filenames = [ "S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E", "S1A_WV_OCN__2SSV_20150526T211029_20150526T211737_006097_007E78_134A", "S1A_WV_OCN__2SSV_20150526T081641_20150526T082418_006090_007E3E_104C" ] api.query_raw(" OR ".join(filenames)) assert len(api.get_products()) == len(filenames) # Download normally result = api.download_all(str(tmpdir)) assert len(result) == len(filenames) for path, product_info in result.items(): pypath = py.path.local(path) assert pypath.purebasename in filenames assert pypath.check(exists=1, file=1) assert pypath.size() == product_info["size"] # Force one download to fail path, product_info = list(result.items())[0] py.path.local(path).remove() with requests_mock.mock(real_http=True) as rqst: url = "https://scihub.copernicus.eu/apihub/odata/v1/Products('%s')/?$format=json" % product_info[ "id"] json = api.session.get(url).json() json["d"]["Checksum"]["Value"] = "00000000000000000000000000000000" rqst.get(url, json=json) result = api.download_all(str(tmpdir), max_attempts=1, checksum=True) assert len(result) == len(filenames) assert result[path] is None
def test_get_products_size(): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) api.query(get_coordinates('tests/map.geojson'), "20151219", "20151228", platformname="Sentinel-2") assert api.get_products_size() == 63.58 api.query_raw( "S1A_WV_OCN__2SSH_20150603T092625_20150603T093332_006207_008194_521E") assert len(api.get_products()) > 0 # Rounded to zero assert api.get_products_size() == 0
class SatelliteRetriever: def __init__(self, login, password): self.username = login self.password = password def login(self): self.api = SentinelAPI(self.username, self.password, 'https://scihub.copernicus.eu/dhus') def getAvailableImages(self, lat, long): products = self.api.query_raw( 'beginposition:[2019-10-01T00:00:00.000Z TO NOW] footprint:"intersects(' + str(lat) + ',' + str(long) + ')" L0') images = [] for productId in products: product = products[productId] time = product['beginposition'].strftime('%s') images.append({ 'url': product['link'], 'name': product['title'], 'time': int(time) }) return images
def test_invalid_query(): api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD']) with pytest.raises(SentinelAPIError) as excinfo: api.query_raw("xxx:yyy") assert excinfo.value.msg is not None print(excinfo)