Auto Update Data #612
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Update Data | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # Runs daily at midnight UTC | |
| workflow_dispatch: # Allows manual trigger | |
| jobs: | |
| update-data: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| statuses: write | |
| checks: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.13' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r scraper/requirements.txt | |
| - name: Calculate current term and run update | |
| id: check | |
| run: | | |
| # Calculate current term | |
| echo "::group::Calculating academic term" | |
| CURRENT_TERM=$(date +%Y%m | awk '{y=int(substr($1,1,4)); m=int(substr($1,5,2)); printf("%d%02d", m>=8?y:y-1, m<=4?2:(m>=8?1:3))}') | |
| echo "Current term: $CURRENT_TERM" | |
| echo "::endgroup::" | |
| # Get current version number | |
| echo "::group::Getting current version" | |
| CURRENT_VERSION=$(ls data-v*.min.json | grep -o '[0-9]\+') | |
| NEXT_VERSION=$((CURRENT_VERSION + 1)) | |
| echo "Current version: $CURRENT_VERSION" | |
| echo "Next version will be: $NEXT_VERSION" | |
| echo "::endgroup::" | |
| echo "::group::Running scraper" | |
| echo "Running scraper/scrape.py with term $CURRENT_TERM" | |
| # Run scraping script with term argument | |
| python scraper/scrape.py $CURRENT_TERM | |
| echo "::endgroup::" | |
| echo "::group::Checking for changes" | |
| # Check if there are any changes | |
| if cmp -s data.min.json "data-v${CURRENT_VERSION}.min.json"; then | |
| echo "No changes detected in scraped data" | |
| rm data.min.json | |
| echo "Removed temporary data.min.json" | |
| echo "changes=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "Changes detected in scraped data" | |
| echo "Moving data.min.json to data-v${NEXT_VERSION}.min.json" | |
| mv data.min.json "data-v${NEXT_VERSION}.min.json" | |
| echo "Removing old data-v${CURRENT_VERSION}.min.json" | |
| git rm -f "data-v${CURRENT_VERSION}.min.json" | |
| echo "Updating JavaScript files" | |
| # Update version and term in JavaScript files | |
| sed -i "s/dataVersion:\([[:space:]]*\)[0-9]\+/dataVersion:\1${NEXT_VERSION}/g" js/suchedule*.js | |
| sed -i "s/term:\([[:space:]]*\)\(.\)[0-9]\+\(.\)/term:\1\2${CURRENT_TERM}\3/g" js/suchedule*.js | |
| echo "changes=true" >> $GITHUB_OUTPUT | |
| echo "version=$NEXT_VERSION" >> $GITHUB_OUTPUT | |
| fi | |
| echo "::endgroup::" | |
| - name: Create Pull Request | |
| if: steps.check.outputs.changes == 'true' | |
| uses: peter-evans/create-pull-request@v5 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "chore: update data to v${{ steps.check.outputs.version }}" | |
| title: "Data Update to v${{ steps.check.outputs.version }}" | |
| body: | | |
| Automated data update | |
| - Updated data file to version ${{ steps.check.outputs.version }} | |
| - Last updated: ${{ github.event.repository.updated_at }} | |
| This PR was automatically generated by GitHub Actions. | |
| branch: auto-update-data | |
| base: master | |
| delete-branch: true |