import requests
import pathlib


def dl_at_qual(pct):
    directory = pathlib.Path(f'imgs/danube_{pct}_pct')
    if not directory.exists():
        directory.mkdir()
    # for num in range(9, 51):
    for num in range(1, 9):
        path = pathlib.Path(f'imgs/danube_{pct}_pct/{num}.jpg')
        if path.exists():
            return
        else:
            # if num == 9:
            #     num = '09'
            num = '0' + str(num)
            r = requests.get(f'https://iiif.onb.ac.at/images/REPO/7502199/000000{num}.jpg/full/pct:{pct}/0/native.jpg')
            open(path, 'wb').write(r.content)


def dl_second_part(pct):
    directory = pathlib.Path(f'imgs/danube_{pct}_pct_part_ii')
    if not directory.exists():
        directory.mkdir()
    for num in range(1, 27):
        path = pathlib.Path(f'imgs/danube_{pct}_pct_part_ii/{num}.jpg')
        if path.exists():
            return
        else:
            if num < 10:
                num = '0' + str(num)
            r = requests.get(f'https://iiif.onb.ac.at/images/REPO/7505639/000000{num}.jpg/full/pct:{pct}/0/native.jpg')
            open(path, 'wb').write(r.content)


if __name__ == '__main__':
    # dl_at_qual(10)
    # dl_at_qual(50)
    dl_at_qual(100)
    # dl_second_part(50)