Find exports for a given account

def get_exports(client, bucket, prefix, latest=True):
    """Find exports for a given account
    """
    keys = client.list_objects_v2(
        Bucket=bucket, Prefix=prefix, Delimiter='/').get('CommonPrefixes', [])
    found = []
    years = []
    for y in keys:
        part = y['Prefix'].rsplit('/', 2)[-2]
        if not part.isdigit():
            continue
        year = int(part)
        years.append(year)

    if not years:
        return []

    years.sort(reverse=True)
    if latest:
        years = [years[0]]

    for y in years:
        keys = client.list_objects_v2(
            Bucket=bucket, Prefix="%s/%d/" % (prefix.strip('/'), y),
            Delimiter='/').get('CommonPrefixes', [])
        months = []
        for m in keys:
            part = m['Prefix'].rsplit('/', 2)[-2]
            if not part.isdigit():
                continue
            month = int(part)
            date_key = (y, month)
            months.append(month)
        months.sort(reverse=True)
        if not months:
            continue
        if latest:
            months = [months[0]]
        for m in months:
            keys = client.list_objects_v2(
                Bucket=bucket, Prefix="%s/%d/%s/" % (
                    prefix.strip('/'), y, ('%d' % m).rjust(2, '0')),
                Delimiter='/').get('CommonPrefixes', [])
            for d in keys:
                part = d['Prefix'].rsplit('/', 2)[-2]
                if not part.isdigit():
                    continue
                day = int(part)
                date_key = (y, m, day)
                found.append(date_key)
    found.sort(reverse=True)
    if latest:
        found = [found[0]]
    return found