From 691de9011774ab4a4189823601ac4c1f1bc17aa5 Mon Sep 17 00:00:00 2001 From: Dianliang233 Date: Sat, 16 Jul 2022 11:31:56 +0800 Subject: [PATCH] Add author, date and category to mcbbs_news --- modules/mcbbs_news/__init__.py | 6 ++++-- modules/mcbbs_news/mcbbs_news.py | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/modules/mcbbs_news/__init__.py b/modules/mcbbs_news/__init__.py index f7f01241..60534bb5 100644 --- a/modules/mcbbs_news/__init__.py +++ b/modules/mcbbs_news/__init__.py @@ -1,5 +1,6 @@ from core.component import on_command from core.builtins.message import MessageSession +from core.elements import Url from .mcbbs_news import news @@ -20,6 +21,7 @@ async def main(msg: MessageSession): else: lst = [] for i in res: - lst += [f'{i["count"]}. {i["title"]} - {i["url"]}'] - message = '\n'.join(lst) + lst += [f'{i["count"]}. [{i["category"]}] {i["title"]} - {i["author"]} @ {i["time"]}\n{i["url"]}'] + message = '\n'.join(lst) + '\n更多资讯详见 ' + \ + Url('https://www.mcbbs.net/forum-news-1.html').url await msg.finish(message) diff --git a/modules/mcbbs_news/mcbbs_news.py b/modules/mcbbs_news/mcbbs_news.py index 6d1cb385..c32da179 100644 --- a/modules/mcbbs_news/mcbbs_news.py +++ b/modules/mcbbs_news/mcbbs_news.py @@ -1,4 +1,5 @@ from bs4 import BeautifulSoup +import datetime from config import Config from core.elements import Url @@ -8,9 +9,8 @@ from core.utils import get_url async def news(): api = 'https://www.mcbbs.net/forum-news-1.html' webrender = Config('web_render') - if not webrender: - return - api = webrender + 'source?url=' + api + if webrender: + api = webrender + 'source?url=' + api html = await get_url(api) print(html) bs = BeautifulSoup(html, 'html.parser') @@ -23,9 +23,21 @@ async def news(): a = i.select_one('a.s.xst') if not a.has_attr('style'): continue + category = i.select_one('tr > th > em > a').get_text() + author = i.select_one( + 'tr > td.by > cite > a').get_text() + time = i.select_one('tr > td.by > em').get_text() + if time.find('-') != -1: + time = time.split('-') + time_class = datetime.date( + int(time[0]), int(time[1]), int(time[2])) + delta = datetime.date.today() - time_class + time = str(delta.days) + ' 天前' + title = a.get_text() url = Url('https://www.mcbbs.net/' + a.get('href')) - res += [{'count': len(res) + 1, 'title': title, 'url': url}] + res += [{'count': len(res) + 1, 'title': title, 'url': url, + 'author': author, 'time': time, 'category': category}] return res else: return None