
# 获取每一个时间段的top20 持仓，汇总到一个excel表格中
import pandas as pd
import sys
import os
import time

arr_file = []
merged_df = None
def makeurl( url,url_show):
    return '=HYPERLINK("{}","{}")'.format(url,  url_show)
def handle_df_fun():
    
    global arr_file , merged_df 
    for index, file in enumerate(arr_file):
        cur_df =   pd.read_csv(file )
    
        if index==0 :
            merged_df = cur_df
            continue
    merged_df["early_trader_number"] =   (merged_df['buy_create_time_diff'] < 45).groupby(merged_df['token_address']).transform('sum')
    
    

 
    
    merged_df["gmgn"] = merged_df.apply(lambda ser: makeurl(
    f"https://gmgn.ai/sol/token/{ser['token_address']}?tab=activity&maker={ser['trader_id']}" , "gmgn" ), axis=1)
    merged_df["defined"] = merged_df.apply(lambda ser: makeurl(
     f"https://www.defined.fi/sol/{ser['token_address']}?maker={ser['trader_id']}" , "defined" ), axis=1)
    
    merged_df["beizhu"] = None
    merged_df =  merged_df.sort_values(by=['early_trader_number','token_address','buy_create_time_diff'], ascending=[False, True,True])
 
 
 

    merged_df.to_excel(f'003_buy_sametoken_time_diff_row_dune_{int(time.time())}.xlsx', index=False)
    return
 
    

if __name__ == "__main__":
 
 
    arr_file = os.listdir(os.getcwd())
  
    arr_file = [file for file in arr_file if "dune_buy_sametoken_time_diff" in file and file.endswith(".csv")]
 
    arr_file.sort()
    print("read file: ",arr_file)
    
    handle_df_fun()
 
    