import os, tqdm, sys
import requests
import urllib.request
import re
#from bs4 import BeautifulSoup
import multiprocessing
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#from webdriver_manager.firefox import GeckoDriverManager
from selenium import webdriver
from utillc import *
from selenium.webdriver.firefox.service import Service as ServiceF
from selenium.webdriver.chrome.service import Service as ServiceC
import PIL
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import numpy as np
#wd = browser = webdriver.Firefox(executable_path=    GeckoDriverManager().install())
EKO()
import sys
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
import time
import cherrypy
import argparse
import PIL
from PIL import Image
import orange

# petites cités
# https://petitescitesdecaractere.com/fr/resultats-de-recherche/*

# aires
# https://www.aireservices.com/reference.html
# https://www.campingcarpark.com/fr_FR/mon-compte/dashboard



def tqdmtqdm(x) : return x

class P(orange.RobotBase) :

    def __init__(self) :
        super().__init__()
        self.build(headless=True)
        pass

    def xx() :
        options = FirefoxOptions()
        options = ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        # changing user-agent because etoro detects the automated browser somehow
        options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) "
                             "Chrome/86.0.4240.183 Safari/537.36")
        EKO()


        s = ServiceC('/usr/bin/chromedriver')
        bot = self.driver = webdriver.Chrome(service=s, options=options)
        #self.driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=options)
        #self.driver = webdriver.Firefox(options=options)
        #self.driver.get("https://pythonbasics.org")
        #wd = webdriver.Firefox(service=service, options=options)
        EKO()

    def load(self) :
        self.url = "https://fr.wikipedia.org/wiki/Liste_des_localit%C3%A9s_adh%C3%A9rant_au_r%C3%A9seau_Les_Plus_Beaux_Villages_de_France"

        bot = self.driver
        browser = self.driver.get(self.url)
        time.sleep(2)
        self.driver.implicitly_wait(5)

        xpath = '/html/body/div[2]/div/div[3]/main/div[3]/div[3]/div[1]/table/tbody/tr'

        lst = ""
        
        elems = bot.find_elements(By.XPATH, xpath)
        EKOX(len(list(elems)))
        region, dept = None, None
        for ie, e in enumerate(elems) :
            rows = list(e.find_elements(By.XPATH, './/td'))
            if len(rows) >= 5 :
                r = rows[-5]
                raa = r.find_element(By.XPATH, './/a')            
                region = raa.get_attribute('innerHTML')

            if len(rows) >= 4 :
                d = rows[-4]
                daa = d.find_element(By.XPATH, './/a')            
                dept = daa.get_attribute('innerHTML')
            e = rows[-3]
            aa = e.find_element(By.XPATH, './/a')
            

            lst += region + ", " + dept + ", " + aa.get_attribute('title') + ", France" + "\n"

        EKOX(lst)
        bot.close()  # shuts down the bot

        with open("pbvf.csv", "w") as fd :
            fd.write(lst)
        
    def pcdc(self) :
        """
        petites cités de caracteres
        """
        bot = self.driver
        time.sleep(2)
        self.driver.implicitly_wait(5)
        lst = ""
        for i in range(25) :
            EKOX(i)
            self.url = 'https://petitescitesdecaractere.com/fr/resultats-de-recherche/*?page=%d' % i
            try :
                browser = self.driver.get(self.url)
                xpath = '/html/body/div[2]/div/section/div[2]/section/div/ol/li'

                elems = bot.find_elements(By.XPATH, xpath)
                EKOX(len(list(elems)))
                region, dept = None, None
                for ie, e in enumerate(elems) :
                    aa = e.find_element(By.XPATH, './/a')
                    EKOX(aa.get_attribute('innerHTML'))
                    lst += aa.get_attribute('innerHTML') + ", France" +  "\n"
            except :
                pass

        bot.close()  # shuts down the bot

        with open("pcdc.csv", "w") as fd :
            fd.write(lst)

if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        prog='pbvf from wikipedia',
        description='download ferrand podcast')
    args = parser.parse_args()
    p = P()
    p.pcdc()
    #p.load()

