Skip to content

Commit

Permalink
support os
Browse files Browse the repository at this point in the history
  • Loading branch information
Womsxd committed May 9, 2023
1 parent ca31495 commit d1a90be
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 69 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var cookie=document.cookie;var ask=confirm('Cookie:'+cookie+'\n\nDo you want to
```javascript
document.cookie
```
4. 从`ltoken=....`开始复制到结尾
5. 将获取到的 Cookie 粘贴到之前获取到 OS 的 Cookie 里面
Expand Down Expand Up @@ -226,13 +227,13 @@ docker-compose pull && docker-compose up -d
定时类型:crontab
定时规则:2 2 28 * *
白名单:ql_main.py
依赖文件:error|mihoyo|genshin|honkai3rd|log|push|req|set|tools|con|acc|honkai2|tearsofthemis|captcha|main|gamecheckin|honkaisr
依赖文件:error|mihoyo|genshin|honkai3rd|log|push|req|set|tools|con|acc|honkai2|tearsofthemis|captcha|main|gamecheckin|honkaisr|hoyo_checkin|hoyo_gs|hoyo_http|hoyo_sr
```
方式 2:指令拉取
```sh
ql repo https://github.com/Womsxd/MihoyoBBSTools.git "ql_main.py" "" "error|mihoyo|genshin|honkai3rd|log|push|req|set|tools|con|acc|honkai2|tearsofthemis|captcha|main|gamecheckin|honkaisr"
ql repo https://github.com/Womsxd/MihoyoBBSTools.git "ql_main.py" "" "error|mihoyo|genshin|honkai3rd|log|push|req|set|tools|con|acc|honkai2|tearsofthemis|captcha|main|gamecheckin|honkaisr|hoyo_checkin|hoyo_gs|hoyo_http|hoyo_sr"
```
### 2.环境变量添加
Expand Down
22 changes: 19 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
update_config_need = False

config = {
'enable': True, 'version': 8,
'enable': True, 'version': 9,
'account': {
'cookie': '',
'login_ticket': '',
Expand All @@ -33,7 +33,8 @@
},
'os': {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []}
'genshin': {'auto_checkin': False, 'black_list': []},
'honkai_sr': {'auto_checkin': False, 'black_list': []}
}
},
'cloud_games': {
Expand Down Expand Up @@ -83,19 +84,34 @@ def config_v8_update(data: dict):
return returns


def config_v9_update(data: dict):
global update_config_need
update_config_need = True
data['version'] = 9
data['games']['os'] = {
'enable': False, 'cookie': '',
'genshin': {'auto_checkin': False, 'black_list': []},
'honkai_sr': {'auto_checkin': False, 'black_list': []}
}
log.info("config已升级到: 9")
return data


def load_config(p_path=None):
global config
if not p_path:
p_path = config_Path
with open(p_path, "r", encoding='utf-8') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
if data['version'] == 8:
if data['version'] == 9:
config = data
else:
if data['version'] == 6:
data = config_v7_update(data)
if data['version'] == 7:
config = config_v8_update(data)
if data['version'] == 8:
config = config_v9_update(data)
save_config()
log.info("Config加载完毕")
return config
Expand Down
6 changes: 3 additions & 3 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
enable: true #这里控制整个config文件是否启用
version: 8 #config的版本
version: 9 #config的版本
account:
cookie: "" #登入账号只需要修改这里就行了,下面会自动获取
login_ticket: ""
Expand Down Expand Up @@ -33,7 +33,7 @@ games:
tears_of_themis: #未定事件簿
auto_checkin: false
black_list: []
honkai_sr: # 铁道
honkai_sr: # 崩坏: 星穹铁道
auto_checkin: false
black_list: []
os: #外服控制区域
Expand All @@ -42,7 +42,7 @@ games:
genshin:
auto_checkin: false
black_list: []
starrail:
honkai_sr: # 崩坏: 星穹铁道
auto_checkin: false
black_list: []
cloud_games: #云游戏专区
Expand Down
43 changes: 20 additions & 23 deletions hoyo_checkin.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import json
import random
import time
import logging
from loghelper import log
import setting

from hoyo_http import http_get_json, http_post_json


RET_CODE_ALREADY_SIGNED_IN = -5003


def hoyo_checkin(
event_base_url: str,
act_id: str,
cookie_str: str
event_base_url: str,
act_id: str,
cookie_str: str
):

lang = 'zh-cn'
referer_url = "https://act.hoyolab.com/"
reward_url = f"{event_base_url}/home?lang={lang}" \
reward_url = f"{event_base_url}/home?lang={setting.os_lang}" \
f"&act_id={act_id}"
info_url = f"{event_base_url}/info?lang={lang}" \
info_url = f"{event_base_url}/info?lang={setting.os_lang}" \
f"&act_id={act_id}"
sign_url = f"{event_base_url}/sign?lang={lang}"
sign_url = f"{event_base_url}/sign?lang={setting.os_lang}"

headers = {
"Referer": referer_url,
"Referer": setting.os_referer_url,
"Accept-Encoding": "gzip, deflate, br",
"Cookie": cookie_str,
}
Expand All @@ -37,24 +34,24 @@ def hoyo_checkin(
first_bind = info_list.get("data", {}).get("first_bind")

if already_signed_in:
logging.info("今天已经签到过")
log.info("今天已经签到过")
ret_msg = "今天已经签到过"
return ret_msg

if first_bind:
logging.info("请手动签到一次")
log.info("请手动签到一次")
ret_msg = "请手动签到一次"
return ret_msg

awards_data = http_get_json(reward_url)

awards = awards_data.get("data", {}).get("awards")

logging.info(f"准备签到: {today} ")
log.info(f"准备签到: {today} ")

# a normal human can't instantly click, so we wait a bit
sleep_time = random.uniform(2.0, 10.0)
logging.debug(f"等待 {sleep_time}")
log.debug(f"等待 {sleep_time}")
time.sleep(sleep_time)

request_data = json.dumps({
Expand All @@ -65,22 +62,22 @@ def hoyo_checkin(

code = response.get("retcode", 99999)

logging.debug(f"return code {code}")
log.debug(f"return code {code}")

if code == RET_CODE_ALREADY_SIGNED_IN:
logging.info("今天已经签到过")
log.info("今天已经签到过")
ret_msg = "今天已经签到过"
return ret_msg
elif code != 0:
logging.error(response['message'])
log.error(response['message'])
ret_msg = response['message']
return ret_msg

reward = awards[total_sign_in_day - 1]

logging.info("签到成功")
logging.info(f"\t已连续签到{total_sign_in_day + 1}天")
logging.info(f"\t今天获得的奖励是: {reward['cnt']}x {reward['name']}")
log.info("签到成功")
log.info(f"\t已连续签到{total_sign_in_day + 1}天")
log.info(f"\t今天获得的奖励是: {reward['cnt']}x {reward['name']}")
ret_msg = f"\t今天获得的奖励是: {reward['cnt']}x {reward['name']}"
return ret_msg
# logging.info(f"\tMessage: {response['message']}")
# logging.info(f"\tMessage: {response['message']}")
39 changes: 16 additions & 23 deletions hoyo_http.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import json
import logging
import tools
import request
from typing import Dict
from loghelper import log

import requests
from requests import HTTPError, Response


def http_get(url: str, max_retries: int = 2, **kwargs) -> Response:
def http_get(url: str, max_retries: int = 2, **kwargs):
return http_request("get", url, max_retries, **kwargs)


def http_get_json(url: str, max_retries: int = 2, **kwargs) -> Dict[str, any]:
resp = http_get(url, max_retries, **kwargs)
data = resp.text
return json.loads(data)
return resp.json()


def http_post(url: str, max_retries: int = 2, **kwargs) -> Response:
def http_post(url: str, max_retries: int = 2, **kwargs):
return http_request("post", url, max_retries, **kwargs)


def http_post_json(url: str, max_retries: int = 2, **kwargs) -> Dict[str, any]:
resp = http_post(url, max_retries, **kwargs)
data = resp.text
return json.loads(data)
return resp.json()


def http_request(
method: str,
url: str,
max_retries: int = 2,
**kwargs
) -> Response:
method: str,
url: str,
max_retries: int = 2,
**kwargs
):
for i in range(max_retries + 1):
try:
logging.debug(f"{method.upper()} {url}, REQ: {i+1}/{max_retries}")
session = requests.Session()
log.debug(f"{method.upper()} {url}, REQ: {i + 1}/{max_retries}")
session = request.get_new_session()
session.headers["USER_AGENT"] = tools.get_useragent()
resp = session.request(method, url, **kwargs)
logging.debug(f"Response: {resp.status_code}\n\n{resp.text}\n")
except HTTPError as e:
logging.error(f"HTTP error: {e}, REQ: {i+1}/{max_retries}")
log.debug(f"Response: {resp.status_code}\n\n{resp.text}\n")
except KeyError as e:
logging.error(f"Wrong response: {e}, REQ: {i+1}/{max_retries}")
log.error(f"Wrong response: {e}, REQ: {i + 1}/{max_retries}")
except Exception as e:
logging.error(f"Unknown error: {e}, REQ: {i+1}/{max_retries}")
log.error(f"HTTP /Unknown error: {e}, REQ: {i + 1}/{max_retries}")
else:
return resp
raise Exception(f"所有HTTP请求失败,请检查网络")
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def main():
log.info("正在进行原神签到")
data = hoyo_gs.run()
return_data += "\n\n" + data
if config.config['games']['os']['starrail']["auto_checkin"]:
if config.config['games']['os']['honkai_sr']["auto_checkin"]:
log.info("正在进行崩坏:星穹铁道签到")
data = hoyo_sr.run()
return_data += "\n\n" + data
Expand Down
33 changes: 19 additions & 14 deletions request.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
try:
# 优先使用httpx,在httpx无法使用的环境下使用requests
import httpx
def get_new_session():
try:
# 优先使用httpx,在httpx无法使用的环境下使用requests
import httpx

http = httpx.Client(timeout=20, transport=httpx.HTTPTransport(retries=10))
# 当openssl版本小于1.0.2的时候直接进行一个空请求让httpx报错
import tools
http = httpx.Client(timeout=20, transport=httpx.HTTPTransport(retries=10))
# 当openssl版本小于1.0.2的时候直接进行一个空请求让httpx报错
import tools

if tools.get_openssl_version() <= 102:
httpx.get()
except (TypeError, ModuleNotFoundError):
import requests
from requests.adapters import HTTPAdapter
if tools.get_openssl_version() <= 102:
httpx.get()
except (TypeError, ModuleNotFoundError):
import requests
from requests.adapters import HTTPAdapter

http = requests.Session()
http.mount('http://', HTTPAdapter(max_retries=10))
http.mount('https://', HTTPAdapter(max_retries=10))
http = requests.Session()
http.mount('http://', HTTPAdapter(max_retries=10))
http.mount('https://', HTTPAdapter(max_retries=10))
return http


http = get_new_session()
5 changes: 5 additions & 0 deletions setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,8 @@
# 云原神相关api
cloud_genshin_Api = "https://api-cloudgame.mihoyo.com"
cloud_genshin_sgin = cloud_genshin_Api + "/hk4e_cg_cn/wallet/wallet/get"

# 接下来是国际服的内容

os_lang = 'zh-cn'
os_referer_url = "https://act.hoyolab.com/"

0 comments on commit d1a90be

Please sign in to comment.