dailycheckin/index.py

130 lines
4.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import json
2020-12-10 12:24:40 +08:00
import os
2020-12-12 08:42:52 +08:00
import sys
2020-12-07 13:29:36 +08:00
import time
from datetime import datetime, timedelta
2020-12-07 13:29:36 +08:00
2020-12-21 21:08:22 +08:00
from motto import Motto
2021-01-11 18:05:04 +08:00
from utils.config import checkin_map, get_checkin_info
2021-01-19 11:12:25 +08:00
from utils.message import (
message2bark,
message2coolpush,
message2dingtalk,
message2qmsg,
message2server,
message2telegram,
)
2020-12-07 13:29:36 +08:00
def main_handler(event, context):
2020-12-10 12:34:33 +08:00
start_time = time.time()
utc_time = datetime.utcnow() + timedelta(hours=8)
2020-12-10 12:24:40 +08:00
if "IS_GITHUB_ACTION" in os.environ:
message = os.getenv("ONLY_MESSAGE")
dingtalk_secret = os.getenv("DINGTALK_SECRET")
dingtalk_access_token = os.getenv("DINGTALK_ACCESS_TOKEN")
2021-01-19 11:12:25 +08:00
bark_url = os.getenv("BARK_URL")
sckey = os.getenv("SCKEY")
tg_bot_token = os.getenv("TG_BOT_TOKEN")
tg_user_id = os.getenv("TG_USER_ID")
qmsg_key = os.getenv("QMSG_KEY")
coolpushskey = os.getenv("COOLPUSHSKEY")
coolpushqq = os.getenv("COOLPUSHQQ")
coolpushwx = os.getenv("COOLPUSHWX")
coolpushemail = os.getenv("COOLPUSHEMAIL")
motto = os.getenv("MOTTO")
2021-01-10 10:13:19 +08:00
check_info = get_checkin_info(data=None)
else:
2020-12-10 12:24:40 +08:00
if isinstance(event, dict):
message = event.get("Message")
else:
message = None
2021-01-12 07:03:34 +08:00
try:
with open(os.path.join(os.path.dirname(__file__), "config.json"), "r", encoding="utf-8") as f:
data = json.loads(f.read())
dingtalk_secret = data.get("DINGTALK_SECRET")
dingtalk_access_token = data.get("DINGTALK_ACCESS_TOKEN")
2021-01-19 11:12:25 +08:00
bark_url = data.get("BARK_URL")
2021-01-12 07:03:34 +08:00
sckey = data.get("SCKEY")
qmsg_key = data.get("QMSG_KEY")
tg_bot_token = data.get("TG_BOT_TOKEN")
tg_user_id = data.get("TG_USER_ID")
coolpushskey = data.get("COOLPUSHSKEY")
coolpushqq = data.get("COOLPUSHQQ")
coolpushwx = data.get("COOLPUSHWX")
coolpushemail = data.get("COOLPUSHEMAIL")
motto = data.get("MOTTO")
check_info = get_checkin_info(data=data)
except Exception as e:
raise e
2020-12-10 12:45:24 +08:00
content_list = [f"当前时间: {utc_time}"]
2020-12-10 17:08:36 +08:00
if message == "xmly":
2021-01-10 10:13:19 +08:00
if check_info.get("xmly_cookie_list"):
2021-01-11 18:05:04 +08:00
msg_list = checkin_map.get("XMLY_COOKIE_LIST")(xmly_cookie_list=check_info.get("xmly_cookie_list")).main()
2020-12-10 17:08:36 +08:00
content_list += msg_list
2021-01-07 23:22:23 +08:00
elif message == "qqread":
return
2021-01-07 17:05:58 +08:00
2020-12-10 17:08:36 +08:00
else:
2021-01-10 10:13:19 +08:00
for one_check, check_func in checkin_map.items():
2021-01-11 18:05:04 +08:00
if one_check not in ["XMLY_COOKIE_LIST"]:
try:
msg_list = check_func(check_info.get(one_check.lower())).main()
except Exception as e:
print(e)
msg_list = []
content_list += msg_list
if motto:
2021-01-10 10:13:19 +08:00
try:
msg_list = Motto().main()
except Exception as e:
print(e)
msg_list = []
content_list += msg_list
2020-12-07 14:28:00 +08:00
use_time_info = f"本次任务使用时间: {time.time() - start_time}"
2020-12-07 13:29:36 +08:00
content_list.append(use_time_info)
2020-12-07 14:28:00 +08:00
content = "\n-----------------------------\n\n".join(content_list)
2020-12-07 13:29:36 +08:00
print(content)
if message == "xmly":
if utc_time.hour in [9, 18] and utc_time.minute == 0:
flag = True
else:
flag = False
else:
flag = True
if flag:
if dingtalk_access_token and dingtalk_secret:
2021-01-10 10:13:19 +08:00
message2dingtalk(
dingtalk_secret=dingtalk_secret, dingtalk_access_token=dingtalk_access_token, content=content
)
if sckey:
2021-01-10 10:13:19 +08:00
message2server(sckey=sckey, content=content)
if qmsg_key:
for content in content_list:
2021-01-10 10:13:19 +08:00
message2qmsg(qmsg_key=qmsg_key, content=content)
if tg_user_id and tg_bot_token:
2021-01-10 10:13:19 +08:00
message2telegram(tg_user_id=tg_user_id, tg_bot_token=tg_bot_token, content=content)
if coolpushskey:
for content in content_list:
2021-01-10 10:13:19 +08:00
message2coolpush(
coolpushskey=coolpushskey,
content=content,
coolpushqq=coolpushqq,
coolpushwx=coolpushwx,
coolpushemail=coolpushemail,
)
2021-01-19 11:12:25 +08:00
if bark_url:
message2bark(bark_url=bark_url, content=content)
return
2020-12-07 13:29:36 +08:00
if __name__ == "__main__":
2020-12-12 08:42:52 +08:00
args = sys.argv
if len(args) > 1:
event = {"Message": args[1]}
2020-12-12 08:42:52 +08:00
else:
event = None
main_handler(event=event, context=None)