From 0b8317fca3a7b861af30f8c74a21b1ca9b3ee81a Mon Sep 17 00:00:00 2001 From: Juhani Naskali Date: Sat, 15 Jul 2023 19:52:00 +0300 Subject: [PATCH 1/4] added whois plugin --- bot/plugin_manager.py | 2 ++ bot/plugins/whois.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 bot/plugins/whois.py diff --git a/bot/plugin_manager.py b/bot/plugin_manager.py index 7faea10..9e60b0a 100644 --- a/bot/plugin_manager.py +++ b/bot/plugin_manager.py @@ -12,6 +12,7 @@ from plugins.ddg_web_search import DDGWebSearchPlugin from plugins.wolfram_alpha import WolframAlphaPlugin from plugins.deepl import DeeplTranslatePlugin from plugins.worldtimeapi import WorldTimeApiPlugin +from plugins.whois import WhoisPlugin class PluginManager: @@ -34,6 +35,7 @@ class PluginManager: 'dice': DicePlugin, 'deepl_translate': DeeplTranslatePlugin, 'gtts_text_to_speech': GTTSTextToSpeech, + 'whois': WhoisPlugin, } self.plugins = [plugin_mapping[plugin]() for plugin in enabled_plugins if plugin in plugin_mapping] diff --git a/bot/plugins/whois.py b/bot/plugins/whois.py new file mode 100644 index 0000000..34c00ef --- /dev/null +++ b/bot/plugins/whois.py @@ -0,0 +1,32 @@ +from typing import Dict +from .plugin import Plugin +import whois + +class WhoisPlugin(Plugin): + """ + A plugin to query whois database + """ + def get_source_name(self) -> str: + return "Whois" + + def get_spec(self) -> [Dict]: + return [{ + "name": "get_whois", + "description": "Get whois registration and expiry information for a domain", + "parameters": { + "type": "object", + "properties": { + "domain": {"type": "string", "description": "Domain name"} + }, + "required": ["domain"], + }, + }] + + async def execute(self, function_name, **kwargs) -> Dict: + try: + whois_result = whois.whois(kwargs['domain']) + return whois_result + except whois.parser.PywhoisError as e: + return {'result': 'No such domain found'} + except Exception as e: + return {'error': 'An unexpected error occurred: ' + str(e)} From eeaaad12584c33045ce016a68a7b5dbcd4d6c180 Mon Sep 17 00:00:00 2001 From: Juhani Naskali Date: Sat, 15 Jul 2023 19:53:09 +0300 Subject: [PATCH 2/4] Fix possible TypeError domain plugin query json sometimes causes 'TypeError: Object of type datetime is not JSON serializable' --- bot/plugin_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/plugin_manager.py b/bot/plugin_manager.py index 9e60b0a..a27652b 100644 --- a/bot/plugin_manager.py +++ b/bot/plugin_manager.py @@ -52,7 +52,7 @@ class PluginManager: plugin = self.__get_plugin_by_function_name(function_name) if not plugin: return json.dumps({'error': f'Function {function_name} not found'}) - return json.dumps(await plugin.execute(function_name, **json.loads(arguments))) + return json.dumps(await plugin.execute(function_name, **json.loads(arguments)), default=str) def get_plugin_source_name(self, function_name) -> str: """ From d1dd1c9e2514fb06fc6ea7c00ee7255febac2ce3 Mon Sep 17 00:00:00 2001 From: Juhani Naskali Date: Sat, 15 Jul 2023 19:54:07 +0300 Subject: [PATCH 3/4] update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d13a52d..e294d92 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ Check out the [official API reference](https://platform.openai.com/docs/api-refe | `youtube_audio_extractor` | Extract audio from YouTube videos | - | `pip install pytube~=15.0.0` | | `deepl_translate` | Translate text to any language (powered by [DeepL](https://deepl.com)) - by [@LedyBacer](https://github.com/LedyBacer) | `DEEPL_API_KEY` | | | `gtts_text_to_speech` | Text to speech (powered by Google Translate APIs) | - | `pip install gtts~=2.3.2` | +| `whois` | Query the whois domain database | - | `pip install whois~=0.9.27` | **Note**: some plugins have additional dependencies that are not listed in the `requirements.txt` file. If you plan on using these plugins, you can install them manually using the command above (see the `Dependency` column). From d64859dabce041f0b8cc99b02e6f6e1aaa25ba7c Mon Sep 17 00:00:00 2001 From: Juhani Naskali Date: Mon, 17 Jul 2023 08:33:33 +0300 Subject: [PATCH 4/4] updated whois calls to use query --- bot/plugins/whois.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/plugins/whois.py b/bot/plugins/whois.py index 34c00ef..417f122 100644 --- a/bot/plugins/whois.py +++ b/bot/plugins/whois.py @@ -24,9 +24,9 @@ class WhoisPlugin(Plugin): async def execute(self, function_name, **kwargs) -> Dict: try: - whois_result = whois.whois(kwargs['domain']) - return whois_result - except whois.parser.PywhoisError as e: - return {'result': 'No such domain found'} + whois_result = whois.query(kwargs['domain']) + if whois_result is None: + return {'result': 'No such domain found'} + return whois_result.__dict__ except Exception as e: return {'error': 'An unexpected error occurred: ' + str(e)}