
# 将special add 在不同时间持仓的csv合并为一个excel
import pandas as pd
import sys
import pytz

arr_file = []
merged_df = None
 
file_transfer_row = ''
arr_all_holders=[]
def makeurl( url,url_show):
    return '=HYPERLINK("{}","{}")'.format(url,  url_show)

def utc_2_shanghaidate(df , utc_time_col ):
# 将字符串转换为UTC时间的datetime对象
    df[utc_time_col] = pd.to_datetime(df[utc_time_col], utc=True)
    # 定义UTC+8时区
    utc_plus_8 = pytz.timezone('Asia/Shanghai')
    # 转换为UTC+8时间
    # df['shanghai_date'] = df['shanghai_date'].dt.tz_convert(utc_plus_8)
    # # 提取时间字符串
    # df['shanghai_date'] = df['shanghai_date'].dt.strftime('%Y-%m-%d %H:%M')
    df[utc_time_col] = df[utc_time_col].dt.tz_convert(utc_plus_8)
    # 提取时间字符串
    df[utc_time_col] = df[utc_time_col].dt.strftime('%Y-%m-%d %H:%M')
    return df
import re

def get_daterange_df(df, str_end_datetime = ''):
    # str_end_datetime = '2025-01-16 02:00:00'

    str_end =  re.split(r'[- :]', str_end_datetime)
    str_end =  str_end[1]+str_end[2]+ '日'+ str_end[3]+str_end[4]
    str_daterange =  '00_' + str_end
    df_holder =  df[    
      (df['block_time']<=pd.Timestamp(str_end_datetime))
    
    ].reset_index(drop=True)

    # 创建两个DataFrame，一个用于'from'列，另一个用于'to'列
    df_from = df_holder[[ 'token_transfer' ,'from_owner']].copy()
    df_from.rename(columns={'from_owner': 'holder_owner' , 'token_transfer':'token_M'}, inplace=True)
    df_from['token_M'] = -df_from['token_M'] /1e6/1e6

    df_to = df_holder[[  	'token_transfer' ,'to_owner']].copy()
    df_to.rename(columns={'to_owner': 'holder_owner','token_transfer':'token_M'}, inplace=True)
    df_to['token_M'] = df_to['token_M']/1e6/1e6

    # 合并两个DataFrame
    df_holder = pd.concat([df_from, df_to], ignore_index=True)
    df_holder = df_holder.groupby('holder_owner')['token_M'].sum().reset_index()
    df_holder['token_M'] = df_holder['token_M'].round(2)
    df_holder.rename(columns={'token_M': f'AM_{str_daterange}'}, inplace=True)
 
    return df_holder  
    
  
def fun():
    global file_transfer_row , merged_df ,  token_add , df_all_holders
    arr_date_range =[]
    arr_col_pct=[]
    df = pd.read_csv(file_transfer_row)
    df = df.drop_duplicates().reset_index(drop=True)
    df = utc_2_shanghaidate(df,utc_time_col='block_time')

    df['block_time'] = pd.to_datetime(df['block_time']  )
    merged_df = df_all_holders[['holder_owner']]
    

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-08 22:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')        
    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-09 01:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-09 04:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-09 07:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-10 10:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-10 21:30:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-11 02:30:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')

    df_daterange  = get_daterange_df(df, str_end_datetime = '2025-01-16 01:00:00')
    df_daterange = df_daterange[df_daterange['holder_owner'].isin(arr_all_holders)].reset_index(drop=True)
    merged_df = pd.merge(merged_df, df_daterange, on='holder_owner', how='outer')



   
    merged_df = pd.merge(merged_df, df_all_holders, on='holder_owner', how='left')

 
    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.to_excel('002_merge_special_add_hold_info.xlsx' ,index=False)

import os
 
 
token_add = '5puhwnyz2Tv8jSmmBD5DSqCwFVXwwPGZacymM7DQpump'
arr_file = os.listdir(os.getcwd())

arr_file = [file for file in arr_file if "dune_speadd_all_transfer_row" in file and file.endswith(".csv")]
file_transfer_row = arr_file[0]

df_all_holders = pd.read_excel('001_all_holders.xlsx')
df_all_holders=df_all_holders.dropna(subset=['holder_owner'])
# df_all_holders = df_all_holders[df_all_holders['domains_owned'].isna()]
# df_all_holders=df_all_holders[['holder_owner']]

arr_all_holders  = df_all_holders['holder_owner'].tolist()
 

print("read file: ",file_transfer_row)
fun()


