
# 获取每一个时间段的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 map_exclude_df(df1,ex_df):
  
    
    ex_df.rename(columns={'ex_add': 'holder_owner', 'info': 'domains_owned'}, inplace=True)

    # 使用merge创建一个临时DataFrame，只包含匹配的行
    temp_df = df1[['holder_owner']].merge(ex_df, on='holder_owner', how='left')
    
    # 更新df1的'b1'列
    df1.set_index('holder_owner', inplace=True)
    temp_df.set_index('holder_owner', inplace=True)
    df1.update(temp_df)

    # 重置索引
    df1.reset_index(inplace=True)

    return df1

def merge_df_fun():
 
    
    global arr_file , merged_df , domains_df,token_add
    for index, file in enumerate(arr_file):
        date_range = file.replace(".csv","").split("_")
        date_range = date_range[-1]
               
        cur_df =   pd.read_csv(file ,dtype=object)
        cur_df=cur_df.dropna(subset=['holder_owner'])

        cur_df =cur_df.astype({
            "amount_M":float,
        })
        cur_df =cur_df.round(1)
        cur_df["belong_date"] =date_range


        cur_df=cur_df.sort_values(by=['amount_M'],ascending = False)
        cur_df =cur_df.head(22)
        cur_df=cur_df[['holder_owner','domains_owned','amount_M','belong_date']]


        if index==0 :
            merged_df = cur_df
            continue
        merged_df = pd.concat([merged_df, cur_df])
  
    
    # merged_df =merged_df.drop_duplicates(subset=['holder_owner'])

    ex_df = pd.read_csv('exclude_add.csv',dtype=object)
    merged_df=map_exclude_df(merged_df ,ex_df)
    merged_df['is_dup'] = merged_df.duplicated(subset='holder_owner', keep=False).astype(int)


    merged_df["gmgn"] = merged_df.apply(lambda ser: makeurl(
    f"https://gmgn.ai/sol/token/{token_add}?tab=activity&maker={ser['holder_owner']}" , "gmgn" ), axis=1)
    merged_df["defined"] = merged_df.apply(lambda ser: makeurl(
     f"https://www.defined.fi/sol/{token_add}?maker={ser['holder_owner']}" , "defined" ), axis=1)
    merged_df["beizhu"] = None

    merged_df.to_excel(f'001_top20_holders_dune_{int(time.time())}.xlsx', index=False)

 
    
token_add = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump'
if __name__ == "__main__":
 
 
    arr_file = os.listdir(os.getcwd())
  
    arr_file = [file for file in arr_file if    file.startswith("dune_holders_xx") and file.endswith(".csv")]
 
    arr_file.sort()
    print("read file: ",arr_file)
    
    merge_df_fun()
 
    