本地检测Excel文件中域名到期时间,发送飞书通知
#!/usr/bin/python3 #coding=utf-8 import json import requests import time import datetime import sys import csv #import importlib import re import collections as col #导入的库 from openpyxl import load_workbook #importlib.reload(sys) #打开的文件 class DateEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime.datetime): return obj.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(obj, datetime.date): return obj.strftime("%Y-%m-%d") else: return json.JSONEncoder.default(self, obj) def send_card(): """发送普通消息""" aa = {"域名": a , "平台": p , "到期时间": d.strftime("%Y-%m-%d")} #print(json.dumps(aa, cls=DateEncoder)) #机器人地址,注意修改id url = 'https://open.feishu.cn/open-apis/bot/v2/hook/<id>' headers = {"Content-Type": "text/plain"} true = True data = { "msg_type": "interactive", "card": { "config": {"wide_screen_mode": true,"enable_forward": true}, "elements": [{"tag": "div", "text": {"content": json.dumps(aa, cls=DateEncoder).encode('utf-8').decode('unicode_escape') ,"tag": "lark_md"} }, # {"tag": "div", # "text":{"content": "平台: p" ,"tag": "lark_md"} }, # {"tag": "div", # "text":{"content": d ,"tag": "lark_md"} }, ], "header": {"title": {"content": "域名即将到期提醒","tag": "plain_text"}} } } r = requests.post(url, headers=headers, json=data) return r.text if __name__ == '__main__': aheaders = {'Content-Type': 'application/json'} day_str = datetime.datetime.strptime(datetime.datetime.now().strftime("%Y-%m-%d"), "%Y-%m-%d") #print(day_str) #n_time=datetime.datetime.now() # 获取当前时间 #n_time.strftime('%Y-%m-%d') fp = '/home/ftp/bao/domain1.xlsx' ws = load_workbook(fp) #要读取的工作薄 sheet = ws.get_sheet_by_name("Sheet1") #按行读取 for i in sheet.rows: a = i[0].value if i[0].value != None else '' #如果单元格的值为空,则空串 d = i[1].value if i[1].value != None else '' p = i[2].value if i[2].value != None else '' e = d - day_str if (d - day_str).days <= 30: #构造cypher语句,用于在cypher-shell下执行 #print ('match (n :Source) where n.name = ','"'+ a + '" set n += ', '{ synonym: "'+b + '" , ref: "'+c+'"};') # wm = wechat_monitor() # curl -X POST \ # "https://open.feishu.cn/open-apis/bot/v2/hook/f17c9e97-2d79-4f43-b6df-b0b70cdeb68d" \ # -H 'Content-Type: application/json' \ # -d '{ # "msg_type": "interactive", # "card":{ # "config": # {"wide_screen_mode": true,"enable_forward": true}, # "elements": # [{"tag": "div","text": {"content": a ,"tag": "lark_md"}}], # "header": {"title": {"content": e,"tag": "plain_text"} # } # # } # # }' #asendmessage() #true = True #infoxx() send_card() print (a,d,p) else: print (a,d,p) #class wechat_monitor(): # sendMessageUrl2 = "https://open.feishu.cn/open-apis/bot/v2/hook/f17c9e97-2d79-4f43-b6df-b0b70cdeb68d" #def sendmessage(data): # res2 = requests.post(sendMessageUrl2,headers=aheaders, data = json.dumps(data) ) # return res2 #if __name__ == '__main__': # wm = wechat_monitor() # true = True # data = {"host":sys.argv[1],"title": sys.argv[2],"content": sys.argv[3],"time": sys.argv[4]} #def infoxx(): # data = { # "msg_type": "interactive", # "card": # {"config": # {"wide_screen_mode": true,"enable_forward": true}, # "elements": # [{"tag": "div","text": {"content": a ,"tag": "lark_md"}}], # "header": {"title": {"content": e,"tag": "plain_text"}} # } # } # res2 = wm.sendmessage(data)