From b63c902d4c8090a384e5ad39fc974429b98c9e3c Mon Sep 17 00:00:00 2001 From: Armin Samii Date: Mon, 5 Feb 2024 12:09:07 -0500 Subject: [PATCH] address PR comments --- plot/plot.py | 11 +++++------ src/api_client.rs | 8 +++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/plot/plot.py b/plot/plot.py index de1ab86..4cec376 100755 --- a/plot/plot.py +++ b/plot/plot.py @@ -25,7 +25,6 @@ DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] YEAR_TO_SPLIT = datetime.datetime.now().year - 1 -YEAR_TO_SPLIT_STR = str(YEAR_TO_SPLIT) A = argparse.ArgumentParser( description='Generates plots of crossword statistics from a CSV', @@ -35,7 +34,7 @@ help="The path to a CSV file generated from the crossword crate") A.add_argument('out_file', action='store', type=str, help="The output path where plots should be saved (e.g. trends.png)") -A.add_argument('-c', '--ceiling', action='store', type=int, default=0, +A.add_argument('-c', '--ceiling', action='store', type=int, default=None, help='Max Y Value in minutes; use 0 to compute from data.') A.add_argument('-s', '--style', action='store', default="Solarize_Light2", type=str, # choices=plt.style.available, # looks gross @@ -135,9 +134,9 @@ def save_split_vln_plot(df, out_path, ymax): ceiling: max y-value to show """ df['solve_time_m'] = df['solve_time_secs'] / 60.0 - df['In ' + YEAR_TO_SPLIT_STR] = df['Solved datetime'] > datetime.datetime(YEAR_TO_SPLIT, 1, 1) + df[f'In {YEAR_TO_SPLIT}'] = df['Solved datetime'] > datetime.datetime(YEAR_TO_SPLIT, 1, 1) try: - ax = sns.violinplot(x="weekday", y="solve_time_m", hue='In ' + YEAR_TO_SPLIT_STR, + ax = sns.violinplot(x="weekday", y="solve_time_m", hue=f'In {YEAR_TO_SPLIT}', split=True, data=df, bw=.25, order=DAYS) except: # Happens if there is no data from last year @@ -153,7 +152,7 @@ def save_split_vln_plot(df, out_path, ymax): ax.legend() # Seems to have the effect of removing the title of the legend handles, labels = ax.get_legend_handles_labels() - ax.legend(handles, ["Before " + YEAR_TO_SPLIT_STR, YEAR_TO_SPLIT_STR + "+"], loc="upper left") + ax.legend(handles, [f"Before {YEAR_TO_SPLIT}", f"{YEAR_TO_SPLIT}+"], loc="upper left") plt.savefig(out_path) plt.close() @@ -162,7 +161,7 @@ def save_split_vln_plot(df, out_path, ymax): def generate(in_file, out_file, ceiling = None): df = parse_data(in_file) - if not ceiling: + if ceiling is None: # Pick an appropriate y-axis, balancing being robust to outliers vs. showing all data ymax = df["solve_time_secs"].quantile(0.99) / 60 else: diff --git a/src/api_client.rs b/src/api_client.rs index 8314875..bf3ca97 100644 --- a/src/api_client.rs +++ b/src/api_client.rs @@ -121,7 +121,13 @@ impl RateLimitedClient { let mut headers = HeaderMap::new(); headers.insert(header::ACCEPT, "application/json".parse().unwrap()); headers.insert(header::DNT, "1".parse().unwrap()); - headers.insert(header::COOKIE, HeaderValue::from_str(&format!("NYT-S={}", nyt_s)).unwrap()); + if nyt_s.len() == 162 { + headers.insert(header::COOKIE, HeaderValue::from_str(&format!("NYT-S={}", nyt_s)).unwrap()); + } else if nyt_s.len() == 142 { + headers.insert("nyt-s", nyt_s.parse().unwrap()); + } else { + panic!("NYT-S must be either 162 characters (for NYT-S cookie) or 142 characters (for nyt-s header)"); + } let client = reqwest::ClientBuilder::new() .user_agent("Scraping personal stats")