MihoyoBBSTools/mihoyobbs.py

236 lines
10 KiB
Python
Raw Normal View History

2022-08-18 11:07:35 +00:00
import json
2021-05-23 13:24:20 +00:00
import time
import tools
import config
import random
2022-10-08 07:59:54 +00:00
import captcha
2021-05-23 13:24:20 +00:00
import setting
from request import http
2022-01-06 05:49:25 +00:00
from loghelper import log
from error import CookieError
2022-01-06 03:27:24 +00:00
2022-04-23 01:35:26 +00:00
today_get_coins = 0
today_have_get_coins = 0 # 这个变量以后可能会用上,先留着了
2022-11-03 00:52:31 +00:00
have_coins = 0
2021-06-06 07:27:33 +00:00
2021-10-25 14:53:34 +00:00
2022-04-23 01:35:26 +00:00
class Mihoyobbs:
2021-05-23 13:24:20 +00:00
def __init__(self):
self.headers = {
2022-08-05 04:25:10 +00:00
"DS": tools.get_ds(web=False),
"cookie": f'stuid={config.config["account"]["stuid"]};stoken={config.config["account"]["stoken"]}',
2021-05-23 13:24:20 +00:00
"x-rpc-client_type": setting.mihoyobbs_Client_type,
2022-12-31 04:37:23 +00:00
"x-rpc-app_version": setting.mihoyobbs_version,
2022-08-19 06:23:44 +00:00
"x-rpc-sys_version": "12",
2022-08-18 08:57:37 +00:00
"x-rpc-channel": "miyousheluodi",
2022-01-06 05:49:25 +00:00
"x-rpc-device_id": tools.get_device_id(),
"x-rpc-device_name": tools.random_text(random.randint(1, 10)),
2021-05-23 13:24:20 +00:00
"x-rpc-device_model": "Mi 10",
"Referer": "https://app.mihoyo.com",
"Host": "bbs-api.mihoyo.com",
"User-Agent": "okhttp/4.8.0"
}
self.Task_do = {
"bbs_Sign": False,
"bbs_Read_posts": False,
"bbs_Read_posts_num": 3,
"bbs_Like_posts": False,
"bbs_Like_posts_num": 5,
"bbs_Share": False
}
2022-04-23 01:35:26 +00:00
self.get_tasks_list()
2021-10-25 14:53:34 +00:00
# 如果这三个任务都做了就没必要获取帖子了
if self.Task_do["bbs_Read_posts"] and self.Task_do["bbs_Like_posts"] and self.Task_do["bbs_Share"]:
pass
else:
2022-01-06 05:49:25 +00:00
self.postsList = self.get_list()
2022-04-23 01:35:26 +00:00
def refresh_list(self) -> None:
self.postsList = self.get_list()
2022-10-08 07:59:54 +00:00
def get_pass_challenge(self):
req = http.get(url=setting.bbs_get_captcha, headers=self.headers)
data = req.json()
if data["retcode"] != 0:
return None
validate = captcha.bbs_captcha(data["data"]["gt"], data["data"]["challenge"])
if validate is not None:
check_req = http.post(url=setting.bbs_captcha_verify, headers=self.headers,
json={"geetest_challenge": data["data"]["challenge"],
"geetest_seccode": validate+"|jordan",
"geetest_validate": validate})
check = check_req.json()
if check["retcode"] == 0:
return check["data"]["challenge"]
return None
2021-10-25 14:53:34 +00:00
# 获取任务列表,用来判断做了哪些任务
2022-04-23 01:35:26 +00:00
def get_tasks_list(self):
global today_get_coins
global today_have_get_coins
2022-11-03 00:52:31 +00:00
global have_coins
2022-01-06 05:49:25 +00:00
log.info("正在获取任务列表")
2022-10-12 14:12:30 +00:00
req = http.get(url=setting.bbs_tasks_list, headers=self.headers)
data = req.json()
2022-01-04 00:27:10 +00:00
if "err" in data["message"] or data["retcode"] == -100:
2022-01-06 05:49:25 +00:00
log.error("获取任务列表失败你的cookie可能已过期请重新设置cookie。")
config.clear_cookies()
raise CookieError('Cookie expires')
else:
2022-04-23 01:35:26 +00:00
today_get_coins = data["data"]["can_get_points"]
today_have_get_coins = data["data"]["already_received_points"]
2022-11-03 00:52:31 +00:00
have_coins = data["data"]["total_points"]
2021-10-25 14:53:34 +00:00
# 如果当日可获取米游币数量为0直接判断全部任务都完成了
2022-04-23 01:35:26 +00:00
if today_get_coins == 0:
self.Task_do["bbs_Sign"] = True
self.Task_do["bbs_Read_posts"] = True
self.Task_do["bbs_Like_posts"] = True
self.Task_do["bbs_Share"] = True
else:
2021-10-25 14:53:34 +00:00
# 如果第0个大于或等于62则直接判定任务没做
2021-06-06 13:19:28 +00:00
if data["data"]["states"][0]["mission_id"] >= 62:
2022-04-23 01:35:26 +00:00
log.info(f"新的一天,今天可以获得{today_get_coins}个米游币")
pass
else:
2022-04-23 01:35:26 +00:00
log.info(f"似乎还有任务没完成,今天还能获得{today_get_coins}")
for i in data["data"]["states"]:
2021-10-25 14:53:34 +00:00
# 58是讨论区签到
2021-06-06 13:19:28 +00:00
if i["mission_id"] == 58:
if i["is_get_award"]:
self.Task_do["bbs_Sign"] = True
2021-10-25 14:53:34 +00:00
# 59是看帖子
2021-06-06 13:19:28 +00:00
elif i["mission_id"] == 59:
if i["is_get_award"]:
self.Task_do["bbs_Read_posts"] = True
else:
self.Task_do["bbs_Read_posts_num"] -= i["happened_times"]
2021-10-25 14:53:34 +00:00
# 60是给帖子点赞
2021-06-06 13:19:28 +00:00
elif i["mission_id"] == 60:
if i["is_get_award"]:
self.Task_do["bbs_Like_posts"] = True
else:
self.Task_do["bbs_Like_posts_num"] -= i["happened_times"]
2021-10-25 14:53:34 +00:00
# 61是分享帖子
2021-06-06 13:19:28 +00:00
elif i["mission_id"] == 61:
if i["is_get_award"]:
self.Task_do["bbs_Share"] = True
2021-10-25 14:53:34 +00:00
# 分享帖子,是最后一个任务,到这里了下面都是一次性任务,直接跳出循环
break
2021-06-05 14:44:07 +00:00
2021-10-25 14:53:34 +00:00
# 获取要帖子列表
2022-01-06 05:49:25 +00:00
def get_list(self) -> list:
temp_list = []
log.info("正在获取帖子列表......")
2022-10-12 14:12:30 +00:00
req = http.get(url=setting.bbs_post_list_url.format(setting.mihoyobbs_List_Use[0]["forumId"]),
2022-04-23 01:35:26 +00:00
headers=self.headers)
data = req.json()["data"]["list"]
for n in range(5):
2022-04-23 01:35:26 +00:00
r_l = random.choice(data)
while r_l["post"]["subject"] in str(temp_list):
r_l = random.choice(data)
temp_list.append([r_l["post"]["post_id"], r_l["post"]["subject"]])
# temp_list.append([data["data"]["list"][n]["post"]["post_id"], data["data"]["list"][n]["post"]["subject"]])
2022-01-06 05:49:25 +00:00
log.info("已获取{}个帖子".format(len(temp_list)))
return temp_list
2021-06-05 14:44:07 +00:00
2021-10-25 14:53:34 +00:00
# 进行签到操作
2022-01-06 05:49:25 +00:00
def signing(self):
if self.Task_do["bbs_Sign"]:
2022-01-06 05:49:25 +00:00
log.info("讨论区任务已经完成过了~")
else:
2022-01-06 05:49:25 +00:00
log.info("正在签到......")
2022-08-18 11:07:35 +00:00
header = {}
header.update(self.headers)
2022-10-12 13:54:30 +00:00
challenge = None
2021-07-16 11:44:30 +00:00
for i in setting.mihoyobbs_List_Use:
2022-10-12 13:54:30 +00:00
challenge = None
check_pass = False
for i2 in range(2):
if check_pass:
continue
header["DS"] = tools.get_ds2("", json.dumps({"gids": i["id"]}))
2022-10-12 14:12:30 +00:00
req = http.post(url=setting.bbs_sign_url, json={"gids": i["id"]}, headers=header)
2022-10-12 13:54:30 +00:00
data = req.json()
if data["retcode"] == 1034:
log.warning("社区签到触发验证码")
challenge = self.get_pass_challenge()
if challenge is not None:
header["x-rpc-challenge"] = challenge
elif "err" not in data["message"] and data["retcode"] == 0:
log.info(str(i["name"] + data["message"]))
check_pass = True
if challenge is not None:
challenge = None
header.pop("x-rpc-challenge")
time.sleep(random.randint(2, 8))
else:
log.error("签到失败你的cookie可能已过期请重新设置cookie。")
config.clear_cookies()
raise CookieError('Cookie expires')
2021-10-25 14:53:34 +00:00
# 看帖子
2022-01-06 05:49:25 +00:00
def read_posts(self):
if self.Task_do["bbs_Read_posts"]:
2022-01-06 05:49:25 +00:00
log.info("看帖任务已经完成过了~")
else:
2022-01-06 05:49:25 +00:00
log.info("正在看帖......")
for i in range(self.Task_do["bbs_Read_posts_num"]):
2022-10-12 14:12:30 +00:00
req = http.get(url=setting.bbs_detail_url.format(self.postsList[i][0]), headers=self.headers)
data = req.json()
if data["message"] == "OK":
2022-01-09 06:32:57 +00:00
log.debug("看帖:{} 成功".format(self.postsList[i][1]))
time.sleep(random.randint(2, 8))
2021-10-25 14:53:34 +00:00
# 点赞
2022-04-23 01:35:26 +00:00
def like_posts(self):
2022-10-08 07:59:54 +00:00
header = {}
header.update(self.headers)
challenge = None
if self.Task_do["bbs_Like_posts"]:
2022-01-06 05:49:25 +00:00
log.info("点赞任务已经完成过了~")
else:
2022-01-06 05:49:25 +00:00
log.info("正在点赞......")
for i in range(self.Task_do["bbs_Like_posts_num"]):
2022-10-12 14:12:30 +00:00
req = http.post(url=setting.bbs_like_url, headers=header,
2021-10-25 14:53:34 +00:00
json={"post_id": self.postsList[i][0], "is_cancel": False})
2021-05-23 13:24:20 +00:00
data = req.json()
2021-06-06 13:19:28 +00:00
if data["message"] == "OK":
2022-01-09 06:32:57 +00:00
log.debug("点赞:{} 成功".format(self.postsList[i][1]))
2022-10-08 07:59:54 +00:00
if challenge is not None:
challenge = None
header.pop("x-rpc-challenge")
elif data["retcode"] == 1034:
log.warning("点赞触发验证码")
challenge = self.get_pass_challenge()
if challenge is not None:
header["x-rpc-challenge"] = challenge
# 判断取消点赞是否打开
2022-08-23 15:43:51 +00:00
if config.config["mihoyobbs"]["cancel_like_posts"]:
time.sleep(random.randint(2, 8))
2022-10-12 14:12:30 +00:00
req = http.post(url=setting.bbs_like_url, headers=self.headers,
2021-10-25 14:53:34 +00:00
json={"post_id": self.postsList[i][0], "is_cancel": True})
data = req.json()
2021-06-06 13:19:28 +00:00
if data["message"] == "OK":
2022-01-09 06:32:57 +00:00
log.debug("取消点赞:{} 成功".format(self.postsList[i][1]))
2021-10-25 14:53:34 +00:00
time.sleep(random.randint(2, 8))
# 分享操作
2022-01-06 05:49:25 +00:00
def share_post(self):
if self.Task_do["bbs_Share"]:
2022-01-06 05:49:25 +00:00
log.info("分享任务已经完成过了~")
else:
2022-01-09 06:32:57 +00:00
log.info("正在执行分享任务......")
for i in range(3):
2022-10-12 14:12:30 +00:00
req = http.get(url=setting.bbs_share_url.format(self.postsList[0][0]), headers=self.headers)
2022-01-09 06:32:57 +00:00
data = req.json()
if data["message"] == "OK":
log.debug("分享:{} 成功".format(self.postsList[0][1]))
log.info("分享任务执行成功......")
break
else:
2022-04-23 01:35:26 +00:00
log.debug(f"分享任务执行失败,正在执行第{i + 2}共3次")
2022-01-09 06:32:57 +00:00
time.sleep(random.randint(2, 8))
2021-10-25 14:53:34 +00:00
time.sleep(random.randint(2, 8))