Initial commit
commit
1a2c56aa3d
|
@ -0,0 +1,7 @@
|
|||
Copyright 2021 Nicholas Krichevsky
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,22 @@
|
|||
[[source]]
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[packages]
|
||||
beautifulsoup4 = "*"
|
||||
requests = "*"
|
||||
coloredlogs = "*"
|
||||
click = "*"
|
||||
yaspin = "*"
|
||||
python-dateutil = "*"
|
||||
|
||||
[dev-packages]
|
||||
black = "*"
|
||||
isort = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.9"
|
||||
|
||||
[pipenv]
|
||||
allow_prereleases = true
|
|
@ -0,0 +1,272 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "0b63f8a93999e1e6b6592c9107cc54cfc9467deaee802b312900e052795fb2b3"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
"python_version": "3.9"
|
||||
},
|
||||
"sources": [
|
||||
{
|
||||
"name": "pypi",
|
||||
"url": "https://pypi.org/simple",
|
||||
"verify_ssl": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"default": {
|
||||
"beautifulsoup4": {
|
||||
"hashes": [
|
||||
"sha256:9a315ce70049920ea4572a4055bc4bd700c940521d36fc858205ad4fcde149bf",
|
||||
"sha256:c23ad23c521d818955a4151a67d81580319d4bf548d3d49f4223ae041ff98891"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==4.10.0"
|
||||
},
|
||||
"certifi": {
|
||||
"hashes": [
|
||||
"sha256:78884e7c1d4b00ce3cea67b44566851c4343c120abd683433ce934a68ea58872",
|
||||
"sha256:d62a0163eb4c2344ac042ab2bdf75399a71a2d8c7d47eac2e2ee91b9d6339569"
|
||||
],
|
||||
"version": "==2021.10.8"
|
||||
},
|
||||
"charset-normalizer": {
|
||||
"hashes": [
|
||||
"sha256:735e240d9a8506778cd7a453d97e817e536bb1fc29f4f6961ce297b9c7a917b0",
|
||||
"sha256:83fcdeb225499d6344c8f7f34684c2981270beacc32ede2e669e94f7fa544405"
|
||||
],
|
||||
"markers": "python_version >= '3'",
|
||||
"version": "==2.0.8"
|
||||
},
|
||||
"click": {
|
||||
"hashes": [
|
||||
"sha256:353f466495adaeb40b6b5f592f9f91cb22372351c84caeb068132442a4518ef3",
|
||||
"sha256:410e932b050f5eed773c4cda94de75971c89cdb3155a72a0831139a79e5ecb5b"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==8.0.3"
|
||||
},
|
||||
"coloredlogs": {
|
||||
"hashes": [
|
||||
"sha256:612ee75c546f53e92e70049c9dbfcc18c935a2b9a53b66085ce9ef6a6e5c0934",
|
||||
"sha256:7c991aa71a4577af2f82600d8f8f3a89f936baeaf9b50a9c197da014e5bf16b0"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==15.0.1"
|
||||
},
|
||||
"humanfriendly": {
|
||||
"hashes": [
|
||||
"sha256:1697e1a8a8f550fd43c2865cd84542fc175a61dcb779b6fee18cf6b6ccba1477",
|
||||
"sha256:6b0b831ce8f15f7300721aa49829fc4e83921a9a301cc7f606be6686a2288ddc"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
|
||||
"version": "==10.0"
|
||||
},
|
||||
"idna": {
|
||||
"hashes": [
|
||||
"sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff",
|
||||
"sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"
|
||||
],
|
||||
"markers": "python_version >= '3'",
|
||||
"version": "==3.3"
|
||||
},
|
||||
"python-dateutil": {
|
||||
"hashes": [
|
||||
"sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86",
|
||||
"sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.8.2"
|
||||
},
|
||||
"requests": {
|
||||
"hashes": [
|
||||
"sha256:6c1246513ecd5ecd4528a0906f910e8f0f9c6b8ec72030dc9fd154dc1a6efd24",
|
||||
"sha256:b8aa58f8cf793ffd8782d3d8cb19e66ef36f7aba4353eec859e74678b01b07a7"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.26.0"
|
||||
},
|
||||
"six": {
|
||||
"hashes": [
|
||||
"sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926",
|
||||
"sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==1.16.0"
|
||||
},
|
||||
"soupsieve": {
|
||||
"hashes": [
|
||||
"sha256:1a3cca2617c6b38c0343ed661b1fa5de5637f257d4fe22bd9f1338010a1efefb",
|
||||
"sha256:b8d49b1cd4f037c7082a9683dfa1801aa2597fb11c3a1155b7a5b94829b4f1f9"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==2.3.1"
|
||||
},
|
||||
"termcolor": {
|
||||
"hashes": [
|
||||
"sha256:1d6d69ce66211143803fbc56652b41d73b4a400a2891d7bf7a1cdf4c02de613b"
|
||||
],
|
||||
"version": "==1.1.0"
|
||||
},
|
||||
"urllib3": {
|
||||
"hashes": [
|
||||
"sha256:4987c65554f7a2dbf30c18fd48778ef124af6fab771a377103da0585e2336ece",
|
||||
"sha256:c4fdf4019605b6e5423637e01bc9fe4daef873709a7973e195ceba0a62bbc844"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
|
||||
"version": "==1.26.7"
|
||||
},
|
||||
"yaspin": {
|
||||
"hashes": [
|
||||
"sha256:c8d34eca9fda3f4dfbe59f57f3cf0f3641af3eefbf1544fbeb9b3bacf82c580a",
|
||||
"sha256:d574cbfaf0a349df466c91f7f81b22460ae5ebb15ecb8bf9411d6049923aee8d"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.1.0"
|
||||
}
|
||||
},
|
||||
"develop": {
|
||||
"black": {
|
||||
"hashes": [
|
||||
"sha256:802c6c30b637b28645b7fde282ed2569c0cd777dbe493a41b6a03c1d903f99ac",
|
||||
"sha256:a042adbb18b3262faad5aff4e834ff186bb893f95ba3a8013f09de1e5569def2"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==21.11b1"
|
||||
},
|
||||
"click": {
|
||||
"hashes": [
|
||||
"sha256:353f466495adaeb40b6b5f592f9f91cb22372351c84caeb068132442a4518ef3",
|
||||
"sha256:410e932b050f5eed773c4cda94de75971c89cdb3155a72a0831139a79e5ecb5b"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==8.0.3"
|
||||
},
|
||||
"isort": {
|
||||
"hashes": [
|
||||
"sha256:6f62d78e2f89b4500b080fe3a81690850cd254227f27f75c3a0c491a1f351ba7",
|
||||
"sha256:e8443a5e7a020e9d7f97f1d7d9cd17c88bcb3bc7e218bf9cf5095fe550be2951"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==5.10.1"
|
||||
},
|
||||
"mypy-extensions": {
|
||||
"hashes": [
|
||||
"sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d",
|
||||
"sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"
|
||||
],
|
||||
"version": "==0.4.3"
|
||||
},
|
||||
"pathspec": {
|
||||
"hashes": [
|
||||
"sha256:7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a",
|
||||
"sha256:e564499435a2673d586f6b2130bb5b95f04a3ba06f81b8f895b651a3c76aabb1"
|
||||
],
|
||||
"version": "==0.9.0"
|
||||
},
|
||||
"platformdirs": {
|
||||
"hashes": [
|
||||
"sha256:367a5e80b3d04d2428ffa76d33f124cf11e8fff2acdaa9b43d545f5c7d661ef2",
|
||||
"sha256:8868bbe3c3c80d42f20156f22e7131d2fb321f5bc86a2a345375c6481a67021d"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==2.4.0"
|
||||
},
|
||||
"regex": {
|
||||
"hashes": [
|
||||
"sha256:0416f7399e918c4b0e074a0f66e5191077ee2ca32a0f99d4c187a62beb47aa05",
|
||||
"sha256:05b7d6d7e64efe309972adab77fc2af8907bb93217ec60aa9fe12a0dad35874f",
|
||||
"sha256:0617383e2fe465732af4509e61648b77cbe3aee68b6ac8c0b6fe934db90be5cc",
|
||||
"sha256:07856afef5ffcc052e7eccf3213317fbb94e4a5cd8177a2caa69c980657b3cb4",
|
||||
"sha256:0f594b96fe2e0821d026365f72ac7b4f0b487487fb3d4aaf10dd9d97d88a9737",
|
||||
"sha256:139a23d1f5d30db2cc6c7fd9c6d6497872a672db22c4ae1910be22d4f4b2068a",
|
||||
"sha256:162abfd74e88001d20cb73ceaffbfe601469923e875caf9118333b1a4aaafdc4",
|
||||
"sha256:2207ae4f64ad3af399e2d30dde66f0b36ae5c3129b52885f1bffc2f05ec505c8",
|
||||
"sha256:2409b5c9cef7054dde93a9803156b411b677affc84fca69e908b1cb2c540025d",
|
||||
"sha256:2fee3ed82a011184807d2127f1733b4f6b2ff6ec7151d83ef3477f3b96a13d03",
|
||||
"sha256:30ab804ea73972049b7a2a5c62d97687d69b5a60a67adca07eb73a0ddbc9e29f",
|
||||
"sha256:3598893bde43091ee5ca0a6ad20f08a0435e93a69255eeb5f81b85e81e329264",
|
||||
"sha256:3b5df18db1fccd66de15aa59c41e4f853b5df7550723d26aa6cb7f40e5d9da5a",
|
||||
"sha256:3c5fb32cc6077abad3bbf0323067636d93307c9fa93e072771cf9a64d1c0f3ef",
|
||||
"sha256:416c5f1a188c91e3eb41e9c8787288e707f7d2ebe66e0a6563af280d9b68478f",
|
||||
"sha256:42b50fa6666b0d50c30a990527127334d6b96dd969011e843e726a64011485da",
|
||||
"sha256:432bd15d40ed835a51617521d60d0125867f7b88acf653e4ed994a1f8e4995dc",
|
||||
"sha256:473e67837f786404570eae33c3b64a4b9635ae9f00145250851a1292f484c063",
|
||||
"sha256:4aaa4e0705ef2b73dd8e36eeb4c868f80f8393f5f4d855e94025ce7ad8525f50",
|
||||
"sha256:50a7ddf3d131dc5633dccdb51417e2d1910d25cbcf842115a3a5893509140a3a",
|
||||
"sha256:529801a0d58809b60b3531ee804d3e3be4b412c94b5d267daa3de7fadef00f49",
|
||||
"sha256:537ca6a3586931b16a85ac38c08cc48f10fc870a5b25e51794c74df843e9966d",
|
||||
"sha256:53db2c6be8a2710b359bfd3d3aa17ba38f8aa72a82309a12ae99d3c0c3dcd74d",
|
||||
"sha256:5537f71b6d646f7f5f340562ec4c77b6e1c915f8baae822ea0b7e46c1f09b733",
|
||||
"sha256:563d5f9354e15e048465061509403f68424fef37d5add3064038c2511c8f5e00",
|
||||
"sha256:5d408a642a5484b9b4d11dea15a489ea0928c7e410c7525cd892f4d04f2f617b",
|
||||
"sha256:61600a7ca4bcf78a96a68a27c2ae9389763b5b94b63943d5158f2a377e09d29a",
|
||||
"sha256:6650f16365f1924d6014d2ea770bde8555b4a39dc9576abb95e3cd1ff0263b36",
|
||||
"sha256:666abff54e474d28ff42756d94544cdfd42e2ee97065857413b72e8a2d6a6345",
|
||||
"sha256:68a067c11463de2a37157930d8b153005085e42bcb7ad9ca562d77ba7d1404e0",
|
||||
"sha256:6e1d2cc79e8dae442b3fa4a26c5794428b98f81389af90623ffcc650ce9f6732",
|
||||
"sha256:74cbeac0451f27d4f50e6e8a8f3a52ca074b5e2da9f7b505c4201a57a8ed6286",
|
||||
"sha256:780b48456a0f0ba4d390e8b5f7c661fdd218934388cde1a974010a965e200e12",
|
||||
"sha256:788aef3549f1924d5c38263104dae7395bf020a42776d5ec5ea2b0d3d85d6646",
|
||||
"sha256:7ee1227cf08b6716c85504aebc49ac827eb88fcc6e51564f010f11a406c0a667",
|
||||
"sha256:7f301b11b9d214f83ddaf689181051e7f48905568b0c7017c04c06dfd065e244",
|
||||
"sha256:83ee89483672b11f8952b158640d0c0ff02dc43d9cb1b70c1564b49abe92ce29",
|
||||
"sha256:85bfa6a5413be0ee6c5c4a663668a2cad2cbecdee367630d097d7823041bdeec",
|
||||
"sha256:9345b6f7ee578bad8e475129ed40123d265464c4cfead6c261fd60fc9de00bcf",
|
||||
"sha256:93a5051fcf5fad72de73b96f07d30bc29665697fb8ecdfbc474f3452c78adcf4",
|
||||
"sha256:962b9a917dd7ceacbe5cd424556914cb0d636001e393b43dc886ba31d2a1e449",
|
||||
"sha256:96fc32c16ea6d60d3ca7f63397bff5c75c5a562f7db6dec7d412f7c4d2e78ec0",
|
||||
"sha256:98ba568e8ae26beb726aeea2273053c717641933836568c2a0278a84987b2a1a",
|
||||
"sha256:a3feefd5e95871872673b08636f96b61ebef62971eab044f5124fb4dea39919d",
|
||||
"sha256:a955b747d620a50408b7fdf948e04359d6e762ff8a85f5775d907ceced715129",
|
||||
"sha256:b43c2b8a330a490daaef5a47ab114935002b13b3f9dc5da56d5322ff218eeadb",
|
||||
"sha256:b483c9d00a565633c87abd0aaf27eb5016de23fed952e054ecc19ce32f6a9e7e",
|
||||
"sha256:b9ed0b1e5e0759d6b7f8e2f143894b2a7f3edd313f38cf44e1e15d360e11749b",
|
||||
"sha256:ba05430e819e58544e840a68b03b28b6d328aff2e41579037e8bab7653b37d83",
|
||||
"sha256:ca49e1ab99593438b204e00f3970e7a5f70d045267051dfa6b5f4304fcfa1dbf",
|
||||
"sha256:ca5f18a75e1256ce07494e245cdb146f5a9267d3c702ebf9b65c7f8bd843431e",
|
||||
"sha256:cd410a1cbb2d297c67d8521759ab2ee3f1d66206d2e4328502a487589a2cb21b",
|
||||
"sha256:ce298e3d0c65bd03fa65ffcc6db0e2b578e8f626d468db64fdf8457731052942",
|
||||
"sha256:d5ca078bb666c4a9d1287a379fe617a6dccd18c3e8a7e6c7e1eb8974330c626a",
|
||||
"sha256:d5fd67df77bab0d3f4ea1d7afca9ef15c2ee35dfb348c7b57ffb9782a6e4db6e",
|
||||
"sha256:da1a90c1ddb7531b1d5ff1e171b4ee61f6345119be7351104b67ff413843fe94",
|
||||
"sha256:dba70f30fd81f8ce6d32ddeef37d91c8948e5d5a4c63242d16a2b2df8143aafc",
|
||||
"sha256:dc07f021ee80510f3cd3af2cad5b6a3b3a10b057521d9e6aaeb621730d320c5a",
|
||||
"sha256:dd33eb9bdcfbabab3459c9ee651d94c842bc8a05fabc95edf4ee0c15a072495e",
|
||||
"sha256:e0538c43565ee6e703d3a7c3bdfe4037a5209250e8502c98f20fea6f5fdf2965",
|
||||
"sha256:e1f54b9b4b6c53369f40028d2dd07a8c374583417ee6ec0ea304e710a20f80a0",
|
||||
"sha256:e32d2a2b02ccbef10145df9135751abea1f9f076e67a4e261b05f24b94219e36",
|
||||
"sha256:e6096b0688e6e14af6a1b10eaad86b4ff17935c49aa774eac7c95a57a4e8c296",
|
||||
"sha256:e71255ba42567d34a13c03968736c5d39bb4a97ce98188fafb27ce981115beec",
|
||||
"sha256:ed2e07c6a26ed4bea91b897ee2b0835c21716d9a469a96c3e878dc5f8c55bb23",
|
||||
"sha256:eef2afb0fd1747f33f1ee3e209bce1ed582d1896b240ccc5e2697e3275f037c7",
|
||||
"sha256:f23222527b307970e383433daec128d769ff778d9b29343fb3496472dc20dabe",
|
||||
"sha256:f341ee2df0999bfdf7a95e448075effe0db212a59387de1a70690e4acb03d4c6",
|
||||
"sha256:f5be7805e53dafe94d295399cfbe5227f39995a997f4fd8539bf3cbdc8f47ca8",
|
||||
"sha256:f7f325be2804246a75a4f45c72d4ce80d2443ab815063cdf70ee8fb2ca59ee1b",
|
||||
"sha256:f8af619e3be812a2059b212064ea7a640aff0568d972cd1b9e920837469eb3cb",
|
||||
"sha256:fa8c626d6441e2d04b6ee703ef2d1e17608ad44c7cb75258c09dd42bacdfc64b",
|
||||
"sha256:fbb9dc00e39f3e6c0ef48edee202f9520dafb233e8b51b06b8428cfcb92abd30",
|
||||
"sha256:fff55f3ce50a3ff63ec8e2a8d3dd924f1941b250b0aac3d3d42b687eeff07a8e"
|
||||
],
|
||||
"version": "==2021.11.10"
|
||||
},
|
||||
"tomli": {
|
||||
"hashes": [
|
||||
"sha256:c6ce0015eb38820eaf32b5db832dbc26deb3dd427bd5f6556cf0acac2c214fee",
|
||||
"sha256:f04066f68f5554911363063a30b108d2b5a5b1a010aa8b6132af78489fe3aade"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==1.2.2"
|
||||
},
|
||||
"typing-extensions": {
|
||||
"hashes": [
|
||||
"sha256:2cdf80e4e04866a9b3689a51869016d36db0814d84b8d8a568d22781d45d27ed",
|
||||
"sha256:829704698b22e13ec9eaf959122315eabb370b0884400e9818334d8b677023d9"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==4.0.0"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
# aoc-get
|
||||
|
||||
A small script to get the Advent of Code inputs for the day, including sample inputs
|
|
@ -0,0 +1,233 @@
|
|||
import dataclasses
|
||||
import datetime
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
from typing import List
|
||||
import random
|
||||
|
||||
import bs4
|
||||
import click
|
||||
import coloredlogs
|
||||
import dateutil.relativedelta
|
||||
import requests
|
||||
import yaspin
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class PuzzleDate:
|
||||
year: int
|
||||
day: int
|
||||
|
||||
@classmethod
|
||||
def get_latest(cls) -> "PuzzleDate":
|
||||
now = datetime.datetime.now()
|
||||
year = now.year
|
||||
day = now.day
|
||||
|
||||
# it's not AoC time yet! The last one is gonna be christmas of last year
|
||||
if now.month != 12:
|
||||
year -= 1
|
||||
day = 25
|
||||
LOG.warning(
|
||||
f"It's not Advent of Code time yet - defaulting to day {day} of the {year} event"
|
||||
)
|
||||
|
||||
return cls(year=year, day=day)
|
||||
|
||||
@classmethod
|
||||
def get_next(cls) -> "PuzzleDate":
|
||||
now = datetime.datetime.now()
|
||||
if now.month < 12:
|
||||
return cls(year=now.year, day=1)
|
||||
elif now.month == 12 and now.day >= 25:
|
||||
return cls(year=now.year + 1, day=1)
|
||||
|
||||
tomorrow_now = now + dateutil.relativedelta.relativedelta(days=1)
|
||||
return cls(year=tomorrow_now.year, day=tomorrow_now.day)
|
||||
|
||||
|
||||
def get_puzzle_url(date: PuzzleDate) -> str:
|
||||
DOMAIN = "https://adventofcode.com/"
|
||||
|
||||
return functools.reduce(
|
||||
lambda total, component: urllib.parse.urljoin(
|
||||
ensure_ends_with(total, "/"), component
|
||||
),
|
||||
(DOMAIN, str(date.year), "day", str(date.day)),
|
||||
)
|
||||
|
||||
|
||||
def get_puzzle_source(date: PuzzleDate) -> str:
|
||||
url = get_puzzle_url(date)
|
||||
LOG.debug(f"Fetching puzzle source from {url}")
|
||||
|
||||
res = requests.get(url)
|
||||
res.raise_for_status()
|
||||
|
||||
return res.text
|
||||
|
||||
|
||||
def extract_sample_inputs(puzzle_source: str) -> List[str]:
|
||||
LOG.debug("Extracting puzzle inputs from puzzle source")
|
||||
souped_puzzle_source = bs4.BeautifulSoup(puzzle_source, "html.parser")
|
||||
sample_inputs = [
|
||||
pre_tag.code.get_text()
|
||||
for pre_tag in souped_puzzle_source.find_all("pre")
|
||||
if pre_tag.code
|
||||
]
|
||||
|
||||
LOG.info(f"Found {len(sample_inputs)} sample input(s)")
|
||||
return sample_inputs
|
||||
|
||||
|
||||
def ensure_ends_with(s: str, c: str) -> str:
|
||||
if not s.endswith(c):
|
||||
return s + c
|
||||
else:
|
||||
return s
|
||||
|
||||
|
||||
def download_sample_inputs(date: PuzzleDate, output_dir: pathlib.Path):
|
||||
LOG.debug("Downloading sample inputs")
|
||||
puzzle_source = get_puzzle_source(date)
|
||||
sample_inputs = extract_sample_inputs(puzzle_source)
|
||||
for i, sample_input in enumerate(sample_inputs, start=1):
|
||||
filename = f"sample-{i}.txt"
|
||||
path = output_dir / filename
|
||||
data_to_write = ensure_ends_with(sample_input, os.linesep)
|
||||
with open(path, "w") as sample_file:
|
||||
sample_file.write(data_to_write)
|
||||
|
||||
LOG.debug(f"Wrote sample input {i} to {path}")
|
||||
|
||||
LOG.info(f"Downloaded {len(sample_inputs)} sample input(s) successfully")
|
||||
|
||||
|
||||
def fetch_input(date: PuzzleDate, token: str) -> str:
|
||||
puzzle_url = get_puzzle_url(date)
|
||||
input_url = urllib.parse.urljoin(ensure_ends_with(puzzle_url, "/"), "input")
|
||||
LOG.debug(f"Fetching puzzle input from {input_url}")
|
||||
|
||||
res = requests.get(input_url, cookies={"session": token})
|
||||
res.raise_for_status()
|
||||
return res.text
|
||||
|
||||
|
||||
def download_input(date: PuzzleDate, token: str, output_dir: pathlib.Path):
|
||||
LOG.debug("Downloading puzzle input")
|
||||
puzzle_input = fetch_input(date, token)
|
||||
data_to_write = ensure_ends_with(puzzle_input, os.linesep)
|
||||
path = output_dir / "input.txt"
|
||||
with open(path, "w") as input_file:
|
||||
input_file.write(data_to_write)
|
||||
|
||||
LOG.debug(f"Wrote puzzle input to {path}")
|
||||
LOG.info("Download puzzle inputs successfully")
|
||||
|
||||
|
||||
def get_token_from_file(path: pathlib.Path) -> str:
|
||||
with open(path) as token_file:
|
||||
token = token_file.read().strip()
|
||||
|
||||
if token.find("\n") != -1:
|
||||
raise ValueError("Token file probably does not containt token; it has newlines")
|
||||
|
||||
return token
|
||||
|
||||
|
||||
def download_latest_inputs(output_dir: pathlib.Path, token: str):
|
||||
date = PuzzleDate.get_latest()
|
||||
|
||||
step = None
|
||||
try:
|
||||
step = "download sample inputs"
|
||||
download_sample_inputs(date, output_dir)
|
||||
step = "download input"
|
||||
download_input(date, token, output_dir)
|
||||
except requests.exceptions.HTTPError as err:
|
||||
LOG.error(f"Failed to {step}: {err}")
|
||||
LOG.debug(f"Response body: {err.response.text}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def wait_for_next_puzzle():
|
||||
next_date = PuzzleDate.get_next()
|
||||
next_datetime = datetime.datetime(year=next_date.year, month=12, day=next_date.day)
|
||||
LOG.info(
|
||||
f"Next puzzle is at {next_datetime.isoformat()}. Waiting for it to be ready..."
|
||||
)
|
||||
|
||||
time_to_wait = next_datetime.timestamp() - datetime.datetime.now().timestamp()
|
||||
# Wait some extra amount of time to not be rude to eric :(
|
||||
time_to_wait += random.randint(0, 1500) / 1000
|
||||
|
||||
LOG.debug(f"Waiting {time_to_wait} seconds before fetching")
|
||||
with yaspin.yaspin(text="Waiting for the next puzzle...") as spinner:
|
||||
time.sleep(time_to_wait)
|
||||
spinner.ok("🎄 ")
|
||||
|
||||
|
||||
def setup_logs(verbose: bool):
|
||||
coloredlogs.install(
|
||||
level=logging.DEBUG if verbose else logging.INFO,
|
||||
fmt="%(asctime)s aoc-get[%(process)d] %(levelname)s %(message)s",
|
||||
)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option("-v", "--verbose", is_flag=True)
|
||||
@click.option(
|
||||
"--token", "passed_token", type=str, help="The advent of code session token."
|
||||
)
|
||||
@click.option(
|
||||
"-o",
|
||||
"output_dir",
|
||||
type=pathlib.Path,
|
||||
help="The output location of the puzzle inputs",
|
||||
default=".",
|
||||
)
|
||||
@click.option(
|
||||
"--token-file",
|
||||
"token_path",
|
||||
type=pathlib.Path,
|
||||
default="~/.local/advent-of-code/session",
|
||||
help="The location of the advent of code session token. Ignored if --token is provided.",
|
||||
)
|
||||
@click.option("--wait", is_flag=True, help="Wait until new puzzle inputs are ready")
|
||||
def main(
|
||||
verbose: bool,
|
||||
passed_token: str,
|
||||
token_path: pathlib.Path,
|
||||
output_dir: pathlib.Path,
|
||||
wait: bool,
|
||||
):
|
||||
PuzzleDate.get_next()
|
||||
token = passed_token
|
||||
if not token and not token_path:
|
||||
LOG.error("no token file given")
|
||||
sys.exit(1)
|
||||
elif not token:
|
||||
expanded_token_path = pathlib.Path(os.path.expanduser(token_path))
|
||||
LOG.debug(f"No token passed. Opening token from {token_path}")
|
||||
try:
|
||||
token = get_token_from_file(expanded_token_path)
|
||||
except (FileNotFoundError, ValueError) as err:
|
||||
LOG.error(f"Failed to read token file: {err}")
|
||||
sys.exit(1)
|
||||
|
||||
setup_logs(verbose)
|
||||
if wait:
|
||||
wait_for_next_puzzle()
|
||||
|
||||
download_latest_inputs(output_dir, token)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue