Skip to content

Commit

Permalink
[✨Feature] 添加 Pushplus 推送方法 (#647)
Browse files Browse the repository at this point in the history
* 🙈 Update .gitignore

For Nodejs version controlling

* ✨ Add new notify method: pushplus

While email notification ruins my inbox, serverChan provides only 5
messages per day. In this case, pushplus is a great alternative with 200
free message push caps every day.

* ✨ Add new notify method: pushplus

This commit adds error handling, fixed the wrong url written by Copilot.

* 🎨 Improve Pushplus configuration

* 🚨 Makes ruff happy
  • Loading branch information
MuelNova authored Jul 18, 2024
1 parent c081263 commit 47f0e19
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ __pycache__/
*.py[cod]
*$py.class

# Nodejs Version Manager
.nvmrc

# C extensions
*.so

Expand Down
1 change: 1 addition & 0 deletions arknights_mower/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def initialize(tasks, scheduler=None):
"server_push_enable": conf["server_push_enable"],
"sendKey": conf["sendKey"],
},
"pushplus_config": conf["pushplus"],
}
base_scheduler.check_mail_enable = conf["check_mail_enable"]
base_scheduler.report_enable = conf["report_enable"]
Expand Down
3 changes: 3 additions & 0 deletions arknights_mower/templates/conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ run_order_grandet_mode:
back_to_index: false
server_push_enable: false
sendKey: ''
pushplus:
enable: false
token: ''
recruit_gap: 9
recruit_auto_5: 1
recruit_auto_only5: false
Expand Down
76 changes: 76 additions & 0 deletions arknights_mower/utils/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,25 @@ def handle_serverJang_error(self, url, data):
logger.exception(e)
self.csleep(delay)

# PushPlus异常处理
def handle_pushplus_error(self, data):
for delay in self.exponential_backoff():
try:
response = requests.post(r"http://www.pushplus.plus/send", json=data)
json_data = response.json()
if json_data.get("code") == 200:
logger.info("PushPlus通知发送成功")
break
else:
logger.error(
f"PushPlus通知发送失败,错误信息:{json_data.get('msg')}"
)
self.csleep(delay)
except Exception as e:
logger.error("PushPlus通知发送失败")
logger.exception(e)
self.csleep(delay)

def send_message(
self,
body="",
Expand Down Expand Up @@ -857,6 +876,8 @@ def send_message_old(
email_config = send_message_config.get("email_config")
# 获取Server酱配置
serverJang_push_config = send_message_config.get("serverJang_push_config")
# 获取PushPlus配置
pushplus_config = send_message_config.get("pushplus_config")

# 邮件通知部分
if email_config and email_config.get("mail_enable", 0):
Expand Down Expand Up @@ -902,6 +923,53 @@ def send_message_old(
except Exception:
failed_methods.append(("serverJang", url, data))

# PushPlus通知部分
if pushplus_config and pushplus_config.get("enable", False):
token = pushplus_config.get("token")
if not token:
logger.error("PushPlus的token未配置")
return

# img 嵌入 html
# if attach_image is not None:
# img = cv2.cvtColor(attach_image, cv2.COLOR_RGB2BGR)
# _, attachment = cv2.imencode(
# ".jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 75]
# )
# img_base64 = base64.b64encode(attachment)
# img_url = f"data:image/jpeg;base64,{img_base64.decode('utf-8')}"
# img_tag = f'<img src="{img_url}" />'

# # 查找 </body> 标签的位置
# body_close_tag = '</body>'
# insert_position = body.find(body_close_tag)

# # 在 </body> 标签前插入 <img> 标签
# if insert_position != -1:
# body = body[:insert_position] + img_tag + body[insert_position:]

url = r"http://www.pushplus.plus/send"
data = {
"token": token,
"title": "Mower通知",
"content": body,
"template": "markdown",
}

try:
response = requests.post(url, json=data)
json_data = response.json()
if json_data.get("code") == 200:
logger.info("PushPlus通知发送成功")
else:
logger.error(
f"PushPlus通知发送失败,错误信息:{json_data.get('msg')}"
)
except Exception as e:
logger.error("PushPlus通知发送失败")
logger.exception(e)
failed_methods.append(("pushplus", data))

# 处理失败的方法
for method, *args in failed_methods:
if method == "email":
Expand All @@ -918,3 +986,11 @@ def send_message_old(
break
except Exception:
self.csleep(1)

elif method == "pushplus":
for _ in range(retry_times):
try:
self.handle_pushplus_error(*args)
break
except Exception:
self.csleep(1)
24 changes: 24 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,30 @@ def test_serverJang_push():
return "发送失败 : " + str(e)


@app.route("/test-pushplus-push")
@require_token
def test_pushplus_push():
import requests

try:
response = requests.post(
r"http://www.pushplus.plus/send",
params={
"token": config.conf["pushplus"]["token"],
"title": "arknights-mower推送测试",
"content": "arknights-mower推送测试",
},
)

if response.status_code == 200 and response.json().get("code") == 200:
return "发送成功"
else:
return "发送失败 : " + response.json().get("message", "")
except Exception as e:
print(type(e))
return "发送失败 : " + str(e)


@app.route("/check-skland")
@require_token
def test_skland():
Expand Down
1 change: 1 addition & 0 deletions ui/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ declare module 'vue' {
NVirtualList: typeof import('naive-ui')['NVirtualList']
NWatermark: typeof import('naive-ui')['NWatermark']
PlanEditor: typeof import('./src/components/PlanEditor.vue')['default']
PushPlus: typeof import('./src/components/PushPlus.vue')['default']
ReclamationAlgorithm: typeof import('./src/components/ReclamationAlgorithm.vue')['default']
Recruit: typeof import('./src/components/Recruit.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
Expand Down
82 changes: 82 additions & 0 deletions ui/src/components/PushPlus.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<script setup>
import { useConfigStore } from '@/stores/config'
import { storeToRefs } from 'pinia'
import { inject, ref } from 'vue'
const store = useConfigStore()
const axios = inject('axios')
const mobile = inject('mobile')
const testPushResult = ref('')
const { pushplus } = storeToRefs(store)
async function test_push() {
testPushResult.value = '正在发送……'
const response = await axios.get(`${import.meta.env.VITE_HTTP_URL}/test-pushplus-push`)
testPushResult.value = response.data
}
</script>
<template>
<n-card>
<template #header>
<n-checkbox v-model:checked="pushplus.enable" class="serverpush-title">
<div class="card-title-wrapper">
<span class="card-title">PushPlus推送通知</span>
<help-text class="card-title-tip">
什么是PushPlus?参考:
<n-button text tag="a" href="https://www.pushplus.plus" target="_blank" type="primary">
https://www.pushplus.plus
</n-button>
</help-text>
</div>
</n-checkbox>
</template>
<template #default>
<n-form
:label-placement="mobile ? 'top' : 'left'"
:show-feedback="false"
label-width="96"
label-align="left"
>
<n-form-item label="Push Plus Token">
<n-input v-model:value="pushplus.token" show-password-on="click" type="password" />
</n-form-item>
</n-form>
<n-divider v-if="pushplus.enable" />
<div v-if="pushplus.enable" class="push-test mt-16">
<n-button @click="test_push">发送测试通知</n-button>
<div>{{ testPushResult }}</div>
</div>
</template>
</n-card>
</template>
<style scoped>
.card-title-wrapper {
display: flex;
align-items: center;
gap: 8px;
}
.card-title {
font-weight: 500;
font-size: 18px;
}
.serverpush-title {
width: 100%;
}
.push-test {
display: flex;
align-items: center;
gap: 16px;
}
.mt-16 {
margin-top: 16px;
}
</style>
1 change: 1 addition & 0 deletions ui/src/pages/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ async function test_screenshot() {
<div><DailyMission /></div>
<div><email /></div>
<div><ServerJang /></div>
<div><PushPlus /></div>
</div>

<div class="grid-right">
Expand Down
6 changes: 6 additions & 0 deletions ui/src/stores/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const useConfigStore = defineStore('config', () => {
const run_order_grandet_mode = ref({})
const server_push_enable = ref(false) // Server酱通知开关
const sendKey = ref('') // Server酱Key值
const pushplus = ref({})
const check_mail_enable = ref(true)
const report_enable = ref(true)
const send_report = ref(true)
Expand Down Expand Up @@ -156,6 +157,8 @@ export const useConfigStore = defineStore('config', () => {
// 新增:加载Server酱的配置
server_push_enable.value = response.data.server_push_enable != 0
sendKey.value = response.data.sendKey
// 新增:加载PushPlus的配置
pushplus.value = response.data.pushplus
check_mail_enable.value = response.data.check_mail_enable
report_enable.value = response.data.report_enable
send_report.value = response.data.send_report
Expand Down Expand Up @@ -240,6 +243,8 @@ export const useConfigStore = defineStore('config', () => {
// 新增:Server酱的配置
server_push_enable: server_push_enable.value ? 1 : 0,
sendKey: sendKey.value,
// 新增:PushPlus的配置
pushplus: pushplus.value,
check_mail_enable: check_mail_enable.value,
report_enable: report_enable.value,
send_report: send_report.value,
Expand Down Expand Up @@ -334,6 +339,7 @@ export const useConfigStore = defineStore('config', () => {
run_order_grandet_mode,
server_push_enable,
sendKey,
pushplus,
check_mail_enable,
report_enable,
send_report,
Expand Down

0 comments on commit 47f0e19

Please sign in to comment.