import os
import glob
import subprocess
import requests
from nansat.node import Node
from string import Template

"""
https://scihub.esa.int/dhus/search?q=GRD AND S1
https://scihub.esa.int/dhus/search?q=ingestionDate:[NOW-1DAY TO NOW]
https://scihub.esa.int/dhus/search?q=ingestionDate:[NOW-30DAYS TO NOW]
https://scihub.esa.int/dhus/search?q=ingestionDate:[NOW-1HOUR TO NOW]
https://scihub.esa.int/dhus/search?q=productType:GRD AND ingestionDate:[NOW-1DAY TO NOW]
https://scihub.esa.int/dhus/search?q=beginPosition:[NOW-3MONTHS TO NOW] AND endPosition:[NOW-3MONTHS TO NOW]
https://scihub.esa.int/dhus/search?q=polarisationmode:VV AND footprint:"Intersects(POLYGON((-4.53 29.85, 26.75 29.85, 26.75 46.80,-4.53 46.80,-4.53 29.85)))"

https://scihub.esa.int/dhus/odata/v1/Products('16902fd3-f323-4014-a950-853ac602e22f')/Nodes('S1A_IW_SLC__1SDV_20141101T165548_20141101T165616_003091_0038AA_558F.SAFE')/Nodes('preview')/Nodes('quick-look.png')/$value
"""

def list_local_files(dstDir):
    ''' Get list of already downloaded local files in the source directory '''
    # list .zip and .SAFE files
    localFiles = sorted(glob.glob(dstDir + '/S1A_*.zip'))
    localFiles += sorted(glob.glob(dstDir + '/S1A_*.SAFE'))
    # only keep names without path and extensions
    localFiles = [os.path.split(localFile)[1].split('.')[0] for localFile in localFiles]
    print 'Already downloaded %d files' % len(localFiles)
    return localFiles

def list_remote_files(polygon, nScenes, auth):
    ''' Get list of all available files (and IDs) on ESA RA '''
    remoteFiles = []
    # send request to SCIHUB and get XML with description of matching files
    r = requests.get('https://scihub.esa.int/dhus/search?q=footprint:"Intersects(%s)"&rows=%d' % (polygon, nScenes), auth=auth)
    # convert XML to Node
    n = Node.create(str(r.text.replace('\n', '')))
    # loop through nodes and fetch filenames
    for i, entry in enumerate(n.nodeList('entry')[::-1]):
        # get title and product ID
        title = str(entry['title'])
        productID = str(entry['id'])
        # get format of file and skip L0
        fmt = title.split('_')[2]
        if fmt == 'RAW':
            continue

        # get file name
        fileName = None
        # loop through children of the node
        for linkTag in entry.nodeList('str'):
            # find entries with attribute name=filename
            if u'name' in linkTag.attributes and linkTag.attributes[u'name'] == u'filename' :
                fileName = str(linkTag.value)
                break
        remoteFiles.append((productID, fileName))

    return remoteFiles

def download_file(productID, fileName, dhusTemplate, localFiles, dstDir, auth):
    ''' Download file for the given productID and fileName '''
    # get output file name
    oFileName = os.path.join(dstDir, fileName.replace('.SAFE', '.zip'))
    if not oFileName.split('.')[0] in localFiles:
        # create link to file
        dhusURL = dhusTemplate.replace('PRODUCTID', productID)

        wgetCommand = ('wget --no-check-certificate --user=%s --password=%s -O %s %s' % (
                  auth[0], auth[1], oFileName, dhusURL))
        print wgetCommand
        status = subprocess.call(wgetCommand.split(' '))
        print status

# sample wget command
#wget --user=korosov --password=sentinel2014 "https://scihub.esa.int/dhus/odata/v1/Products('efbd9bf1-079c-460a-8a08-8157478cf099')/\$value"
# template of the downloadable URL
dhusTemplate = "https://scihub.esa.int/dhus/odata/v1/Products('PRODUCTID')/$value"

# number of scence to search
nScenes = 3
# download destination directory
dstDir = '/Data/sat/downloads/sentinel1/'
# areas of download
polygons = [
    # svalbard
    'POLYGON((-10 74, 50 74, 50 80, -10 80, -10 74))',
    # agulhas
    'POLYGON((24 -35, 32 -35, 32 -30, 24 -30, 24 -35))',
    # lofoten
    'POLYGON((10 67, 15 67, 15 70, 10 70, 10 67))',
]
# authentication information
auth = ('korosov', 'sentinel2014')

# get list of already downloaded files
localFiles = list_local_files(dstDir)
# download data for each area
for polygon in polygons:
    print polygon
    # get list of available files from scihub
    remoteFiles = list_remote_files(polygons[0], nScenes, auth)
    # download each file
    for remoteFile in remoteFiles:
        download_file(remoteFile[0], remoteFile[1], dhusTemplate, localFiles, dstDir, auth)

# change group and permissions
subprocess.call(['chown', ':15001', dstDir, '-R'])
subprocess.call(['chmod', '775', dstDir, '-R'])
