diff options
| author | Mistivia <i@mistivia.com> | 2025-11-16 15:13:31 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-16 15:13:31 +0800 |
| commit | 1bb8abca1549dba9c0c5660e9b3efa81ccb9d781 (patch) | |
| tree | 521903ed909d7f2686d8ce37e1d5c564237bfc7f /ircbot/main.py | |
| parent | 779d76f20c983f4cda0c8912edb8cf7b156b9f09 (diff) | |
update
Diffstat (limited to 'ircbot/main.py')
| -rw-r--r-- | ircbot/main.py | 87 |
1 files changed, 15 insertions, 72 deletions
diff --git a/ircbot/main.py b/ircbot/main.py index 096dec8..c260839 100644 --- a/ircbot/main.py +++ b/ircbot/main.py @@ -7,9 +7,7 @@ import os import datetime import urllib.parse import hashlib -import re -import requests -import html +from urltitle import url_titles connect_time = time.time() @@ -114,6 +112,8 @@ def save_lastseen(): # with open("lastseen", "w") as fp: # json.dump(LAST_SEEN, fp) +NO_GREETINGS = set(config['no_greeting_nicks']) + def get_greeting(chan, sender, args): if time.time() - connect_time < 300: return '' @@ -121,7 +121,7 @@ def get_greeting(chan, sender, args): return '' if not chan in GREETINGS: return '' - if sender in config['no_greeting_nicks']: + if sender in NO_GREETINGS: return '' if chan in LAST_SEEN \ and sender in LAST_SEEN[chan] \ @@ -221,27 +221,17 @@ def choice_command(chan, sender, args): def dice_command(chan, sender, args): if not args: return "请指定要掷的骰子。用法: dice [数量]d[面数]" - - try: - if args[0][0] == 'd': - num_dice = 1 - num_sides = int(args[0][1:]) - else: - num_dice_str, num_sides_str = args[0].split('d') - num_dice = int(num_dice_str) - num_sides = int(num_sides_str) - except ValueError: - return "无效的骰子格式。请使用 [数量]d[面数] 的格式。" - - if num_dice <= 0 or num_sides <= 1: - return "骰子数量必须大于0,面数必须大于1。" - - results = [] - for _ in range(num_dice): - results.append(random.randint(1, num_sides)) - - total = sum(results) - return f"{total}" + query = " ".join(args) + command = ['/usr/bin/python3', '/opt/dice/main.py'] + process = subprocess.Popen( + command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + stdout, stderr = process.communicate(input=query) + return stdout @command("roll") def roll_command(chan, sender, args): @@ -249,53 +239,6 @@ def roll_command(chan, sender, args): # ======================================================================== -url_pattern = re.compile( - r'(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%?=~_|])', - re.IGNORECASE -) - -title_regex = re.compile( - r'<title[^>]*>([^>]*?)</title>', - re.IGNORECASE | re.DOTALL -) - -def url_titles(text): - found_urls = url_pattern.findall(text) - unique_urls = [] - if len(found_urls) > 0: - url = found_urls[0] - unique_urls = [url] - results = [] - MAX_CONTENT_SIZE = 5 * 1024 * 1024 - for url in unique_urls: - try: - with requests.get( - url, - timeout=10, - allow_redirects=True, - stream=True - ) as response: - if response.status_code != requests.codes.ok: - continue - content_type = response.headers.get('Content-Type', '').lower() - if 'text/html' not in content_type: - continue - content = b'' - for chunk in response.iter_content(chunk_size=8192): - content += chunk - if len(content) >= MAX_CONTENT_SIZE: - continue - encoding = 'utf-8' - html_doc = content.decode(encoding) - match = title_regex.search(html_doc) - if match: - title_content = match.group(1).strip() - if title_content: - results.append(html.unescape(title_content)) - except Exception as e: - print(e) - return results - def cut_string(text, chunk_size=420): chunks = [] current_chunk = [] |
