Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)}")
Expand Down Expand Up @@ -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:
Expand All @@ -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已取消操作")
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion lottery_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class LotteryRequest(BaseModel):
topic_url: str
winners_count: int
start_floor: Optional[int] = None
last_floor: Optional[int] = None


Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ <h1>LINUX DO 抽奖程序</h1>
placeholder="请输入中奖人数">
</div>

<div class="form-group">
<label for="startFloor">参与抽奖起始楼层 (选填)</label>
<input type="number" id="startFloor" min="1"
placeholder="可选,限制参与抽奖的起始楼层">
</div>

<div class="form-group">
<label for="lastFloor">参与抽奖最后楼层 (选填)</label>
<input type="number" id="lastFloor" min="1"
Expand Down Expand Up @@ -326,6 +332,9 @@ <h1>LINUX DO 抽奖程序</h1>
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
Expand Down