import time
import base64
from selenium import webdriver


def download_images(url, destination_folder, chromedriver_path):
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory": destination_folder}
    chromeOptions.add_experimental_option("prefs", prefs)
    driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions)
    driver.get(url)
    driver.implicitly_wait(10)
    pages = driver.find_elements_by_class_name("page-controls__page-numbers")
    page_count = int(pages[0].text.split(' / ')[1])
    default_index = 3
    for index in range(page_count):
        try:
            canvas = driver.find_element_by_xpath(f'//*[@id="ird3-main"]/div/div/div[{default_index}]/div[1]/div[1]/canvas')
            base64_image = base64.b64decode(canvas.screenshot_as_base64)
            # save to a file
            with open(rf"{destination_folder}\\image_{index}.jpg", 'wb') as f:
                f.write(base64_image)
                print(f'image_{index}.jpg downloaded!')
                next_button = driver.find_element_by_id('ird3-button-next')
                next_button.click()
                time.sleep(5)
        except Exception as ex:
            print(ex.msg)
            continue


if __name__ == '__main__':
    # download python 3.9.7
    # pip install selenium   
    # The image saving path:
    destination_folder = r"C:\Users\USER\Downloads\BookCoursePDF"
    #  You must download a chromedriver and specify a full path to it
    chromedriver_path = r"C:\Users\USER\Downloads\Compressed\chromedriver.exe"
    # URL of the flipbooks/PDF website
    url = "https://issuu.com/Website-address"
    print("Start")
    download_images(url, destination_folder, chromedriver_path)
    print("Done.")
