rj1
about | log | files | refs
commit 5335f79406f3e1a5b7b6f632976f671dd5e1f961
parent 5771e96e1b7d357820ce8a1bec42a5a6a2901db2
author: rj1 <[email protected]>
date:   Sat,  3 Dec 2022 02:33:06 -0600

added readme + stats scraper

Diffstat:
AREADME.md | 24++++++++++++++++++++++++
Aupdate-score | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,24 @@ +# advent of code + +this repo contains my solutions for advent of code + +## scores +### part 1 + +|day|time|rank|score| +|---|---|---|---| +|1|00:09:20|5035|0| +|2|00:13:22|4899|0| +|3|00:04:22|368|0| + +### part 2 + +|day|time|rank|score| +|---|---|---|---| +|1|00:10:24|3863|0| +|2|00:14:26|2281|0| +|3|00:08:06|442|0| + +## notes + +advent of code is fun! diff --git a/update-score b/update-score @@ -0,0 +1,56 @@ +#!/bin/python +import os +import requests +from bs4 import BeautifulSoup +import re + +base_dir = os.path.realpath(os.path.dirname(__file__)) +with open(base_dir + "/.session", "r") as file: + session = file.read().rstrip() + +# get data from adventofcode.com +response = requests.get( + "https://adventofcode.com/2022/leaderboard/self", cookies={"session": session} +) + +soup = BeautifulSoup(response.text, "lxml") +scoreboard = soup.find("article").find("pre").text +lines = scoreboard.strip().splitlines() + +# turn data into a markdown table +data_rows = lines[2:] +data_rows.reverse() +data_columns = [row.split() for row in data_rows] + +tables = [] +for i in range(2): + table = "" + table += "|" + "|".join(["day", "time", "rank", "score"]) + "|\n" + table += "|" + "|".join(["---"] * 4) + "|\n" + + for row in data_columns: + if i == 0: + table += "|" + "|".join(row[:4]) + "|\n" + else: + table += "|" + "|".join([row[0]] + row[4:]) + "|\n" + + tables.append(table) + +# insert markdown table into README.md +with open(base_dir + "/README.md") as file: + text = file.read() + +rx = r"^## scores\n(.*?)^## notes" +match = re.search(rx, text, flags=re.DOTALL | re.MULTILINE) + +if match: + new = "## scores\n" + new += f"### part 1\n\n{tables[0]}\n" + new += f"### part 2\n\n{tables[1]}\n" + new += "## notes" + + replace = text.replace(match.group(0), new) + with open(base_dir + "/README.md", "w") as file: + file.write(replace) + +print("updated scoreboard in README.md")