deviras/aoc/main.py

68 lines
2.4 KiB
Python
Raw Normal View History

2023-11-13 15:40:50 +05:30
import praw
import requests
import os
client_id = os.environ["REDDIT_CLIENT_ID"]
client_secret = os.environ["REDDIT_CLIENT_SECRET"]
reddit_pass = os.environ["REDDIT_PASSWORD"]
username = os.environ["REDDIT_USERNAME"]
user_agent = 'AdventOfCode Leaderboard Updater - developersIndia'
2023-11-13 15:40:50 +05:30
aoc_session_cookie = os.environ["AOC_SESSION_COOKIE"]
2023-11-13 16:25:50 +05:30
aoc_leaderboard_code = os.environ["AOC_LEADERBOARD_CODE"]
aoc_year = os.environ.get("AOC_YEAR")
2024-12-01 12:45:37 +05:30
reddit_post_id = os.environ.get("AOC_LEADERBOARD_REDDIT_POST_ID")
2023-11-13 16:25:50 +05:30
2024-12-01 12:45:37 +05:30
aoc_url = f'https://adventofcode.com/{aoc_year}/leaderboard/private/view/{aoc_leaderboard_code}.json'
2023-11-13 15:40:50 +05:30
def get_leaderboard_data():
response = requests.get(aoc_url.format(year=aoc_year), cookies={'session': aoc_session_cookie})
2023-11-13 15:40:50 +05:30
data = response.json()
return data
2023-12-02 18:59:40 +05:30
def format_leaderboard(data, num_players=100):
leaderboard_stats = f"r/developersIndia Advent of Code {aoc_year} - Leaderboard\n\n"
2023-12-01 12:25:43 +05:30
leaderboard_stats += "| Rank | Player | Stars | Score |\n"
leaderboard_stats += "|------|--------|-------|-------|\n"
2023-11-13 15:40:50 +05:30
# Sort members by stars in descending order
2023-12-02 18:59:40 +05:30
sorted_members = sorted(data['members'].values(), key=lambda x: x['local_score'], reverse=True)
2023-11-13 15:40:50 +05:30
2023-11-13 16:30:31 +05:30
# Include only the top players
for i, member_data in enumerate(sorted_members[:num_players]):
2023-12-28 22:25:58 +05:30
# check for non-zero local_score
if member_data['local_score'] > 0:
2024-12-05 18:07:37 +05:30
# if member's name is null, use the member_id
if member_data['name'] == None:
member_data['name'] = f"Anonymous #{member_data['id']}"
2023-12-28 22:25:58 +05:30
leaderboard_stats += f"| {i + 1} | {member_data['name']} | {member_data['stars']} | {member_data['local_score']} |\n"
2023-11-13 15:40:50 +05:30
2023-12-02 18:59:40 +05:30
leaderboard_stats += f"\nUpdated every 24 hours"
2023-11-13 16:25:50 +05:30
2023-11-13 15:40:50 +05:30
return leaderboard_stats
def update_reddit_post(reddit, post_id, new_stats):
post = reddit.submission(id=post_id)
post.edit(new_stats)
def main():
2023-11-13 16:30:31 +05:30
if not reddit_post_id:
2024-12-01 12:45:37 +05:30
print("Please set the AOC_LEADERBOARD_REDDIT_POST_ID environment variable.")
2023-11-13 16:30:31 +05:30
return
2023-11-13 15:40:50 +05:30
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
username=username,
password=reddit_pass,
user_agent=user_agent
)
leaderboard_data = get_leaderboard_data()
formatted_stats = format_leaderboard(leaderboard_data)
2024-12-05 18:07:37 +05:30
print(formatted_stats)
# update_reddit_post(reddit, reddit_post_id, formatted_stats)
2023-11-13 15:40:50 +05:30
if __name__ == "__main__":
main()