diff --git a/bot.py b/bot.py index 55fa4e27c..cb802acc4 100644 --- a/bot.py +++ b/bot.py @@ -11,7 +11,7 @@ from datetime import datetime from pyrate_limiter import Duration, Limiter, RequestRate -from utils import Color +from utils import * __all__ = ["feishuBot", "wecomBot", "dingtalkBot", "qqBot", "telegramBot", "mailBot"] today = datetime.now().strftime("%Y-%m-%d") @@ -46,9 +46,9 @@ def send(self, text_list: list): r = requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy) if r.status_code == 200: - Color.print_success('[+] feishuBot 发送成功') + console.print('[+] feishuBot 发送成功', style='bold green') else: - Color.print_failed('[-] feishuBot 发送失败') + console.print('[-] feishuBot 发送失败', style='bold red') print(r.text) def send_markdown(self, text): @@ -88,9 +88,9 @@ def send(self, text_list: list): r = requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy) if r.status_code == 200: - Color.print_success('[+] wecomBot 发送成功') + console.print('[+] wecomBot 发送成功', style='bold green') else: - Color.print_failed('[-] wecomBot 发送失败') + console.print('[-] wecomBot 发送失败', style='bold red') print(r.text) @@ -123,9 +123,9 @@ def send(self, text_list: list): r = requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy) if r.status_code == 200: - Color.print_success('[+] dingtalkBot 发送成功') + console.print('[+] dingtalkBot 发送成功', style='bold green') else: - Color.print_failed('[-] dingtalkBot 发送失败') + console.print('[-] dingtalkBot 发送失败', style='bold red') print(r.text) @@ -160,11 +160,11 @@ def send(self, text_list: list): try: r = requests.post(f'{self.server}/send_group_msg?group_id={id}&&message={text}') if r.status_code == 200: - Color.print_success(f'[+] qqBot 发送成功 {id}') + console.print(f'[+] qqBot 发送成功 {id}', style='bold green') else: - Color.print_failed(f'[-] qqBot 发送失败 {id}') + console.print(f'[-] qqBot 发送失败 {id}', style='bold red') except Exception as e: - Color.print_failed(f'[-] qqBot 发送失败 {id}') + console.print(f'[-] qqBot 发送失败 {id}', style='bold red') print(e) def start_server(self, qq_id, qq_passwd, timeout=60): @@ -182,14 +182,14 @@ def start_server(self, qq_id, qq_passwd, timeout=60): while True: try: requests.get(self.server) - Color.print_success('[+] qqBot 启动成功') + console.print('[+] qqBot 启动成功', style='bold green') return True except Exception as e: time.sleep(1) if time.time() > timeout: qqBot.kill_server() - Color.print_failed('[-] qqBot 启动失败') + console.print('[-] qqBot 启动失败', style='bold red') return False @classmethod @@ -245,9 +245,9 @@ def send(self, text: str): try: self.smtp.sendmail(self.sender, self.receiver.split(','), msg.as_string()) - Color.print_success('[+] mailBot 发送成功') + console.print('[+] mailBot 发送成功', style='bold green') except Exception as e: - Color.print_failed('[+] mailBot 发送失败') + console.print('[+] mailBot 发送失败', style='bold red') print(e) @@ -265,7 +265,7 @@ def test_connect(self): self.bot.get_me() return True except Exception as e: - Color.print_failed('[-] telegramBot 连接失败') + console.print('[-] telegramBot 连接失败', style='bold red') return False @staticmethod @@ -288,7 +288,7 @@ def send(self, text_list: list): for id in self.chat_id: try: self.bot.send_message(chat_id=id, text=text, parse_mode='HTML') - Color.print_success(f'[+] telegramBot 发送成功 {id}') + console.print(f'[+] telegramBot 发送成功 {id}', style='bold green') except Exception as e: - Color.print_failed(f'[-] telegramBot 发送失败 {id}') + console.print(f'[-] telegramBot 发送失败 {id}', style='bold red') print(e) diff --git a/requirements.txt b/requirements.txt index b491cefae..adc6bcd5d 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ +rich requests feedparser pyfiglet -colorama schedule pyyaml python-telegram-bot diff --git a/utils.py b/utils.py index 9724c9a28..12e1bf102 100644 --- a/utils.py +++ b/utils.py @@ -1,24 +1,9 @@ -import pprint -from colorama import Fore - - -class Color: - @staticmethod - def print_focus(data: str): - print(Fore.YELLOW+data+Fore.RESET) - - @staticmethod - def print_success(data: str): - print(Fore.LIGHTGREEN_EX+data+Fore.RESET) - - @staticmethod - def print_failed(data: str): - print(Fore.LIGHTRED_EX+data+Fore.RESET) - - @staticmethod - def print(data): - pprint.pprint(data) +from rich import print +from rich.console import Console +from rich.progress import Progress +console = Console() +progress = Progress() class Pattern: @staticmethod diff --git a/yarb.py b/yarb.py index 418845aec..574380dac 100755 --- a/yarb.py +++ b/yarb.py @@ -13,7 +13,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed from bot import * -from utils import Color, Pattern +from utils import * import requests requests.packages.urllib3.disable_warnings() @@ -100,9 +100,9 @@ def filter(title: str): item = {entry.title: entry.link} print(item) result |= item - Color.print_success(f'[+] {title}\t{url}\t{len(result.values())}/{len(r.entries)}') + console.print(f'[+] {title}\t{url}\t{len(result.values())}/{len(r.entries)}', style='bold green') except Exception as e: - Color.print_failed(f'[-] failed: {url}') + console.print(f'[-] failed: {url}', style='bold red') print(e) return title, result @@ -157,10 +157,10 @@ def init_rss(conf: dict, update: bool=False, proxy_url=''): if not check: feeds.append(url) except Exception as e: - Color.print_failed(f'[-] 解析失败:{value}') + console.print(f'[-] 解析失败:{value}', style='bold red') print(e) - Color.print_focus(f'[+] {len(feeds)} feeds') + console.print(f'[+] {len(feeds)} feeds', style='bold yellow') return feeds @@ -200,12 +200,12 @@ def job(args): if result: numb += len(result.values()) results.append({title: result}) - Color.print_focus(f'[+] {len(results)} feeds, {numb} articles') + console.print(f'[+] {len(results)} feeds, {numb} articles', style='bold yellow') # temp_path = root_path.joinpath('temp_data.json') # with open(temp_path, 'w+') as f: # f.write(json.dumps(results, indent=4, ensure_ascii=False)) - # Color.print_focus(f'[+] temp data: {temp_path}') + # console.print(f'[+] temp data: {temp_path}', style='bold yellow') # 更新today update_today(results)