#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Search for products on CODE-DE

"""
import requests





API_QUERY = "https://finder.code-de.org/resto/api/collections/\
Sentinel2/search.json?maxRecords=2000&startDate=2015-06-01T00%3A00%3A00Z&completionDate=2020-08-31T23%3A59%3A59Z&location=all&processingLevel=LEVEL2A&productType=L2A&sortParam=startDate&sortOrder=descending&status=all&geometry=MULTIPOLYGON(((10.075764399693792+50.7584800022297%2C12.857942844697517+50.11881493057439%2C13.26617381810968+49.29881963934912%2C14.217987338132907+49.22218442674205%2C14.300279271021745+48.11263889020893%2C13.109595651339728+48.095905408956725%2C13.301598637480263+47.3767477717403%2C9.935377441223283+47.33192184804318%2C9.648877768451609+49.76911209425333%2C8.795579731357476+49.83629725294199%2C8.342330582735308+50.162347132709186%2C9.421883063132148+50.266440862302375%2C10.075764399693792+50.7584800022297)))&dataset=ESA-DATASET"

#%%
API_search_string = "https://finder.code-de.org/resto/api/collections/"
API_search_platform = "Sentinel2"
API_search_max_rec = "2000" # 2000 maximum
API_search_start_date = "2015-06-01"
API_search_end_date = "2015-12-31"
# mecklenburg-vorpommern, TODO method to load and shape from drive
#API_search_geometry = "geometry=MULTIPOLYGON(((10.075764399693792+50.7584800022297%2C12.857942844697517+50.11881493057439%2C13.26617381810968+49.29881963934912%2C14.217987338132907+49.22218442674205%2C14.300279271021745+48.11263889020893%2C13.109595651339728+48.095905408956725%2C13.301598637480263+47.3767477717403%2C9.935377441223283+47.33192184804318%2C9.648877768451609+49.76911209425333%2C8.795579731357476+49.83629725294199%2C8.342330582735308+50.162347132709186%2C9.421883063132148+50.266440862302375%2C10.075764399693792+50.7584800022297)))"
# bavaria 
API_search_geometry = "geometry=MULTIPOLYGON(((10.075764399693792+50.7584800022297%2C12.857942844697517+50.11881493057439%2C13.26617381810968+49.29881963934912%2C14.217987338132907+49.22218442674205%2C14.300279271021745+48.11263889020893%2C13.109595651339728+48.095905408956725%2C13.301598637480263+47.3767477717403%2C9.935377441223283+47.33192184804318%2C9.648877768451609+49.76911209425333%2C8.795579731357476+49.83629725294199%2C8.342330582735308+50.162347132709186%2C9.421883063132148+50.266440862302375%2C10.075764399693792+50.7584800022297)))"
API_search_product = "L2A"


#%%
tester = API_search_string + "&" + API_search_platform + "/search.json?maxRecords=" + API_search_max_rec + \
API_search_start_date + API_search_end_date + API_search_geometry + API_search_product


#%%




#%%
#API_QUERY = "https://finder.code-de.org/resto/api/collections/Sentinel2/search.json?maxRecords=2000&startDate=2020-06-01T00%3A00%3A00Z&completionDate=2020-06-10T23%3A59%3A59Z&location=all&processingLevel=LEVEL2A&productType=L2A&sortParam=startDate&sortOrder=descending&status=all&geometry=MULTIPOLYGON(((10.075764399693792+50.7584800022297%2C12.857942844697517+50.11881493057439%2C13.26617381810968+49.29881963934912%2C14.217987338132907+49.22218442674205%2C14.300279271021745+48.11263889020893%2C13.109595651339728+48.095905408956725%2C13.301598637480263+47.3767477717403%2C9.935377441223283+47.33192184804318%2C9.648877768451609+49.76911209425333%2C8.795579731357476+49.83629725294199%2C8.342330582735308+50.162347132709186%2C9.421883063132148+50.266440862302375%2C10.075764399693792+50.7584800022297)))&dataset=ESA-DATASET"
totalResults = requests.get(API_QUERY+str(1)).json()

nr = 1
page = 1
#%%
while True:
    try:
        result = requests.get(API_QUERY+str(page)).json()['features']
        page += 1
    except:
        print('exception error')
        break
    else:
        if result:
            for product in result:
                print(f"{product['properties']['productIdentifier']}")
                nr += 1
            else:
                stop = 1
                break
            print(f"number of records:{nr-1}")
           
#%% query token june 01 to 10th June 2020 using geojson extents
https://finder.code-de.org/resto/api/collections/Sentinel2/search.json?maxRecords=2000&startDate=2020-06-01T00%3A00%3A00Z&completionDate=2020-06-10T23%3A59%3A59Z&location=all&processingLevel=LEVEL2A&productType=L2A&sortParam=startDate&sortOrder=descending&status=all&geometry=MULTIPOLYGON(((10.075764399693792+50.7584800022297%2C12.857942844697517+50.11881493057439%2C13.26617381810968+49.29881963934912%2C14.217987338132907+49.22218442674205%2C14.300279271021745+48.11263889020893%2C13.109595651339728+48.095905408956725%2C13.301598637480263+47.3767477717403%2C9.935377441223283+47.33192184804318%2C9.648877768451609+49.76911209425333%2C8.795579731357476+49.83629725294199%2C8.342330582735308+50.162347132709186%2C9.421883063132148+50.266440862302375%2C10.075764399693792+50.7584800022297)))&dataset=ESA-DATASET



#%%


