Skip to content
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

Add the possibility to have one badge for multiple years #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ You need to add this session id as [encrypted secret](https://docs.github.com/en

Now you can set up the workflow. The sample workflow beyond will help you.

## Handle multiple years
### Multiple badges
If you want to set up badges for multiple years in one repository just add this action multiple times (once for each year using the `year` input).
Have slightly different badges for each year with a custom regex using the regex inputs.
The day badge probably doesn't makes sense for multiple years as it only depends on the current date (and therefore only works in December).

### One badge
If you want to have a custom badge to group multiple years in one repository just add all the years, separated by a comma, inside the `year:` optional input.

## Sample Workflow

```yml
Expand All @@ -67,7 +72,7 @@ jobs:

# Optional inputs:
#
# year: 2021 # The year for which stats should be retrieved
# year: 2021 # The year for which stats should be retrieved. Write 20XX,20XX to combine multiple years.
# leaderboard: 'https://adventofcode.com/2020/leaderboard/private/view/00000.json' # The url of the leaderboard from witch the data is fetched. Typically your private leaderboard.
# file: 'README.md' # The file that contains the badges
# dayRegex: '(?<=https:\/\/img\.shields\.io\/badge\/day%20📅-)[0-9]+(?=-blue)' # Regular expression that finds the content of the day badge in your file.
Expand Down
80 changes: 43 additions & 37 deletions aoc-badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,66 @@
import pytz

# environment variables
year = os.getenv('INPUT_YEAR')
years_array = os.getenv('INPUT_YEAR')
leaderboard = os.getenv('INPUT_LEADERBOARD')
session = os.getenv('INPUT_SESSION')
readme = os.getenv('INPUT_FILE')
userid = os.getenv('INPUT_USERID')
day_regex = os.getenv('INPUT_DAYREGEX')
stars_regex = os.getenv('INPUT_STARSREGEX')
days_completed_regex = os.getenv('INPUT_DAYSCOMPLETEDREGEX')
if year is None or not year:
year = date.today().year
if years_array is None or not years_array:
years = date.today().year
else:
try:
year = int(year)
years = list(map(int,years_array.split(',')))
except ValueError:
print('year input is not an integer')
exit(1)
if leaderboard is None or not leaderboard:
leaderboard = f'https://adventofcode.com/{year}/leaderboard/private/view/{userid}.json'

# fetch stars
cookie = {'session': session}
print('Fetching leaderboard data from : ' + leaderboard)
r = requests.get(leaderboard, cookies=cookie)
if r.status_code != 200:
print(f'Leaderboard API returned status code {r.status_code}: {r.text}')
exit(1)
try:
data = json.loads(r.text)
except json.JSONDecodeError as err:
print('Could not parse leaderboard json. Is the leaderboard url correct & your session code valid?')
print(err)
exit(1)
# noinspection PyUnboundLocalVariable
stars = data['members'][userid]['stars']

# completed days
stars = 0
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
for year in years:
if leaderboard is None or not leaderboard:
leaderboard = f'https://adventofcode.com/{year}/leaderboard/private/view/{userid}.json'

# fetch stars
cookie = {'session': session}
print('Fetching leaderboard data from : ' + leaderboard)
r = requests.get(leaderboard, cookies=cookie)
if r.status_code != 200:
print(f'Leaderboard API returned status code {r.status_code}: {r.text}')
exit(1)
try:
data = json.loads(r.text)
except json.JSONDecodeError as err:
print('Could not parse leaderboard json. Is the leaderboard url correct & your session code valid?')
print(err)
exit(1)
# noinspection PyUnboundLocalVariable
stars += data['members'][userid]['stars']

# Set the timezone to New York
new_york_tz = pytz.timezone('America/New_York')
# completed days
for day in data['members'][userid]['completion_day_level']:
if '2' in data['members'][userid]['completion_day_level'][day]:
days_completed += 1

# Get the current time in New York
today = datetime.now(new_york_tz).date()
# Set the timezone to New York
new_york_tz = pytz.timezone('America/New_York')

# Your existing logic to determine the day
if today < datetime(year, 12, 1, tzinfo=new_york_tz).date():
day = 0
elif today > datetime(year, 12, 31, tzinfo=new_york_tz).date():
day = 24
else:
day = today.day
# Get the current time in New York
today = datetime.now(new_york_tz).date()

# Your existing logic to determine the day
if today < datetime(year, 12, 1, tzinfo=new_york_tz).date():
day = 0
elif today > datetime(year, 12, 31, tzinfo=new_york_tz).date():
day = 24
else:
day = today.day

# Reset the leaderboard for the next year iteration
leaderboard = None

print(f'Day: {day}')
print(f'Stars: {stars}')
Expand Down