From 17190ff67447eafc4e30908eedfa206ef6c7e422 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 8 Jan 2021 15:01:23 +0800 Subject: [PATCH] update --- rssbot.py | 27 ++++++++++++++++++++++++++- rsslist.py | 39 ++++++++++++++++++++++++++++++++++++--- textc.py | 11 +++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/rssbot.py b/rssbot.py index cc64212..31e828e 100644 --- a/rssbot.py +++ b/rssbot.py @@ -30,7 +30,7 @@ from random import randrange from textc import textc from re import search, I from rsschecker import RSSCheckerThread -from rsslist import getInlineKeyBoardForRSSList, InlineKeyBoardForRSSList +from rsslist import getInlineKeyBoardForRSSList, InlineKeyBoardForRSSList, getInlineKeyBoardForRSSInList, getTextContentForRSSInList from usercheck import checkUserPermissionsInChat, UserPermissionsInChatCheckResult @@ -835,6 +835,31 @@ class callbackQueryHandle(Thread): 'message_id': self._data['message']['message_id']} self._main._request("deleteMessage", "post", json=di) return + elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.Content: + di = {'chat_id': self._data['message']['chat']['id'], + 'message_id': self._data['message']['message_id']} + rssList = self._main._db.getRSSListByChatId(chatId) + ind = int(self._inputList[3]) + if ind >= len(rssList): + ind = len(rssList) - 1 + di['text'] = getTextContentForRSSInList(rssList[ind]) + di['parse_mode'] = 'HTML' + di['reply_markup'] = getInlineKeyBoardForRSSInList( + chatId, rssList[ind], ind) + self._main._request("editMessageText", "post", json=di) + self.answer() + return + elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.BackToList: + di = {'chat_id': self._data['message']['chat']['id'], + 'message_id': self._data['message']['message_id']} + rssList = self._main._db.getRSSListByChatId(chatId) + ind = int(self._inputList[3]) + di['text'] = '列表如下:' + di['reply_markup'] = getInlineKeyBoardForRSSList( + chatId, rssList, itemIndex=ind) + self._main._request("editMessageText", "post", json=di) + self.answer() + return else: self.answer('未知的按钮。') return diff --git a/rsslist.py b/rsslist.py index c42ed33..5fdc7ae 100644 --- a/rsslist.py +++ b/rsslist.py @@ -13,10 +13,12 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from RSSEntry import RSSEntry +from RSSEntry import RSSEntry, ChatEntry from typing import List from enum import Enum, unique -from math import ceil +from math import ceil, floor +from textc import textc, timeToStr +from html import escape @unique @@ -27,9 +29,29 @@ class InlineKeyBoardForRSSList(Enum): NextPage = 3 Close = 4 Content = 5 + BackToList = 6 -def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page: int = 1, lastPage: bool = False) -> dict: +def getTextContentForRSSInList(rssEntry: RSSEntry) -> str: + text = textc() + text.addtotext(f"""{rssEntry.title}""") + temp = '更新间隔:未知' if rssEntry.interval is None else f'更新间隔:{rssEntry.interval}分' + text.addtotext(temp) + temp = '上次更新时间:未知' if rssEntry.lastupdatetime is None or rssEntry.lastupdatetime < 0 else f'上次更新时间:{timeToStr(rssEntry.lastupdatetime)}' + text.addtotext(temp) + if len(rssEntry.chatList) > 0: + chatEntry: ChatEntry = rssEntry.chatList[0] + config = chatEntry.config + text.addtotext("设置:") + text.addtotext(f"禁用预览:{config.disable_web_page_preview}") + text.addtotext(f"显示RSS标题:{config.show_RSS_title}") + text.addtotext(f"显示内容标题:{config.show_Content_title}") + text.addtotext(f"显示内容:{config.show_content}") + text.addtotext(f"发送媒体:{config.send_media}") + return text.tostr() + + +def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page: int = 1, lastPage: bool = False, itemIndex: int = None) -> dict: d = [] i = -1 lineLimit = 7 @@ -37,6 +59,8 @@ def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page: i pn = ceil(l / lineLimit) if lastPage: page = pn + if itemIndex is not None and itemIndex >= 0: + page = floor(itemIndex / lineLimit) + 1 if l != 0: page = max(min(pn, page), 1) s = max(lineLimit * (page - 1), 0) @@ -70,3 +94,12 @@ def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page: i d[i].append( {'text': '关闭', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.Close.value}'}) return {'inline_keyboard': d} + + +def getInlineKeyBoardForRSSInList(chatId: int, rssEntry: RSSEntry, index: int) -> dict: + d = [] + i = -1 + d.append([]) + d[i].append( + {'text': '返回', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.BackToList.value},{index}'}) + return {'inline_keyboard': d} diff --git a/textc.py b/textc.py index c33ecbe..74326c3 100644 --- a/textc.py +++ b/textc.py @@ -13,6 +13,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from time import strftime, localtime, timezone + + class textc: def __init__(self): self.__str = '' @@ -25,3 +28,11 @@ class textc: self.__str = f"{self.__str}{s}" else: self.__str = f'{self.__str}\n{s}' + + +def timeToStr(t: int) -> str: + te = strftime('%Y-%m-%dT%H:%M:%S', localtime(t)) + op = '-' if timezone > 0 else '+' + te = te + op + \ + f'{int(abs(timezone)/3600):02}:{int(abs(timezone)%3600/60):02}' + return te