mirror of
https://github.com/hex-ci/smzdm_script.git
synced 2026-02-03 10:34:33 +08:00
Introduce loguru
This commit is contained in:
67
main.py
67
main.py
@@ -2,33 +2,35 @@ import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from pprint import pp, pprint
|
||||
|
||||
import prettytable as pt
|
||||
import requests
|
||||
from loguru import logger
|
||||
|
||||
from notify.notify import NotifyBot
|
||||
from utils.file_helper import TomlHelper
|
||||
|
||||
CURRENT_PATH = Path(__file__).parent.resolve()
|
||||
CONFIG_PATH = Path(CURRENT_PATH, 'config')
|
||||
CONFIG_PATH = Path(CURRENT_PATH, "config")
|
||||
|
||||
|
||||
class SMZDM_Bot(object):
|
||||
|
||||
DEFAULT_HEADERS = {
|
||||
'Accept': '*/*',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||
'Connection': 'keep-alive',
|
||||
'Host': 'zhiyou.smzdm.com',
|
||||
'Referer': 'https://www.smzdm.com/',
|
||||
'Sec-Fetch-Dest': 'script',
|
||||
'Sec-Fetch-Mode': 'no-cors',
|
||||
'Sec-Fetch-Site': 'same-site',
|
||||
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
|
||||
'AppleWebKit/537.36 (KHTML, like Gecko) '
|
||||
'Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.42'),
|
||||
"Accept": "*/*",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9",
|
||||
"Connection": "keep-alive",
|
||||
"Host": "zhiyou.smzdm.com",
|
||||
"Referer": "https://www.smzdm.com/",
|
||||
"Sec-Fetch-Dest": "script",
|
||||
"Sec-Fetch-Mode": "no-cors",
|
||||
"Sec-Fetch-Site": "same-site",
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54"
|
||||
),
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@@ -39,12 +41,12 @@ class SMZDM_Bot(object):
|
||||
self.session.cookies.update(cookies)
|
||||
|
||||
def set_cookies(self, cookies):
|
||||
self.session.headers['Cookie'] = cookies
|
||||
self.session.headers["Cookie"] = cookies
|
||||
|
||||
def checkin(self):
|
||||
url = 'https://zhiyou.smzdm.com/user/checkin/jsonp_checkin'
|
||||
url = "https://zhiyou.smzdm.com/user/checkin/jsonp_checkin"
|
||||
resp = self.session.get(url)
|
||||
if resp.status_code == 200:
|
||||
if resp.status_code == 200 and resp.json()["error_code"] == 0:
|
||||
resp_data = resp.json()["data"]
|
||||
checkin_num = resp_data["checkin_num"]
|
||||
days_of_week = resp_data["continue_checkin_days"]
|
||||
@@ -54,20 +56,19 @@ class SMZDM_Bot(object):
|
||||
rank = resp_data["rank"]
|
||||
cards = resp_data["cards"]
|
||||
tb = pt.PrettyTable()
|
||||
tb.field_names = ["签到天数", "星期", "金币", "积分", "经验", "等级", "补签卡"]
|
||||
tb.add_row([checkin_num, days_of_week,
|
||||
gold, point, exp, rank, cards])
|
||||
pprint(tb)
|
||||
msg = f'''⭐签到成功{checkin_num}天
|
||||
tb.field_names = ["签到天数", "连续签到", "金币", "积分", "经验", "等级", "补签卡"]
|
||||
tb.add_row([checkin_num, days_of_week, gold, point, exp, rank, cards])
|
||||
logger.info(f"\n{tb}")
|
||||
msg = f"""⭐签到成功{checkin_num}天
|
||||
🏅金币{gold}
|
||||
🏅积分{point}
|
||||
🏅经验{exp}
|
||||
🏅等级{rank}
|
||||
🏅补签卡{cards}'''
|
||||
🏅补签卡{cards}"""
|
||||
return msg
|
||||
else:
|
||||
pprint("Faile to sign in")
|
||||
sys.exit(1)
|
||||
logger.error("Faile to sign in")
|
||||
msg = "签到失败,请从浏览器手动签到一次,并更新cookies"
|
||||
|
||||
|
||||
def main():
|
||||
@@ -75,13 +76,12 @@ def main():
|
||||
conf_kwargs = {}
|
||||
|
||||
if Path.exists(Path(CONFIG_PATH, "config.toml")):
|
||||
pprint("Get configration from config.toml")
|
||||
logger.info("Get configration from config.toml")
|
||||
conf_kwargs = TomlHelper(Path(CONFIG_PATH, "config.toml")).read()
|
||||
SMZDM_COOKIE = conf_kwargs.get(
|
||||
"SMZDM_COOKIE").encode('UTF-8').decode('latin-1')
|
||||
SMZDM_COOKIE = conf_kwargs.get("SMZDM_COOKIE").encode("UTF-8").decode("latin-1")
|
||||
smzdm_bot.set_cookies(SMZDM_COOKIE)
|
||||
elif os.environ.get("SMZDM_COOKIE", None):
|
||||
pprint("Get configration from env")
|
||||
logger.info("Get configration from env")
|
||||
conf_kwargs = {
|
||||
"SMZDM_COOKIE": os.environ.get("SMZDM_COOKIE"),
|
||||
"PUSH_PLUS_TOKEN": os.environ.get("PUSH_PLUS_TOKEN", None),
|
||||
@@ -90,11 +90,10 @@ def main():
|
||||
"TG_USER_ID": os.environ.get("TG_USER_ID", None),
|
||||
"TG_BOT_API": os.environ.get("TG_BOT_API", None),
|
||||
}
|
||||
SMZDM_COOKIE = conf_kwargs.get(
|
||||
"SMZDM_COOKIE").encode('UTF-8').decode('latin-1')
|
||||
SMZDM_COOKIE = conf_kwargs.get("SMZDM_COOKIE").encode("UTF-8").decode("latin-1")
|
||||
smzdm_bot.set_cookies(SMZDM_COOKIE)
|
||||
elif Path.exists(Path(CONFIG_PATH, "cookies.json")):
|
||||
pprint("Load cookis from cookies.json")
|
||||
logger.info("Load cookis from cookies.json")
|
||||
with open(Path(CONFIG_PATH, "cookies.json", "r")) as f:
|
||||
cookies = json.load(f)
|
||||
smzdm_cookies = {}
|
||||
@@ -102,11 +101,11 @@ def main():
|
||||
smzdm_cookies.update({cookie["name"]: cookie["value"]})
|
||||
smzdm_bot.update_cookies(smzdm_cookies)
|
||||
else:
|
||||
pprint("Fail to get SMZDM_COOKIE, exit")
|
||||
logger.info("Fail to get SMZDM_COOKIE, exit")
|
||||
sys.exit(1)
|
||||
msg = smzdm_bot.checkin()
|
||||
NotifyBot(content=msg, **conf_kwargs)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user