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
from selenium.webdriver.common.action_chains import ActionChains
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

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, outdir, unfold) :
        self.url = "https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles"
        bot = self.driver
        browser = self.driver.get(self.url)

        self.driver.implicitly_wait(5)
            
        EKOX(self.driver.title)
        html_source = self.driver.page_source
        #with open("html.html", "w") as fd : fd.write(html_source)

        EKOX(hash(html_source))
        #EKOX(dir(bot))

        for i in range(unfold) :
            EKOX(i)
            button = bot.find_element(By.XPATH, '//button[@data-testid="LoadNext"]')
            EKOX(button)
            try :
                ActionChains(bot).move_to_element(button).click(button).perform()
                EKOT("clicked")
                #elem.click()
                time.sleep(2)
                EKO()
            except Exception as ex:
                EKOX(ex)
                EKOT("tout déplié")
                break
            
        xpath = '//li[@class="Collection-section-items-item svelte-1qwu1v2 separators"]'
        elems = bot.find_elements(By.XPATH, xpath)
        EKOX(len(list(elems)))
        for ie, e in tqdmtqdm(enumerate(list(elems))) :
            x2 = e.find_element(By.XPATH, './/a[@data-testid="link"]')
            EKOX(x2.text)
            title = x2.text
            
            x3 = e.find_element(By.XPATH, './/button[@type="button"]')
            x3.click()
            time.sleep(1)

            EKO()
            p1 = bot.find_element(By.XPATH, "/html/body/audio")
            href = p1.get_attribute('src')
            EKOX(href)
            filename = title.replace("\n", "").replace("/", "_").replace("'", "_").strip() + ".mp3"
            ll = [ "'", "?", " ", ":"]
            for ee in ll :
                filename = filename.replace(ee, "_")
            EKOX(filename)
            # declick ce podcast
            if not os.path.exists(filename) :
                EKOT("downloading")
                doc = requests.get(href)
                EKOT("writing")                
                with open(os.path.join(outdir, filename), 'wb') as f:
                    f.write(doc.content)
                    EKOT("%s written" % filename)
            else :
                EKOT("%s already there" % filename)
            
            
                
        bot.close()  # shuts down the bot


if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        prog='ferrand aspirator',
        description='download ferrand podcast')
    parser.add_argument('--outdir')
    parser.add_argument('--nunfold', type=int, default=2)
    args = parser.parse_args()
    p = P()
    os.makedirs(args.outdir, exist_ok=True)
    p.load(args.outdir, args.nunfold)

