From 92fa74553341d4cdac134b0cafdaf16fa94b3afa Mon Sep 17 00:00:00 2001 From: Qetesh <4559341+Qetesh@users.noreply.github.com> Date: Sat, 12 Oct 2024 01:08:06 +0800 Subject: [PATCH] support Deny-/allow list wildcard url --- common/logger.py | 10 ++++ core/__init__.py | 0 core/entry_filter.py | 31 +++++++++++ main.py | 24 ++------- tests/test_filter.py | 125 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 common/logger.py create mode 100644 core/__init__.py create mode 100644 core/entry_filter.py create mode 100644 tests/test_filter.py diff --git a/common/logger.py b/common/logger.py new file mode 100644 index 0000000..cd05a28 --- /dev/null +++ b/common/logger.py @@ -0,0 +1,10 @@ +import logging +from yaml import safe_load + +config = safe_load(open('config.yml', encoding='utf8')) +logger = logging.getLogger(__name__) +logger.setLevel(config.get('log_level', 'INFO')) +formatter = logging.Formatter('%(asctime)s - %(filename)s - %(lineno)d - %(levelname)s - %(message)s') +console = logging.StreamHandler() +console.setFormatter(formatter) +logger.addHandler(console) diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/entry_filter.py b/core/entry_filter.py new file mode 100644 index 0000000..62ab9f5 --- /dev/null +++ b/core/entry_filter.py @@ -0,0 +1,31 @@ +import fnmatch + +def filter_entry(config, agent, entry): + start_with_list = [name[1]['title'] for name in config['agents'].items()] + style_block = [name[1]['style_block'] for name in config['agents'].items()] + [start_with_list.append('\n' diff --git a/tests/test_filter.py b/tests/test_filter.py new file mode 100644 index 0000000..5aa4add --- /dev/null +++ b/tests/test_filter.py @@ -0,0 +1,125 @@ +import unittest +from yaml import safe_load +from core.entry_filter import filter_entry + +test_config = ''' +{ + "test_style_block": { + "agents": { + "test": { + "title": "🌐AI 翻译", + "style_block": true, + "allow_list": , + "deny_list": + } + } + }, + "test_allow_list": { + "agents": { + "test": { + "title": "🌐AI 翻译", + "style_block": false, + "allow_list": [ + "https://9to5mac.com/", + "https://home.kpmg/*" + ], + "deny_list": + } + } + }, + "test_deny_list": { + "agents": { + "test": { + "title": "🌐AI 翻译", + "style_block": false, + "allow_list": , + "deny_list": [ + "https://9to5mac.com/", + "https://home.kpmg/cn/zh/home/insights.html" + ] + } + } + }, + "test_None": { + "agents": { + "test": { + "title": "🌐AI 翻译", + "style_block": false, + "allow_list": , + "deny_list": + } + } + } +} +''' + +test_entries = ''' +{ + "test_style_block": + { + "entry": + { + "content": '