Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Global leaderboard #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 71 additions & 0 deletions aoc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,77 @@ impl AocClient {
Ok(())
}

fn get_global_leaderboard_html(&self) -> AocResult<String> {
debug!("🦌 Fetching global leaderboard for {}", self.year);

let url = format!("https://adventofcode.com/{}/leaderboard", self.year);
let response = reqwest::blocking::get(url)?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use reqwest directly instead of calling http_client? This way you're missing some common error handling there and also not passing the user agent header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just used reqwest directly because I saw the session cookie wasn't required for grabbing the global leaderboard. Happy to switch it over for the error handling :)

let contents = response.error_for_status()?.text()?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to handle NOT FOUND which can only happen if the year is invalid (like it's done in get_calendar_html):

        if response.status() == StatusCode::NOT_FOUND {
            // A 404 reponse means the leaderboard for
            // the requested year is not yet available
            return Err(AocError::InvalidEventYear(self.year));
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Added :)


let main = Regex::new(r"(?i)(?s)<main>(?P<main>.*)</main>")
.unwrap()
.captures(&contents)
.ok_or(AocError::AocResponseError)?
.name("main")
.unwrap()
.as_str()
.to_string();

Ok(main)
}

pub fn show_global_leaderboard(&self) -> AocResult<()> {
let leaderboard_html = self.get_global_leaderboard_html()?;
let leadboard_text = self.html2text(&leaderboard_html);
const MAX_RANK_LEN: usize = 4; //"100)"

let colored_leaderboard = leadboard_text
.replace("(AoC++)", &"(AoC++)".color(GOLD).to_string())
.replace(
"(Sponsor)",
&"(Sponsor)".color(Color::BrightBlue).to_string(),
);

println!("");
for line in colored_leaderboard
.lines()
.skip_while(|line| !line.is_empty())
.skip(1)
.take_while(|line| !line.contains(")"))
{
println!("{}", line);
}

for line in colored_leaderboard
.lines()
.skip_while(|line| !line.is_empty())
.skip(1)
.skip_while(|line| !line.contains(")"))
{
if line.is_empty() {
continue;
}
let split: Vec<&str> = line.split_whitespace().collect();
let (rank, info) = line.split_once(" ").unwrap();
// normal case, entry has its own unique ranking
if rank.contains(")") {
// The longest ranking is "100)", want every other ranking to
// line up it's ")" with the ) in "100)"
let padding = " ".repeat(MAX_RANK_LEN - split[0].len());
println!("{}{} {}", padding, rank, info);
} else {
// in this corner case, two or more entries share the same
// raking, but the rank # is only given to the first entry. Under
// these conditions, 'rank' holds the users's score and 'info' holds
// the user's remaining info (e.g. name, AoC++, etc)
let padding = " ".repeat(MAX_RANK_LEN);
println!("{} {} {}", padding, rank, info);
}
}

Ok(())
}

fn get_private_leaderboard(
&self,
leaderboard_id: LeaderboardId,
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub enum Command {
#[command(visible_alias = "d")]
Download,

/// Show Advent of Code global leaderboard
#[command(visible_alias = "g")]
GlobalLeaderboard,

/// Read puzzle statement (the default command)
#[command(visible_alias = "r")]
Read,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn run(args: &Args, client: AocClient) -> AocResult<()> {
}
Ok(())
}
Some(Command::GlobalLeaderboard) => client.show_global_leaderboard(),
Some(Command::Submit { part, answer }) => {
client.submit_answer_and_show_outcome(part, answer)
}
Expand Down