
# from datetime import datetime, timezone
# from playwright.async_api import async_playwright, Playwright
# from playwright.sync_api import expect
from playwright.async_api import Playwright, async_playwright, expect
import random

from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)

baseclass.makedirpath(baseclass.browsercookie_path)
baseclass.makedirpath(baseclass.dalao_solscan_info_path)
old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
 

async def do_some_thing(playwright,   arr_url_dalao , url_type):

    # browser =  playwright.chromium.launch(headless=True)
    context = await playwright.firefox.launch_persistent_context(user_data_dir=USER_DIR_PATH, accept_downloads=True, headless=False)
    # context = browser.new_context()
    context.set_default_timeout(19000)
    # context = await browser.new_context()
    arr_page = [None]*2
    arr_page[0] = await context .new_page()
    await arr_page[0].goto(
        "https://solscan.io/")

    # print(context.pages)

    for idx_url in range(0, len(arr_url_dalao)):

        str_dalaoTransferUrl = arr_url_dalao[idx_url]
        str_dalaoaddress =obj_url_2_dalao[str_dalaoTransferUrl]
        
        print(f"enter str_dalaoaddress= {str_dalaoaddress} url_type={url_type}")
        # print(f"enter str_dalaoTransferUrl= {str_dalaoTransferUrl}")
        print(f"cur_arr_str_dalaoaddress remainder = {len(arr_url_dalao)-idx_url}")
        arr_page[1] = await context.new_page()
        page = arr_page[1]

        async with page.expect_download() as download_info:
            try:
                await page.goto(str_dalaoTransferUrl, timeout=0)
            except:
                # Wait for the download to start
                download = await download_info.value
                # Wait for the download process to complete
                print(await download.path())
                # Save downloaded file somewhere
                await download.save_as(baseclass.dalao_solscan_info_path / f"{url_type}_{str_dalaoaddress}.csv")
            await page.wait_for_timeout(2200)
        time.sleep(1)
        await page.close()

    await context.close()


async def get_onedriver_swapactivities( arr_url_dalao , url_type):
    async with async_playwright() as playwright:

        await do_some_thing(playwright, arr_url_dalao, url_type)


async def main():
    print("enter main()")
    if len(arr_dalao_sol_transfer_url)>0:
        await get_onedriver_swapactivities(
            arr_url_dalao = arr_dalao_sol_transfer_url , url_type = "sol_transfer")
        await asyncio.sleep(3)
        
    if len(arr_dalao_token_transfer_url)>0:
        await get_onedriver_swapactivities(
            arr_url_dalao = arr_dalao_token_transfer_url , url_type = "token_transfer")
        await asyncio.sleep(3)
 
 
    if len(arr_dalao_usdc_transfer_url)>0:
        await get_onedriver_swapactivities(
            arr_url_dalao = arr_dalao_usdc_transfer_url , url_type = "usdc_transfer")

    if len(arr_dalao_special_token_transfer_url)>0:
        await get_onedriver_swapactivities(
            arr_url_dalao = arr_dalao_special_token_transfer_url , url_type = "special_token")





find_depth = "depth_01"
df = pd.read_excel(baseclass.dalao_tran_gmgnOrdex_path /
                 "input_dalao.xlsx", dtype=object)

str_tokenaddress_dalao_source = "4GULMPKBJLruChBZWksZzukAg1AjSCmCTMn9ny2Xpump"
arr_str_dalaoaddress = df[f'dalao_{find_depth}'].drop_duplicates().dropna().tolist()
obj_ex_address ={
    
    "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1":"buy-achipompomhat.sol",
"CoXccZ3RqJGHQspgei4NdMCW2No1EtTCgevy9CYwaNey":"cryptoxiao.sol",
"6dEm5Z5NGqNuavGV2fmiRnE55V9jpygsy8RngLxszBGA":"coinbaseventures.sol"
}
arr_exclude_address= list(obj_ex_address.keys())
arr_str_dalaoaddress=[add for add in arr_str_dalaoaddress if add not in  arr_exclude_address]
arr_dalao_sol_transfer_url =[]
arr_dalao_token_transfer_url =[]
arr_dalao_special_token_transfer_url =[]
arr_dalao_usdc_transfer_url =[]
obj_url_2_dalao={
    
}
def init():
    now_time = time.time()
    global obj_url_2_dalao
    global arr_dalao_sol_transfer_url ,arr_dalao_token_transfer_url ,    arr_dalao_usdc_transfer_url
    for str_dalaoaddress in arr_str_dalaoaddress: 
 
        sol_transfer_file = baseclass.dalao_solscan_info_path / f"sol_transfer_{str_dalaoaddress}.csv"
        token_transfer_file = baseclass.dalao_solscan_info_path / f"token_transfer_{str_dalaoaddress}.csv"
        special_token_transfer_file = baseclass.dalao_solscan_info_path / f"special_token_transfer_{str_dalaoaddress}.csv"
        usdc_transfer_file = baseclass.dalao_solscan_info_path / f"usdc_transfer_{str_dalaoaddress}.csv"
        
        
 
        url_sol_transfer = f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&activity_type[]=ACTIVITY_SPL_TRANSFER&activity_type[]=ACTIVITY_SPL_MINT&amount[]=0.3&amount[]=undefined&token=So11111111111111111111111111111111111111111&exclude_amount_zero=true&remove_spam=true"    
        # url_token_inflow_transfer = f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&activity_type[]=ACTIVITY_SPL_TRANSFER&activity_type[]=ACTIVITY_SPL_MINT&exclude_token=So11111111111111111111111111111111111111111&exclude_amount_zero=true&remove_spam=true&flow=in"
        # url_token_outflow_transfer = f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&activity_type[]=ACTIVITY_SPL_TRANSFER&activity_type[]=ACTIVITY_SPL_MINT&exclude_token=So11111111111111111111111111111111111111111&exclude_amount_zero=true&remove_spam=true&flow=out"
        url_token_transfer = f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&activity_type[]=ACTIVITY_SPL_TRANSFER&activity_type[]=ACTIVITY_SPL_MINT&exclude_token=So11111111111111111111111111111111111111111&exclude_amount_zero=true&remove_spam=true"
 
        url_special_token_transfer =f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&token={str_tokenaddress_dalao_source}"
        url_usdc_transfer = f"https://api-v2.solscan.io/v2/account/transfer/export?address={str_dalaoaddress}&activity_type[]=ACTIVITY_SPL_TRANSFER&activity_type[]=ACTIVITY_SPL_MINT&amount[]=100&amount[]=undefined&token=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&exclude_amount_zero=true&remove_spam=true"
        obj_url_2_dalao[url_sol_transfer] =str_dalaoaddress
 
        obj_url_2_dalao[url_token_transfer] =str_dalaoaddress
        obj_url_2_dalao[url_usdc_transfer] =str_dalaoaddress
        obj_url_2_dalao[url_special_token_transfer] = str_dalaoaddress
        
    
        if sol_transfer_file.exists():
            modified_time  =  sol_transfer_file.stat().st_mtime
            # 3 * 60 分钟内不会在获取
            if(now_time -modified_time >12 * 60 * 60):
                arr_dalao_sol_transfer_url.append(url_sol_transfer)
            else:
                pass
        else:
            arr_dalao_sol_transfer_url.append(url_sol_transfer)
            
        if token_transfer_file.exists():
            modified_time  =  token_transfer_file.stat().st_mtime
            # 3 * 60 分钟内不会在获取
            if(now_time -modified_time >12 * 60 * 60):
                arr_dalao_token_transfer_url.append(url_token_transfer)
            else:
                pass
        else:
            arr_dalao_token_transfer_url.append(url_token_transfer)
            
 
        if usdc_transfer_file.exists():
            modified_time  =  usdc_transfer_file.stat().st_mtime
            # 3 * 60 分钟内不会在获取
            if(now_time -modified_time >12 * 60 * 60):
                arr_dalao_usdc_transfer_url.append(url_usdc_transfer)
            else:
                pass
        else:
            arr_dalao_usdc_transfer_url.append(url_usdc_transfer)
            
        if special_token_transfer_file.exists():
            modified_time  =  special_token_transfer_file.stat().st_mtime
            # 3 * 60 分钟内不会在获取
            if(now_time -modified_time >12 * 60 * 60):
                arr_dalao_special_token_transfer_url.append(url_special_token_transfer)
            else:
                pass
        else:
            arr_dalao_special_token_transfer_url.append(url_special_token_transfer)
            
          
            
            


init()
if len(arr_dalao_sol_transfer_url) ==0 and len(arr_dalao_token_transfer_url) ==0 and  len(arr_dalao_usdc_transfer_url)==0 and len(arr_dalao_special_token_transfer_url)==0:
    print("dont has dalao need get")
    exit()

downloads_path = baseclass.dalao_solscan_info_path
USER_DIR_PATH = baseclass.browsercookie_path
asyncio.run(main())
print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
