from pyvirtualdisplay import Display
from selenium import webdriver
import time
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

#from selenium.webdriver.common.by import By
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import sys,traceback

def process_download(driver):
    try:
        downnow = driver.find_element_by_xpath('//a/div[@class="download_now"]')
        downlink = downnow
        downlink.click()

        #downclick = driver.find_element_by_xpath('//a[@text="Download Manually"]')
        #downclick.click()
        
        # wait
        time.sleep(20)
        #exit()


    except Exception as e:
        traceback.print_exc(file=sys.stdout)
        time.sleep(4)


def process_first_page(driver):
    # get download links
    downloads = driver.find_elements_by_class_name('search_result_title')
    print(len(downloads))
    maxi = len(downloads)



    i=0
    while i< maxi:
        try:
            downloads = driver.find_elements_by_class_name('search_result_title')
            a = downloads[i].find_element_by_tag_name('a')
            print(a.text)
            print(a.get_attribute('href'))

            # download each file
            #a.click()
            # open in a new tab
            a.send_keys(Keys.CONTROL + Keys.RETURN)
            handles = driver.window_handles
            driver.switch_to_window(handles[2])
            
            process_download(driver)

            # verify there's no more download pending?

        except Exception as e:
            traceback.print_exc(file=sys.stdout)
            
            
        finally:
            # go back
            driver.close()
            driver.switch_to_window(handles[1])

        i+=1
        time.sleep(5)  



def process_page(driver):
    # get download links
    downloads = driver.find_elements_by_class_name('search_result_title')
    print(len(downloads))
    maxi = len(downloads)



    i=0
    while i< maxi:
        try:
            downloads = driver.find_elements_by_class_name('search_result_title')
            a = downloads[i].find_element_by_tag_name('a')
            print(a.text)
            print(a.get_attribute('href'))

            # download each file
            #a.click()
            # open in a new tab
            a.send_keys(Keys.CONTROL + Keys.RETURN)
            handles = driver.window_handles
            driver.switch_to_window(handles[3])
            
            process_download(driver)

            # verify there's no more download pending?

        except Exception as e:
            traceback.print_exc(file=sys.stdout)
            
            
        finally:
            # go back
            driver.close()
            driver.switch_to_window(handles[2])
        i+=1
        time.sleep(5)  


def process_category(driver):
    # pagination
    pagination = driver.find_element_by_class_name('search_pagination')
    pages = pagination.find_elements_by_tag_name('a')

    process_first_page(driver)

    for page in pages[:2]:
        print(page.get_attribute('href'))
        print(page.text)

        page.send_keys(Keys.CONTROL + Keys.RETURN)
        handles = driver.window_handles
        driver.switch_to_window(handles[2])

        try:
            process_page(driver)
        except Exception as e:
            traceback.print_exc(file=sys.stdout)
        

        driver.close()
        driver.switch_to_window(handles[1])


def process_category_all(driver):
    # pagination
    pagination = driver.find_element_by_class_name('search_pagination')
    pages = pagination.find_elements_by_tag_name('a')

    process_first_page(driver)

    for page in pages[2:]:
        print(page.get_attribute('href'))
        print(page.text)

        page.send_keys(Keys.CONTROL + Keys.RETURN)
        handles = driver.window_handles
        driver.switch_to_window(handles[2])

        try:
            process_page(driver)
        except Exception as e:
            traceback.print_exc(file=sys.stdout)
        

        driver.close()
        driver.switch_to_window(handles[1])

if __name__ == '__main__':

    headless = False
    if headless:
        display = Display(visible=0, size=(800, 600))
        display.start()

    options = webdriver.ChromeOptions() 
    options.add_argument("download.default_directory=/home/homero/KDB/Projectes/dakd-project/samples/safe/")

    driver = webdriver.Chrome(chrome_options=options)

    driver.get('http://www.tucows.com/downloads')
    print (driver.title)




    # get file categories

    left_menu2 = driver.find_element_by_class_name('left_menu')
    categories = left_menu2.find_elements_by_tag_name('a')

    # print(len(categories))
    # for cat in categories:
    #     print(cat.text)
    #     print(cat.get_attribute('href'))

    j=34
    while j < len(categories):
        categories[j].send_keys(Keys.CONTROL + Keys.RETURN)
        handles = driver.window_handles
        driver.switch_to_window(handles[1])

        try:
            process_category(driver)
        except Exception as e:
            traceback.print_exc(file=sys.stdout)
        
        driver.close()
        driver.switch_to_window(handles[0])
        j +=1

    j=0
    while j < len(categories):
        categories[j].send_keys(Keys.CONTROL + Keys.RETURN)
        handles = driver.window_handles
        driver.switch_to_window(handles[1])

        try:
            process_category_all(driver)
        except Exception as e:
            traceback.print_exc(file=sys.stdout)
        
        driver.close()
        driver.switch_to_window(handles[0])
        j +=1