Skip to content

Commit

Permalink
Add GitHub Actions workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-maltsev committed Aug 3, 2024
1 parent ae2c4b7 commit 8f6965e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Generate courses

on:
workflow_dispatch:
push:
branches:
- main
schedule:
- cron: '0 8 * * *'

jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v4
with:
ref: main
path: main
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Generate courses
run: |
python -u ./main/courses_to_json.py last-3 './gh-pages/courses_{year}_{semester}.json'
- name: Deploy
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .
git diff-index --quiet --cached HEAD || git commit -m "Update courses"
git push
41 changes: 37 additions & 4 deletions courses_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ def send_request(query: str):
delay = min(delay * 2, 300)


def get_last_semesters(semester_count: int):
params = {
"sap-client": "700",
"$select": ",".join(
[
"PiqYear",
"PiqSession",
]
),
# "$inlinecount": "allpages",
}
raw_data = send_request(f"SemesterSet?{urllib.parse.urlencode(params)}")
raw_results = raw_data["d"]["results"]

results = []
for result in raw_results:
year = int(result["PiqYear"])
semester = int(result["PiqSession"])
if semester not in [200, 201, 202]:
continue

results.append((year, semester))

return sorted(results, reverse=True)[:semester_count]


def get_sap_course_numbers(year: int, semester: int):
params = {
"sap-client": "700",
Expand Down Expand Up @@ -531,12 +557,19 @@ def main():
if len(year_and_semester) != 2:
raise RuntimeError(f"Invalid year_and_semester: {year_and_semester}")

year = int(year_and_semester[0])
semester = int(year_and_semester[1])

start = time.time()

run(year, semester, Path(args.output_file))
if year_and_semester[0] == "last":
semester_count = int(year_and_semester[1])
for year, semester in get_last_semesters(semester_count):
print(f"Getting courses for year: {year}, semester: {semester}")
output_file = args.output_file.format(year=year, semester=semester)
run(year, semester, output_file)
else:
year = int(year_and_semester[0])
semester = int(year_and_semester[1])
output_file = Path(args.output_file)
run(year, semester, output_file)

end = time.time()
print(f"Completed in {(end - start) / 60:.2f} minutes")
Expand Down

0 comments on commit 8f6965e

Please sign in to comment.