import random from mastodon import Mastodon # Mastodon API 设置 mastodon = Mastodon( access_token = 'YOUR_ACCESS_TOKEN', api_base_url = 'https://mastodon.social' ) def get_post_comments(post_id): """获取指定帖子的所有评论""" comments = mastodon.status_context(post_id)['descendants'] return comments def is_local_user(account): """检查用户是否为本站用户""" return '@' not in account['acct'] def get_eligible_users(comments): """获取符合条件的本站用户列表""" eligible_users = set() for comment in comments: if is_local_user(comment['account']): eligible_users.add(comment['account']['acct']) return list(eligible_users) def draw_winner(eligible_users, num_winners=1): """从符合条件的用户中随机抽取指定数量的获奖者""" if len(eligible_users) < num_winners: return eligible_users return random.sample(eligible_users, num_winners) def main(): post_id = input("请输入 Mastodon 帖子 ID: ") num_winners = int(input("请输入要抽取的获奖者数量: ")) comments = get_post_comments(post_id) eligible_users = get_eligible_users(comments) winners = draw_winner(eligible_users, num_winners) print(f"\n共有 {len(comments)} 条评论") print(f"符合条件的本站用户数量:{len(eligible_users)}") print(f"抽取的 {len(winners)} 名获奖者是:") for winner in winners: print(winner) if __name__ == "__main__": main()