Skip to content

Commit

Permalink
Change to rolling static updates, fix scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Jan 30, 2024
1 parent c51b8ab commit 32ff5e4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
9 changes: 7 additions & 2 deletions flask_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,26 @@ def project_menorah_settings() -> str:
@app.route("/recent", methods=["GET"])
def recent() -> str:
"""Route for recent GitHub activity."""
with open("assets/contrib/recent.json", encoding="utf-8") as respfile:
datetime_fmt = "%Y%m%d%H"
current_datetime = datetime.datetime.now(dateutil.tz.gettz())
current_datetime_str = current_datetime.strftime(datetime_fmt)
with open(
f"assets/contrib/recent_{current_datetime_str}.json", encoding="utf-8"
) as respfile:
contents = json.load(respfile)
contributions, repos = contents["contributionsCollection"], contents["repositories"]
end_datetime = dateutil.parser.parse(contributions["endedAt"])
start_datetime = dateutil.parser.parse(contributions["startedAt"])
diff_datetime: datetime.timedelta = end_datetime - start_datetime
oldest_push = dateutil.parser.parse(repos["nodes"][-1]["pushedAt"])
current_datetime = datetime.datetime.now(dateutil.tz.UTC)
diff_oldest = current_datetime - oldest_push
return render_template(
"recent.html",
repos=repos["nodes"],
num_contributions=contributions["contributionCalendar"]["totalContributions"],
duration_days=diff_datetime.days,
diff_oldest=math.ceil(diff_oldest.days / 365),
current_datetime=current_datetime_str,
)


Expand Down
2 changes: 1 addition & 1 deletion flask_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def consolidate_sorted_jobs(jobs: list[JobDict]) -> list[list[JobDict]]:
grouped_jobs_dict[employer][-1]["startDate"], "%m/%Y"
)
if job["endDate"] == "current":
end_role = datetime.datetime.now(dateutil.tz.UTC)
end_role = datetime.datetime.now(dateutil.tz.gettz())
else:
end_role = datetime.datetime.strptime(job["endDate"], "%m/%Y")

Expand Down
2 changes: 1 addition & 1 deletion flask_app/templates/recent.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{% endif %}
<div class="col-md-6">
<div class="card recent-load-in" style="--delay: {{ loop.index0 * 0.2 + 0.2}}s">
<a href="{{ repo.url }}"><img src="/static/img/gh_cards/card{{ loop.index0 }}.png" class="card-img-top"/></a>
<a href="{{ repo.url }}"><img src="/static/img/gh_cards/{{ current_datetime }}/card{{ loop.index0 }}.png" class="card-img-top"/></a>
<div class="card-body" style="background-color: white;">
{% if repo.languages.nodes|length == 0 %}
<span class="badge badge-other-language">Other</span>
Expand Down
18 changes: 15 additions & 3 deletions scripts/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@

"""Download the images from the GraphQL query to the static folder."""

import datetime
import json
import pathlib
import sys

import dateutil.tz
import requests

URL = "https://api.github.com/graphql"
DATETIME_FMT = "%Y%m%d%H"

current_datetime = datetime.datetime.now(dateutil.tz.gettz())
current_datetime_str = current_datetime.strftime(DATETIME_FMT)

next_datetime = current_datetime + datetime.timedelta(minutes=10)
next_datetime_str = next_datetime.strftime(DATETIME_FMT)

base_dir = pathlib.Path(sys.argv[1])

resp_dir = base_dir / "assets/contrib/"
card_dir = base_dir / "flask_app/static/img/gh_cards/"
new_resp_file = resp_dir / ("recent_" + next_datetime_str + ".json")
parent_card_dir = base_dir / "flask_app/static/img/gh_cards/"
new_card_dir = parent_card_dir / next_datetime_str
resp_dir.mkdir(exist_ok=True)
card_dir.mkdir(exist_ok=True)
parent_card_dir.mkdir(exist_ok=True)
new_card_dir.mkdir(exist_ok=True)

with open("/etc/config.json", encoding="utf-8") as jsonfile:
config = json.load(jsonfile)
Expand Down Expand Up @@ -44,6 +56,6 @@
img_resp = requests.get(node["openGraphImageUrl"], timeout=5)
status_okay = 200
if img_resp.status_code == status_okay:
with open(str(card_dir / f"card{index}.png"), mode="wb") as imgfile:
with open(str(new_card_dir / f"card{index}.png"), mode="wb") as imgfile:
for data_chunk in img_resp:
imgfile.write(data_chunk)
30 changes: 30 additions & 0 deletions scripts/post_graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: 2024 Alec Delaney
# SPDX-License-Identifier: MIT

"""Delete the images from previous GraphQL query from the static folder."""

import datetime
import pathlib
import sys

import dateutil.tz

DATETIME_FMT = "%Y%m%d%H"

current_datetime = datetime.datetime.now(dateutil.tz.gettz())
current_datetime_str = current_datetime.strftime(DATETIME_FMT)

last_datetime = current_datetime - datetime.timedelta(minutes=10)
last_datetime_str = last_datetime.strftime(DATETIME_FMT)

base_dir = pathlib.Path(sys.argv[1])

resp_dir = base_dir / "assets/contrib/"
old_resp_file = resp_dir / ("recent_" + last_datetime_str + ".json")
parent_card_dir = base_dir / "flask_app/static/img/gh_cards/"
old_card_dir = parent_card_dir / last_datetime_str

for card in old_card_dir.glob("*"):
card.unlink(missing_ok=True)
old_card_dir.unlink(missing_ok=True)
old_resp_file.unlink(missing_ok=True)
16 changes: 12 additions & 4 deletions scripts/schedule_cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
# SPDX-License-Identifier: MIT

REPOPATH=$(realpath .)
SCRIPTPATH="$REPOPATH/scripts/graphql.py"
COMMAND="55 23 * * * python $SCRIPTPATH $REPOPATH"
JOBNAME=$(echo "$REPOPATH" | xargs basename)

cronberry enter "Cache cards for $JOBNAME" "$COMMAND" --overwrite
PYTHONBIN="$REPOPATH/.venv/bin/python"

UPDATESCRIPTPATH="$REPOPATH/scripts/graphql.py"
UPDATECOMMAND="55 * * * * $PYTHONBIN $UPDATESCRIPTPATH $REPOPATH"
UPDATEJOBNAME=$(echo "$REPOPATH" | xargs basename)

DELETESCRIPTPATH="$REPOPATH/scripts/post_graphql.py"
DELETECOMMAND="5 * * * * $PYTHONBIN $DELETESCRIPTPATH $REPOPATH"
DELETEJOBNAME=$(echo "$REPOPATH" | xargs basename)

cronberry enter "Cache new cards for $UPDATEJOBNAME" "$UPDATECOMMAND" --overwrite
cronberry enter "Delete old cards for $DELETEJOBNAME" "$DELETECOMMAND" --overwrite
3 changes: 2 additions & 1 deletion scripts/update_cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT

REPOPATH=$(realpath .)
PATH="$REPOPATH/.venv/bin:$PATH"
SCRIPTPATH="$REPOPATH/scripts/graphql.py"

/usr/bin/python3.12 "$SCRIPTPATH" "$REPOPATH"
python "$SCRIPTPATH" "$REPOPATH"

0 comments on commit 32ff5e4

Please sign in to comment.