mirror of
https://github.com/developersIndia/deviras.git
synced 2024-12-22 08:09:49 +05:30
Merge pull request #11 from ni5arga/main
Add script for updating advent of code leaderboard stats
This commit is contained in:
commit
ee3efe7c13
BIN
aoc/cookie.png
Normal file
BIN
aoc/cookie.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 134 KiB |
61
aoc/main.py
Normal file
61
aoc/main.py
Normal file
@ -0,0 +1,61 @@
|
||||
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 (by https://github.com/ni5arga/)'
|
||||
aoc_session_cookie = os.environ["AOC_SESSION_COOKIE"]
|
||||
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}'
|
||||
|
||||
def get_leaderboard_data():
|
||||
response = requests.get(aoc_url.format(year=2023), cookies={'session': aoc_session_cookie})
|
||||
data = response.json()
|
||||
return data
|
||||
|
||||
def format_leaderboard(data, num_players=20):
|
||||
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
|
||||
sorted_members = sorted(data['members'].values(), key=lambda x: x['stars'], reverse=True)
|
||||
|
||||
# Include only the top players
|
||||
for i, member_data in enumerate(sorted_members[:num_players]):
|
||||
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"
|
||||
|
||||
return leaderboard_stats
|
||||
|
||||
def update_reddit_post(reddit, post_id, new_stats):
|
||||
post = reddit.submission(id=post_id)
|
||||
post.edit(new_stats)
|
||||
|
||||
def main():
|
||||
if not reddit_post_id:
|
||||
print("Please set the REDDIT_POST_ID environment variable.")
|
||||
return
|
||||
|
||||
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)
|
||||
|
||||
update_reddit_post(reddit, reddit_post_id, formatted_stats)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
48
aoc/readme.md
Normal file
48
aoc/readme.md
Normal file
@ -0,0 +1,48 @@
|
||||
# AoC Private Leaderboard Stats Updater Script for Reddit
|
||||
|
||||
## Required Environment Variables
|
||||
|
||||
|
||||
1. `REDDIT_CLIENT_ID`: Reddit API client ID.
|
||||
2. `REDDIT_CLIENT_SECRET`: Reddit API client secret.
|
||||
3. `REDDIT_PASSWORD`: Reddit account password.
|
||||
4. `REDDIT_USERNAME`: Reddit account username.
|
||||
5. `AOC_SESSION_COOKIE`: Session cookie for the Advent of Code website.
|
||||
6. `AOC_LEADERBOARD_CODE`: Code for the Advent of Code leaderboard.
|
||||
7. `REDDIT_POST_ID`: ID of Reddit post which is used as leaderboard.
|
||||
|
||||
----
|
||||
## Instructions on how to get `AOC_SESSION_COOKIE`
|
||||
1. **Create an Advent of Code Account:**
|
||||
- If you don't have an Advent of Code account, go to the [Advent of Code website](https://adventofcode.com/), and sign up for an account.
|
||||
|
||||
2. **Log into Your AoC Account & open the leaderboard**
|
||||
- After creating an account, log into the AoC website using your credentials. Make sure you have joined the private leaderboard which's ID you have set in `AOC_LEADERBOARD_CODE`. Now navigate to the leaderboard page.
|
||||
|
||||
3. **Open Developer Tools in Your Browser:**
|
||||
- Open the browser's developer tools. You can usually do this by right-clicking on the web page, selecting "Inspect" or "Inspect Element," and then navigating to the "Network" tab.
|
||||
|
||||
4. **Go to the Network Tab:**
|
||||
- In the developer tools, go to the "Network" tab. This tab will show all network requests made by the website.
|
||||
|
||||
5. **Refresh the Page:**
|
||||
- Refresh the Advent of Code website. This will trigger various network requests, including the one that authenticates your session.
|
||||
|
||||
6. **Look for the Request with the Cookie:**
|
||||
- In the "Network" tab, look for a network request that is related to the Advent of Code website. It might be named something like "session" or "authenticate."
|
||||
- Click on this request to view its details.
|
||||
|
||||
7. **Find the Cookie Information:**
|
||||
- In the details of the network request, look for a section named "Request Headers" or "Cookies." You are interested in the value of the `session` cookie.
|
||||
|
||||
8. **Copy the Session Cookie Value:**
|
||||
- Copy the value of the `session` cookie. It is usually a long hex string of letters and numbers.
|
||||
|
||||
9. **Use the Session Cookie:**
|
||||
- Paste the copied session cookie value into the appropriate environment variable (`AOC_SESSION_COOKIE` in this case) in your code or set it as an environment variable.
|
||||
|
||||
![session-cookie](https://github.com/ni5arga/deviras/blob/main/aoc/cookie.png?raw=true)
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user