markdown table style

This commit is contained in:
Nisarga Adhikary 2023-11-13 16:30:31 +05:30 committed by GitHub
parent 1c08df1867
commit 1b4d263a51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ username = os.environ["REDDIT_USERNAME"]
user_agent = 'AdventOfCode Leaderboard Updater (by https://github.com/ni5arga/)' user_agent = 'AdventOfCode Leaderboard Updater (by https://github.com/ni5arga/)'
aoc_session_cookie = os.environ["AOC_SESSION_COOKIE"] aoc_session_cookie = os.environ["AOC_SESSION_COOKIE"]
aoc_leaderboard_code = os.environ["AOC_LEADERBOARD_CODE"] aoc_leaderboard_code = os.environ["AOC_LEADERBOARD_CODE"]
reddit_post_id = os.environ.get("REDDIT_POST_ID")
aoc_url = f'https://adventofcode.com/{{year}}/leaderboard/private/view/{aoc_leaderboard_code}' aoc_url = f'https://adventofcode.com/{{year}}/leaderboard/private/view/{aoc_leaderboard_code}'
@ -17,15 +18,17 @@ def get_leaderboard_data():
data = response.json() data = response.json()
return data return data
def format_leaderboard(data): def format_leaderboard(data, num_players=20):
leaderboard_stats = "r/DevelopersIndia Advent of Code Leaderboard Stats\n\n" leaderboard_stats = "r/DevelopersIndia Advent of Code Leaderboard Stats\n\n"
leaderboard_stats += "| Rank | Player | Stars |\n"
leaderboard_stats += "|------|--------|-------|\n"
# Sort members by stars in descending order # Sort members by stars in descending order
sorted_members = sorted(data['members'].values(), key=lambda x: x['stars'], reverse=True) sorted_members = sorted(data['members'].values(), key=lambda x: x['stars'], reverse=True)
# Include only the top 10 players # Include only the top players
for i, member_data in enumerate(sorted_members[:10]): for i, member_data in enumerate(sorted_members[:num_players]):
leaderboard_stats += f"{i + 1}. {member_data['name']} - Stars: {member_data['stars']} | Score: {member_data['local_score']}\n" leaderboard_stats += f"| {i + 1} | {member_data['name']} | {member_data['stars']} |\n"
leaderboard_stats += f"\n[Advent of Code Leaderboard](https://adventofcode.com/2023/leaderboard/private/view/{aoc_leaderboard_code})\n" leaderboard_stats += f"\n[Advent of Code Leaderboard](https://adventofcode.com/2023/leaderboard/private/view/{aoc_leaderboard_code})\n"
@ -36,6 +39,10 @@ def update_reddit_post(reddit, post_id, new_stats):
post.edit(new_stats) post.edit(new_stats)
def main(): def main():
if not reddit_post_id:
print("Please set the REDDIT_POST_ID environment variable.")
return
reddit = praw.Reddit( reddit = praw.Reddit(
client_id=client_id, client_id=client_id,
client_secret=client_secret, client_secret=client_secret,
@ -44,17 +51,11 @@ def main():
user_agent=user_agent user_agent=user_agent
) )
post_id = 'YOUR_REDDIT_POST_ID'
if post_id == 'YOUR_REDDIT_POST_ID':
post = reddit.subreddit('developersindia').submit('Advent of Code Leaderboard', selftext='devsindia')
post_id = post.id
leaderboard_data = get_leaderboard_data() leaderboard_data = get_leaderboard_data()
formatted_stats = format_leaderboard(leaderboard_data) formatted_stats = format_leaderboard(leaderboard_data)
update_reddit_post(reddit, post_id, formatted_stats) update_reddit_post(reddit, reddit_post_id, formatted_stats)
if __name__ == "__main__": if __name__ == "__main__":
main() main()