-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
194 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
import enum | ||
NTYPE = enum.Enum('Notification_type', [ | ||
'FROM_CHATROOM', 'FROM_CHAT', 'FROM_KEYWORD']) | ||
CODE = enum.Enum('REDPACKET_CODE', ['SUCCESS', 'LOSED', 'NOT_ME', "ZERO"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# -*- coding: utf-8 -*- | ||
from abc import ABC | ||
import queue | ||
import re | ||
from plyer import notification | ||
|
||
from src.api.config import GLOBAL_CONFIG | ||
from src.api.enum import NTYPE | ||
from src.utils.file import project_root | ||
|
||
|
||
class Event(ABC): | ||
|
||
def __init__(self, type: NTYPE, sender: str, content: str): | ||
self.type = type | ||
self.sender = sender | ||
self.content = content | ||
|
||
def __str__(self): | ||
return f'{self.type} {self.sender}: {self.content}' | ||
|
||
|
||
EVENT_QUEUE = queue.Queue[Event]() | ||
|
||
|
||
def sender(event: Event, *consumers): | ||
for consumer in consumers: | ||
consumer(event) | ||
EVENT_QUEUE.put(event) | ||
|
||
|
||
def flush(): | ||
events: list[Event] = [] | ||
while not EVENT_QUEUE.empty(): | ||
event = EVENT_QUEUE.get() | ||
events.append(event) | ||
EVENT_QUEUE.task_done() | ||
|
||
if events: | ||
# 批量写入文件 | ||
with open("./events.log", "a") as f: | ||
for event in events: | ||
f.write(f"{str(event)}\n") | ||
|
||
|
||
def sys_notification(event: Event): | ||
notification.notify( | ||
title=render_func[event.type](event.sender), | ||
app_name='摸鱼派python客户端', | ||
app_icon=f'{project_root}/icon.ico', | ||
message=f'{event.sender}: {event.content}', | ||
timeout=5 # 通知持续时间(秒) | ||
) | ||
|
||
|
||
render_func = { | ||
NTYPE.FROM_CHATROOM: lambda user: f'{user}在聊天室@你', | ||
NTYPE.FROM_CHAT: lambda user: f'{user}发送了一条私聊信息', | ||
NTYPE.FROM_KEYWORD: lambda _: '关心的消息', | ||
} | ||
|
||
|
||
def put_keyword_to_nitification(args: tuple[str, ...]) -> None: | ||
for keyword in args: | ||
if GLOBAL_CONFIG.chat_config.kw_notification.__contains__(keyword): | ||
print(f'{keyword} 已在加入关键词提醒') | ||
continue | ||
GLOBAL_CONFIG.chat_config.kw_notification.append(keyword) | ||
print(f'{keyword} 已在加入关键词提醒') | ||
if GLOBAL_CONFIG.cfg_path is None: | ||
return | ||
# 持久化到文件 | ||
lines: list[str] = [] | ||
with open(GLOBAL_CONFIG.cfg_path, "r+", encoding='utf-8') as src: | ||
lines = src.readlines() | ||
|
||
for i in range(len(lines)): | ||
lines[i] = re.sub(r'^kw[nN]tification\s*=.*', "kwNtification=" + | ||
str(GLOBAL_CONFIG.chat_config.kw_notification).replace("\'", "\""), lines[i]) | ||
with open(GLOBAL_CONFIG.cfg_path, 'w', encoding='utf-8') as dst: | ||
dst.write("".join(lines)) | ||
|
||
|
||
def remove_keyword_to_nitification(args: tuple[str, ...]) -> None: | ||
for keyword in args: | ||
if GLOBAL_CONFIG.chat_config.kw_notification.__contains__(keyword) == False: | ||
print(f'{keyword} 不在关键词提醒池中') | ||
continue | ||
GLOBAL_CONFIG.chat_config.kw_notification.remove(keyword) | ||
print(f'{keyword} 不再提醒') | ||
if GLOBAL_CONFIG.cfg_path is None: | ||
return | ||
# 持久化到文件 | ||
lines: list[str] = [] | ||
|
||
after: str = '' | ||
if len(GLOBAL_CONFIG.chat_config.kw_notification) == 0: | ||
after = 'kwNtification=[]' | ||
else: | ||
after = "kwNtification=" + \ | ||
str(GLOBAL_CONFIG.chat_config.kw_notification).replace("\'", "\"") | ||
|
||
with open(GLOBAL_CONFIG.cfg_path, "r+", encoding='utf-8') as src: | ||
lines = src.readlines() | ||
|
||
for i in range(len(lines)): | ||
lines[i] = re.sub(r'^kw[nN]tification\s*=.*', after, lines[i]) | ||
with open(GLOBAL_CONFIG.cfg_path, 'w', encoding='utf-8') as dst: | ||
dst.write("".join(lines)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
from pathlib import Path | ||
|
||
|
||
def ensure_directory_exists(file_path): | ||
directory = os.path.dirname(file_path) | ||
|
||
if not os.path.exists(directory): | ||
os.makedirs(directory) | ||
|
||
|
||
# 项目根目录 | ||
project_root = Path(__file__).parent.parent.parent.resolve() |