-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
192 lines (156 loc) · 5.94 KB
/
utils.py
File metadata and controls
192 lines (156 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import cProfile
import io
import json
import os
import pstats
import shutil
from datetime import datetime
from functools import wraps
from os.path import exists
from time import time
import markdown
import requests
import snakemd
from bs4 import BeautifulSoup
from dotenv import load_dotenv
## Profiling functions
# from https://towardsdatascience.com/bite-sized-python-recipes-52cde45f1489
def timeit(func):
"""
:param func: Decorated function
:return: Execution time for the decorated function
"""
@wraps(func)
def wrapper(*args, **kwargs):
start = time()
result = func(*args, **kwargs)
end = time()
print(f'Timeit: {func.__name__} executed in {end - start:.6f} seconds')
return result
return wrapper
def profile(func):
def wrapper(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = func(*args, **kwargs)
pr.disable()
s = io.StringIO()
sort_by = pstats.SortKey.CUMULATIVE # 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sort_by)
ps.print_stats()
print(s.getvalue())
return retval
return wrapper
# Markdown generator functions
load_dotenv()
CURRENT_DIR = os.getcwd()
SESSION_COOKIE = os.getenv('SESSION_COOKIE')
HEADERS = {"cookie": f"session={SESSION_COOKIE}", }
readme_table = {}
def read_readme(directory, year):
if not exists(f'{directory}README.md'):
return None
else:
if not readme_table:
soup = BeautifulSoup(markdown.markdown(
open(f"{directory}README.md", "r", encoding='utf-8', errors="ignore"
).read()), "html.parser")
a = soup.findAll('a')
i = 1
for row in (a[1:]):
if 'md' not in str(row):
continue
else:
title = row.text
readme_table[year, i] = title
i += 1
return readme_table
def get_puzzle_name(directory, year, day):
table = read_readme(directory, year)
if (year, day) in table:
name = table[(year, day)]
else:
name = fetch_puzzle_name(year, day)
return name
def fetch_puzzle_name(year, day):
url = f"https://adventofcode.com/{year}/day/{day}"
ret = requests.get(url).text.strip()
x = BeautifulSoup(ret, "html.parser")
name = x.article.contents[0].contents[0]
name = name.strip().split(':')[1].strip()[:-4]
return name
def get_table_body(year, upto, directory):
base_url = "https://github.com/mukundv/aoc/"
body = []
for i in range(1, int(upto) + 1):
name = get_puzzle_name(directory, year, i)
a = str(i).zfill(2)
if name:
puzzle = snakemd.InlineText(name, url=f"{base_url}blob/main/{year}/day{a}/day{a}.md")
else:
puzzle = snakemd.InlineText(fetch_puzzle_name(year, i), url=f"{base_url}blob/main/day{a}/day{a}.md")
aoc_input = snakemd.InlineText(f"day{a}_input.txt", url=f"{base_url}blob/main/{year}/day{a}/day{a}_input.txt")
solution = snakemd.InlineText(f"day{a}.py", url=f"{base_url}blob/main/{year}/day{a}/day{a}.py")
body.append([a, puzzle, aoc_input, solution])
return body
def get_stars_days(year, userid):
leaderboard = f'https://adventofcode.com/{year}/leaderboard/private/view/{userid}.json'
print('Fetching leaderboard data from : ' + leaderboard)
r = requests.get(leaderboard, headers=HEADERS)
if r.status_code != 200:
print(f'Leaderboard API returned status code {r.status_code}: {r.text}')
exit(1)
data = json.loads(r.text)
stars = data['members'][userid]['stars']
days_completed = 0
for day in data['members'][userid]['completion_day_level']:
if '2' in data['members'][userid]['completion_day_level'][day]:
days_completed += 1
print(f'Stars: {stars}')
print(f'Days completed: {days_completed}')
return stars, days_completed
def get_patches(year):
stars, days_completed = get_stars_days(year, '1681351')
star_url = f"https://img.shields.io/badge/stars%20-{stars}-yellow"
days_completed_url = f"https://img.shields.io/badge/days%20completed-{days_completed}-red"
stars_patch = snakemd.InlineText("stars", url=star_url, image=True)
days_completed_patch = snakemd.InlineText("stars", url=days_completed_url, image=True)
return stars_patch, days_completed_patch
def generate_readme(name, year, day, directory):
readme = snakemd.Document(name)
readme.add_header(f"AOC {year}")
stars, days = get_patches(year)
readme.add_element(
snakemd.Paragraph([stars, " ", days])
)
readme.add_paragraph(f"Fun with Python :snake: - aoc {year}") \
.insert_link(f"aoc {year}", f"https://adventofcode.com/{year}")
header = ["Day", "Puzzle", "Input", "Solution"]
readme.add_element(snakemd.Table(header=header, body=get_table_body(year, day, directory)))
now = datetime.today().strftime('%d-%m-%Y %H:%M:%S')
readme.add_paragraph(f"This document was automatically rendered on {now} using SnakeMD") \
.insert_link("SnakeMD", url="https://github.com/TheRenegadeCoder/SnakeMD")
readme.output_page(dump_dir=directory)
print(f'Readme Generated {directory}README.md')
def generate_dirs(day, year):
if not os.path.exists(f'{os.getcwd()}/{year}'):
os.mkdir(f'{os.getcwd()}/{year}')
if len(str(day)) == 1:
day = '0' + str(day)
path = f'{os.getcwd()}/{year}/day{day}'
if os.path.exists(path):
print(f' Directory already exists')
else:
os.mkdir(path)
print(f'Directory {path} created')
copy_template(path, f'day{day}.py')
print(f'File {path}/day{day}.py created')
def copy_template(path, target):
source = f'{os.getcwd()}/template.py'
target = f'{path}/{target}'
shutil.copyfile(source, target)
def setup_year(year):
for day in range(1, 26):
generate_dirs(day, year)
if __name__ == '__main__':
setup_year(2020)