diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..8ca6682 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,114 @@ +name: Check Website URLs + +on: + schedule: + # Runs daily at 03:00 UTC + - cron: '0 3 * * *' + workflow_dispatch: # Allows manual triggering + +permissions: + contents: write # Needed to commit changes back to the repo + +jobs: + check-urls: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + # Fetch full history to be able to compare changes if needed (optional) + fetch-depth: 0 + + - name: Install jq + run: sudo apt-get update && sudo apt-get install -y jq + + - name: Check URLs and update modflix.json + id: check_urls + run: | + echo "Starting URL check process..." + cp modflix.json modflix.json.bak # Backup original file + updated_json=$(cat modflix.json) + changes_made=false + + # Iterate over each key in the JSON object + for key in $(echo "$updated_json" | jq -r 'keys_unsorted[]'); do + original_url=$(echo "$updated_json" | jq -r --arg k "$key" '.[$k].url') + echo "Checking key: $key, URL: $original_url" + + # Use curl to follow redirects (-L), get final URL (-w '%{url_effective}'), + # suppress output (-s), discard body (-o /dev/null), get status code (-w '%{http_code}') + # Set timeouts to prevent hanging + response=$(curl -Lso /dev/null -w "%{http_code} %{url_effective}" --max-time 15 --connect-timeout 7 "$original_url") + http_code=$(echo "$response" | awk '{print $1}') + final_url=$(echo "$response" | awk '{print $2}') + + echo " -> Status: $http_code, Final URL: $final_url" + + # Check if it was a redirect (3xx) and the final URL is different + if [[ "$http_code" =~ ^3[0-9]{2}$ ]] && [[ "$original_url" != "$final_url" ]]; then + echo " -> Redirect detected. Original: $original_url, New Effective: $final_url" + + # Extract the scheme and host from the final URL + new_scheme_host=$(echo "$final_url" | sed -E 's|^(https?://[^/]+).*|\1|') + + # Extract the path, query, and fragment from the original URL + # This preserves things like trailing slashes or specific paths + original_path_etc=$(echo "$original_url" | sed -E 's|https?://[^/]+(.*)|\1|') + if [[ -z "$original_path_etc" ]]; then + # Handle cases where the original URL might just be the domain (e.g., https://example.com) + # Check if the original URL ended with a slash + if [[ "$original_url" == */ ]]; then + original_path_etc="/" + else + # If final URL has a path and original didn't, maybe use final's path? + # For now, keeping it simple: if original had no path, new one has no path unless original ended with / + : # Keep original_path_etc empty or "/" based on above check + fi + fi + + + # Construct the new URL using the new scheme/host and original path/query/fragment + new_url="${new_scheme_host}${original_path_etc}" + echo " -> Constructing new URL: $new_url" + + # Update the JSON content in the variable + updated_json=$(echo "$updated_json" | jq --arg k "$key" --arg nu "$new_url" '.[$k].url = $nu') + changes_made=true + echo " -> Updated JSON for key $key" + elif [[ "$http_code" == "200" ]]; then + echo " -> URL OK (200)." + else + echo " -> URL check failed or returned non-redirect/non-200 status ($http_code). No changes made for this URL." + fi + done + + if $changes_made; then + echo "Changes were made. Writing updated modflix.json" + echo "$updated_json" | jq '.' > modflix.json # Write updated content back, pretty-printed + # Set output for the next step + echo "changes_detected=true" >> $GITHUB_OUTPUT + else + echo "No changes detected in URLs." + # Restore original file to avoid timestamp changes if only formatting changed + mv modflix.json.bak modflix.json + echo "changes_detected=false" >> $GITHUB_OUTPUT + fi + + - name: Commit and push changes + if: steps.check_urls.outputs.changes_detected == 'true' + run: | + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + git add modflix.json + # Check if there are staged changes before committing + if ! git diff --staged --quiet; then + git commit -m "chore: Update redirected URLs in modflix.json + + Automatically updated URLs based on HTTP redirects." + git push + echo "Changes committed and pushed." + else + echo "No effective changes to commit after writing file (might be formatting only)." + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Use the default token