import os
import re
import requests
from lxml import html
import json

#----------------------------------------------------------------
def getListaAtivos():
    HtmlTxt = get_url('https://opcoes.oplab.com.br/mercado/acoes/opcoes/PETR4')
    if HtmlTxt:
        # Regex para capturar os URLs
        pattern = r"\"/mercado/acoes/opcoes/[A-Z0-9]{5,6}\""
        
        # Encontrar todas as ocorrências do padrão
        matches = re.findall(pattern, HtmlTxt)
        matches = sorted(set(matches))
        
        # Imprimir as capturas
        #print(matches)
        listaAtivos = []
        for match in matches:
            #print(match[-6:-1])
            listaAtivos.append(match.split('/')[-1].replace('"', '').replace('\\',''))
        return listaAtivos
    #
#


def get_url(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
    return ""
#


def process_page(url, nome):
    html_txt = get_url(url)
    if html_txt:
        tree = html.fromstring(html_txt)
        node_list = tree.xpath('//*[@id="__NEXT_DATA__"]')
        #print(node_list[0].text)
        
        if node_list:
            jsonTxt = node_list[0].text
            return json.loads(jsonTxt)
            #if data:
                #print('data')
                #save_path = os.path.join(os.getcwd(), 'vols', f'{nome}.json')
                #os.makedirs(os.path.dirname(save_path), exist_ok=True)
                #with open(save_path, 'w', encoding='utf-8') as file:
                #    file.write(data)
            #
        #
    #
#


def removeKeys(branch, keysToKeep):
    branchKeys = list(branch.keys())
    #print(branchKeys)
    for key in branchKeys:
        if key not in keysToKeep:
            del branch[key]
            #for s in series:
    return branch
#


def cleanJson(json_node):
    #json_node = json.loads(jsonTxt)
    #print(json.dumps(json_node, indent=4))
    #json_node_new = json_node.copy()
    #pageProps = json_node['props']['pageProps']

    removeKeys(json_node, {'props'})


    keysToKeep = ['series']
    pageProps = json_node['props']['pageProps']
    removeKeys(pageProps, keysToKeep)
    #print(json.dumps(json_node, indent=4))

    series = json_node['props']['pageProps']['series']
    ##print(series)
    for serie in series:
        strikes = list(range(len(serie['strikes'])))
        for strike in strikes:
            #print(strike)
            #print(serie)
            #print(serie['strikes'])
            call = serie['strikes'][strike]['call']
            keysToKeep = ['symbol', 'strike', 'bs']
            removeKeys(call, keysToKeep)
            #
            bs = serie['strikes'][strike]['call']['bs']
            keysToKeep = ['volatility']
            removeKeys(bs, keysToKeep)
            #
            #
            put = serie['strikes'][strike]['put']
            keysToKeep = ['symbol', 'strike', 'bs']
            removeKeys(put, keysToKeep)
            #
            bs = serie['strikes'][strike]['put']['bs']
            keysToKeep = ['volatility']
            removeKeys(bs, keysToKeep)
        #
    return json_node
#


def save_to_local(file_content, file_name):
    folder = f"oplaboptionvols"
    os.makedirs(folder, exist_ok=True)  # Garante que a pasta existe
    file_path = os.path.join(folder, f"{file_name}.json")  
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(file_content)
    print(f"File saved: {file_path}")  # Mensagem de confirmação
#


listaAtivos = getListaAtivos()
base_url = 'https://opcoes.oplab.com.br/mercado/acoes/opcoes/'
for ativo in listaAtivos:#[0:1]:
    print(ativo)
    json_node = process_page(base_url + ativo, ativo)
    #
    json_clean = cleanJson(json_node)
    #
    file_name = ativo
    save_to_local(json.dumps(json_clean), file_name)