-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Personal stats #18
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
3becde5
39cf606
7e8f2f1
63b4d28
ebb1069
075348f
7e9edfc
bc07470
92f9f27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,6 +421,76 @@ impl AocClient { | |
| Ok(()) | ||
| } | ||
|
|
||
| fn get_personal_stats_html(&self) -> AocResult<String> { | ||
| debug!("🦌 Fetching {} personal stats", self.year); | ||
|
|
||
| let url = | ||
| format!("https://adventofcode.com/{}/leaderboard/self", self.year); | ||
| let response = http_client(&self.session_cookie, "text/html")? | ||
| .get(url) | ||
| .send()?; | ||
|
|
||
| if response.status() == StatusCode::NOT_FOUND { | ||
| // A 402 reponse means the calendar for | ||
| // the requested year is not yet available | ||
| return Err(AocError::InvalidEventYear(self.year)); | ||
| } else if response.status() == StatusCode::FOUND { | ||
| // A 302 reponse is a redirect and it likely | ||
| // means we're not logged in | ||
| warn!( | ||
| "🍪 It looks like you are not logged in, try logging in again" | ||
| ); | ||
| return Err(AocError::AocResponseError); | ||
| } | ||
|
|
||
| let contents = response.error_for_status()?.text()?; | ||
|
|
||
| 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_personal_stats(&self) -> AocResult<()> { | ||
| let stats_html = self.get_personal_stats_html()?; | ||
| let stats_text = self.html2text(&stats_html); | ||
|
|
||
| // print explanatory paragraph before recoloring the text | ||
| for line in stats_text.lines().take_while(|line| !line.is_empty()) { | ||
| println!("{}", line); | ||
| } | ||
|
|
||
| let stats_text = stats_text | ||
| .replace( | ||
| "--------Part 1---------", | ||
| &"--------Part 1---------".color(SILVER).to_string(), | ||
| ) | ||
| .replace( | ||
| "--------Part 2---------", | ||
| &"--------Part 2---------".color(GOLD).to_string(), | ||
|
||
| ) | ||
| .replace("Time", &"Time".color(GOLD).to_string()) | ||
| .replace("Rank", &"Rank".color(GOLD).to_string()) | ||
| .replace("Score", &"Score".color(GOLD).to_string()) | ||
| .replacen("Time", &"Time".color(SILVER).to_string(), 1) | ||
| .replacen("Rank", &"Rank".color(SILVER).to_string(), 2) // "Rank" also in explanation | ||
| .replacen("Score", &"Score".color(SILVER).to_string(), 2); // "Score" also in | ||
|
||
| // explanation | ||
|
|
||
| // just print out the day by day stats, expalanatory paragraph is recolored now | ||
| for line in stats_text.lines().skip_while(|line| !line.is_empty()) { | ||
| println!("{}", line); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn get_private_leaderboard( | ||
| &self, | ||
| leaderboard_id: LeaderboardId, | ||
|
|
||
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.
Maybe this deserves a new error, say AocError::NotLoggedIn. When showing the calendar not being logged in isn't a problem as you can still show the calendar with no stars. But here it's different...