From f4f08bfd786a6a8adbe593de6de759685d7f62ef Mon Sep 17 00:00:00 2001 From: Hobee Date: Fri, 3 Oct 2025 23:51:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B5=B7=E5=A7=8B?= =?UTF-8?q?=E6=A5=BC=E5=B1=82=E7=AD=9B=E9=80=89=E5=8A=9F=E8=83=BD=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8A=BD=E5=A5=96=E8=8C=83=E5=9B=B4=E7=B2=BE=E7=A1=AE?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lottery.py | 26 ++++++++++++++++++++++---- lottery_server.py | 3 ++- static/index.html | 9 +++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lottery.py b/lottery.py index cd198a4..a2e8e2a 100644 --- a/lottery.py +++ b/lottery.py @@ -85,7 +85,7 @@ def fetch_topic_info(self): except KeyError: raise TopicError("返回的JSON数据格式不正确") - def fetch_valid_post_numbers(self, last_floor=None): + def fetch_valid_post_numbers(self, start_floor=None, last_floor=None): """获取有效的楼层号""" valid_posts_url = f"{self.connect_url}/api/topic/{self.topic_id}/valid_post_number" try: @@ -108,6 +108,13 @@ def fetch_valid_post_numbers(self, last_floor=None): self.valid_post_ids = self.valid_post_ids[:cut_index] self.valid_post_created = self.valid_post_created[:cut_index] + if start_floor is not None: + cut_index = next((i for i, floor in enumerate(self.valid_post_numbers) if floor >= start_floor), 0) + + self.valid_post_numbers = self.valid_post_numbers[cut_index:] + self.valid_post_ids = self.valid_post_ids[cut_index:] + self.valid_post_created = self.valid_post_created[cut_index:] + return self.valid_post_numbers except requests.RequestException as e: raise TopicError(f"获取有效楼层失败: {str(e)}") @@ -179,6 +186,15 @@ def get_interactive_input(): print("错误: 中奖人数必须为大于0的整数") continue + start_floor = input("请输入参与抽奖的起始楼层(可选,直接回车使用所有楼层): ") + if start_floor: + if not start_floor.isdigit() or int(start_floor) < 1: + print("错误: 起始楼层必须为大于0的整数") + continue + start_floor = int(start_floor) + else: + start_floor = None + last_floor = input("请输入参与抽奖的最后楼层(可选,直接回车使用所有楼层): ") if last_floor: if not last_floor.isdigit() or int(last_floor) < 0: @@ -188,7 +204,7 @@ def get_interactive_input(): else: last_floor = None - return topic_url, int(winners_count), last_floor + return topic_url, int(winners_count), start_floor, last_floor except KeyboardInterrupt: print("\n已取消操作") @@ -205,24 +221,26 @@ def main(): parser.add_argument('-t', '--terminal', action='store_true', help='启用终端交互模式') parser.add_argument('topic_url', nargs='?', help='帖子URL') parser.add_argument('winners_count', nargs='?', type=int, help='中奖人数') + parser.add_argument('-s', '--start-floor', type=int, help='参与抽奖的起始楼层(可选)') parser.add_argument('-f', '--last-floor', type=int, help='参与抽奖的最后楼层(可选)') args = parser.parse_args() if args.terminal: - topic_url, winners_count, last_floor = get_interactive_input() + topic_url, winners_count, start_floor, last_floor = get_interactive_input() else: if args.topic_url is None or args.winners_count is None: parser.print_help() sys.exit(1) topic_url = args.topic_url winners_count = args.winners_count + start_floor = args.start_floor last_floor = args.last_floor try: topic_info = ForumTopicInfo.from_url(topic_url) topic_info.fetch_topic_info() - valid_floors = topic_info.fetch_valid_post_numbers(last_floor) + valid_floors = topic_info.fetch_valid_post_numbers(start_floor, last_floor) total_floors = len(valid_floors) if total_floors < 1: diff --git a/lottery_server.py b/lottery_server.py index 222b166..9bc4772 100644 --- a/lottery_server.py +++ b/lottery_server.py @@ -36,6 +36,7 @@ class LotteryRequest(BaseModel): topic_url: str winners_count: int + start_floor: Optional[int] = None last_floor: Optional[int] = None @@ -69,7 +70,7 @@ async def draw_lottery(request: LotteryRequest): # 初始化主题信息 topic_info = ForumTopicInfo.from_url(request.topic_url) topic_info.fetch_topic_info() - valid_floors = topic_info.fetch_valid_post_numbers(request.last_floor) + valid_floors = topic_info.fetch_valid_post_numbers(request.start_floor, request.last_floor) total_floors = len(valid_floors) if total_floors < 1: diff --git a/static/index.html b/static/index.html index fc54cc6..86002f5 100644 --- a/static/index.html +++ b/static/index.html @@ -248,6 +248,12 @@

LINUX DO 抽奖程序

placeholder="请输入中奖人数"> +
+ + +
+
LINUX DO 抽奖程序 body: JSON.stringify({ topic_url: document.getElementById('topicUrl').value, winners_count: parseInt(document.getElementById('winnersCount').value), + start_floor: document.getElementById('startFloor').value ? + parseInt(document.getElementById('startFloor').value) : + null, last_floor: document.getElementById('lastFloor').value ? parseInt(document.getElementById('lastFloor').value) : null