Update GeoIP Data #1
This file contains 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: Update GeoIP Data | |
on: | |
schedule: | |
- cron: '0 0 * * 0' # Runs every Sunday at midnight (UTC) | |
workflow_dispatch: | |
jobs: | |
update-geoip: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Download and process GeoIP data | |
env: | |
MAXMIND_ACCOUNT_ID: ${{ secrets.MAXMIND_ACCOUNT_ID }} | |
MAXMIND_LICENSE_KEY: ${{ secrets.MAXMIND_LICENSE_KEY }} | |
MAXMIND_DATABASE_URL: https://download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip | |
run: | | |
# Download and extract GeoLite2 database | |
curl -o geolite2.zip -J -L -u ${MAXMIND_ACCOUNT_ID}:${MAXMIND_LICENSE_KEY} ${MAXMIND_DATABASE_URL} | |
unzip -o ./geolite2.zip -d ./geolite2/ | |
# Define file paths | |
IPV4_FILE=$(find ./geolite2/ -name GeoLite2-Country-Blocks-IPv4.csv) | |
LOCATIONS_FILE=$(find ./geolite2/ -name GeoLite2-Country-Locations-en.csv) | |
OUTPUT_FILE='data/geoip2-ipv4.csv' | |
# Remove headers and unnecessary columns from IPV4_FILE | |
tail -n +2 "$IPV4_FILE" | cut -d',' -f1,2,5,6 > blocks_trimmed.csv | |
# Remove headers and extract relevant columns from LOCATIONS_FILE | |
tail -n +2 "$LOCATIONS_FILE" | cut -d',' -f1,3,4,5,6,7 > locations_trimmed.csv | |
# Sort both files by geoname_id | |
sort -t',' -k2,2 blocks_trimmed.csv > blocks_sorted.csv | |
sort -t',' -k1,1 locations_trimmed.csv > locations_sorted.csv | |
# Join the files on geoname_id | |
join -t',' -1 2 -2 1 -o 1.1,1.2,1.3,1.4,2.2,2.3,2.4,2.5,2.6 blocks_sorted.csv locations_sorted.csv > merged_unsorted.csv | |
# Add the header row to the merged file | |
echo "network,geoname_id,is_anonymous_proxy,is_satellite_provider,continent_code,continent_name,country_iso_code,country_name,is_in_european_union" > "$OUTPUT_FILE" | |
cat merged_unsorted.csv >> "$OUTPUT_FILE" | |
# Cleanup | |
rm geolite2.zip blocks_trimmed.csv locations_trimmed.csv blocks_sorted.csv locations_sorted.csv merged_unsorted.csv | |
rm -r ./geolite2/ | |
- name: Check for changes | |
id: check_changes | |
run: | | |
if git diff --quiet data/geoip2-ipv4.csv; then | |
echo "File has not changed." | |
echo "changed=false" >> $GITHUB_ENV | |
else | |
echo "File has changed." | |
echo "changed=true" >> $GITHUB_ENV | |
fi | |
- name: Commit and push changes | |
if: env.changed == 'true' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
git add data/geoip2-ipv4.csv | |
git commit -m "Update GeoIP data [skip ci]" | |
git push |