forked from enwaiax/smzdm_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (106 loc) · 4.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import json
import os
import sys
from pathlib import Path
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")
MANUAL_ERR_MSG = "签到失败,请从浏览器手动签到一次,并更新cookies"
#USER_AGENT = (
# "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) "
# "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 "
# "Mobile/15E148 Safari/604.1 Edg/108.0.0.0"
#)
USER_AGENT = ("smzdm 10.4.20 rv:134.2 (iPhone 11; iOS 16.2; zh_CN)/iphone_smzdmapp/10.4.20")
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": USER_AGENT,
}
def __init__(self):
self.session = requests.Session()
self.session.headers = self.DEFAULT_HEADERS
def update_cookies(self, cookies):
self.session.cookies.update(cookies)
def set_cookies(self, cookies):
self.session.headers["Cookie"] = cookies
def checkin(self):
url = "https://zhiyou.smzdm.com/user/checkin/jsonp_checkin"
resp = self.session.get(url)
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"]
gold = resp_data["gold"]
point = resp_data["point"]
exp = resp_data["exp"]
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])
logger.info(f"\n{tb}")
msg = f"""⭐签到成功{checkin_num}天
🏅金币{gold}
🏅积分{point}
🏅经验{exp}
🏅等级{rank}
🏅补签卡{cards}"""
return msg
else:
logger.error("Faile to sign in")
msg = MANUAL_ERR_MSG
return msg
def main():
smzdm_bot = SMZDM_Bot()
conf_kwargs = {}
if Path.exists(Path(CONFIG_PATH, "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_bot.set_cookies(SMZDM_COOKIE)
elif os.environ.get("SMZDM_COOKIE", None):
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),
"SC_KEY": os.environ.get("SC_KEY", None),
"TG_BOT_TOKEN": os.environ.get("TG_BOT_TOKEN", None),
"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_bot.set_cookies(SMZDM_COOKIE)
elif Path.exists(Path(CONFIG_PATH, "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 = {}
for cookie in cookies:
smzdm_cookies.update({cookie["name"]: cookie["value"]})
smzdm_bot.update_cookies(smzdm_cookies)
else:
logger.info("Fail to get SMZDM_COOKIE, exit")
sys.exit(1)
msg = smzdm_bot.checkin()
NotifyBot(content=msg, **conf_kwargs)
if msg == MANUAL_ERR_MSG:
logger.error("Fail the Github action job")
sys.exit(1)
if __name__ == "__main__":
main()