-
Notifications
You must be signed in to change notification settings - Fork 20
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
WillLillis
wants to merge
3
commits into
scarvalhojr:main
Choose a base branch
from
WillLillis:global_leaderboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)?; | ||
let contents = response.error_for_status()?.text()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 callinghttp_client
? This way you're missing some common error handling there and also not passing the user agent header.There was a problem hiding this comment.
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 :)