import time
import asyncio
 
 
import json
import datetime
import signal
import sys
import math
import requests
 
from apscheduler.schedulers.background import BackgroundScheduler
 
import os
import traceback
import re
from apscheduler.schedulers.asyncio import AsyncIOScheduler

old_print = print
def keep_alnum_forgmgn(string):
    return re.sub(r'[^a-zA-Z0-9\.\s\+\-]', ' ', string)

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


print = timestamped_print
print('\n'*5)

def global_exception_hook(exc_type, exc_value, exc_traceback):
    print(f"Global exception: {exc_type.__name__} - {exc_value}")
    traceback.print_tb(exc_traceback)
    raise
 
 
def term_sig_handler(signum, frame):   
    # save_obj_unraydium_token()
    print(f'catched singal: {signum}')
    
    sys.exit()


def global_exception_hook(exc_type, exc_value, exc_traceback):
    print(f"Global exception: {exc_type.__name__} - {exc_value}")
    traceback.print_tb(exc_traceback)
    raise


int_pattern  = r"^\d+$"
 
 

feishu_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/a68cffbf-9104-427b-8e25-62dfaf363a47'
utc_plus_8 = datetime.timezone(datetime.timedelta(hours=8))


# flux 获取dlao swap info 保存进 obj_queueing_pairaddress 
# 在一个定时器 的函数中 便利读取 obj_queueing_pairaddress ，对每一个元素进行check 如果可以买卖则进行买卖


def send_token_info_to_feishu(token_info):
    print("send_token_info_to_feishu token_info=",token_info)
    return


import pathlib
import shutil

import  pandas as pd
 
set_has_read_file = set()
df_for_feishu = pd.DataFrame()
is_handleing_gmgn_file = False
async def handle_Downloads_gmgn_pump_file_timed():
        
        global set_has_read_file,is_handleing_gmgn_file  ,df_for_feishu

        if is_handleing_gmgn_file==True:
            return
        is_handleing_gmgn_file = True
        
        arr_csv_file = [f for f in os.listdir(
            "/root/Downloads/") if f.endswith(".csv")]
        cur_now_timestamp = int(time.time()*1000)
        print("arr_csv_file=",arr_csv_file)

        for csv_file in arr_csv_file:
            
            handle_csv_file = csv_file.replace(".csv","")
            
        
            if "_gmgn_filter" in handle_csv_file:
                str_timestamp = handle_csv_file.replace("_gmgn_filter","")
            else:
                # print("not _gmgn_filter handle_csv_file=",handle_csv_file)
                
                os.remove(f"/root/Downloads/{csv_file}")
                continue

            if not re.match(int_pattern, str_timestamp):
                # print("not match handle_csv_file=",handle_csv_file)
                os.remove(f"/root/Downloads/{csv_file}")
                continue
            csv_file_timestamp = int(str_timestamp)
            # 24小时以前的文件直接删除
            if cur_now_timestamp - csv_file_timestamp >= 24*60 * 60 * 1000:
                # os.path.exists(f"/root/Downloads/{csv_file}"):
                # print("24小时以前 handle_csv_file=",handle_csv_file)
                os.remove(f"/root/Downloads/{csv_file}")
        #重新获取当前csv文本 
        arr_csv_file = [f for f in os.listdir(
            "/root/Downloads/") if f.endswith(".csv")]
        set_cur_file = set(arr_csv_file)
        
        arr_cur_no_read_file =list (set_cur_file - set_has_read_file)
        if len(arr_cur_no_read_file)==0:
            print("no read_file")
            is_handleing_gmgn_file = False
            return
        df = None
   
        try:
            for csv_file in arr_cur_no_read_file:
                cur_df = pd.read_csv(f"/root/Downloads/{csv_file}",dtype=object)
                str_save_timestamp = csv_file.replace("_gmgn_filter.csv","")
                cur_df["save_timestamp"] = str_save_timestamp
                df =pd.concat([df,cur_df])
         
        except Exception as e:
            print(e)
            print("arr_cur_no_read_file=",arr_cur_no_read_file   )
            is_handleing_gmgn_file = False
            return

        if os.path.exists(f"/home/listen_from_browser/df_gmgn_pump_total.csv" ):
            df.to_csv(f"/home/listen_from_browser/df_gmgn_pump_total.csv",  mode='a',header =False , index = False  )
            
        else:
            df.to_csv(f"/home/listen_from_browser/df_gmgn_pump_total.csv",  index = False  )
            
        df = pd.read_csv(f"/home/listen_from_browser/df_gmgn_pump_total.csv",dtype=object)
        df["save_timestamp"] = df["save_timestamp"].astype(int)
        # 删除 10 天前保存的数据
        df =df[df["save_timestamp"]>=  int(cur_now_timestamp - 10 * 24*60 * 60 * 1000)].reset_index(drop=True)
        df = df.drop_duplicates(subset='token_url', keep='last')
        
        df.to_csv(f"/home/listen_from_browser/df_gmgn_pump_total.csv",  index = False  )
        
        
        df_for_feishu   =df[df["save_timestamp"] ==df["save_timestamp"].max()].reset_index(drop=True)
        df_for_feishu = df_for_feishu[df_for_feishu['age'].str.contains('h', case=False, na=False)].reset_index(drop=True)
        
        df_for_feishu['age'] = df_for_feishu['age'].str.replace('h', '', case=False)
        df_for_feishu["age"] = df_for_feishu["age"].astype(int)
        df_for_feishu = df_for_feishu[df_for_feishu["age"] <= 24].reset_index(drop=True)
       
        df_for_feishu=df_for_feishu[ ['token_url', 'token_name', 'age', 'mc', 'holders' ,'save_timestamp'  ] ]
        

        set_has_read_file = set(arr_csv_file)
        is_handleing_gmgn_file = False
        return


async def send_feishu(title,str_text):

    payload_message = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": title ,
                    "content": [
                        
                    ]
                }
            }
        }
    }

    headers = {
        "Content-Type": "application/json; charset=utf-8",
    }
    content = []
     

    content.append([{
        "tag": "text",
        'text':  str_text
    } 
    ])

    payload_message["content"]["post"]["zh_cn"]["content"] = content
    payload_message["content"] = json.dumps(payload_message["content"])

    payload_message = json.dumps(payload_message)
    response = requests.post(
        url=feishu_url, headers=headers, data=payload_message)
    # response_json = response.json()
    # print("response_json=",response_json)
    return 
     

async def send_cur_files_feishu_timed():
    global set_has_read_file
    arr_has_read_file = list(set_has_read_file)
    str_text =  "\n".join(arr_has_read_file)

    await send_feishu( title = "read files",str_text =str_text)


    return

async def ready_send_feishu_timed():
    global df_for_feishu
 
    str_text_df = df_for_feishu.to_csv(index=False).strip().replace(","," , ")
 
    if str_text_df=="":
        return

    await send_feishu( title= "gmgn 12小时内的pump 信息" , str_text=str_text_df)
    df_for_feishu=pd.DataFrame()
    return


 
obj_global_info ={
    "timedTasks_idx":0
}

sys.excepthook = global_exception_hook
signal.signal(signal.SIGTERM, term_sig_handler)
signal.signal(signal.SIGINT, term_sig_handler)
print(f"Begin Listening for messages containing  in chats ichat names...")
 
 
async def timedTasks():
    global obj_global_info
    obj_global_info["timedTasks_idx"]+=1
    cur_timedTasks_idx =obj_global_info["timedTasks_idx"] 
    if cur_timedTasks_idx <5:
        return

    if cur_timedTasks_idx%7300 == 0:
        obj_global_info["timedTasks_idx"]=10

    if cur_timedTasks_idx%3600==0:
        await handle_Downloads_gmgn_pump_file_timed()

    if cur_timedTasks_idx%(3600*2+9)==0:
        #  一次飞书
        await ready_send_feishu_timed()
   

    if cur_timedTasks_idx%(3600*2+120+9)==0:
    #  一次飞书
        await send_cur_files_feishu_timed()

    return

 
# scheduler = BackgroundScheduler({'apscheduler.timezone': 'UTC'})
scheduler = AsyncIOScheduler({'apscheduler.timezone': 'UTC'})
scheduler.add_job(timedTasks, "interval", seconds=1)   
# scheduler.add_job(func=timedTasks, trigger="cron", seconds=10)   
# '*/1 * * * * *'

scheduler.start()
 
asyncio.get_event_loop().run_forever()
# client_bill.run_until_disconnected()
