respect-validation/bin/update-postal-codes
Alexandre Gomes Gaigalas ce9608d0a8 Auto update postal code list
- For this particular updater, a list of exceptions to the rules
   downloaded by geonames is included in POSTAL_CODES_EXTRA, for
   cases in which we seem to do better than geonames itself based
   on previous user reports.
 - Added an option to also validate formatting of the postal codes.
 - Combined multiple PR bots into a single one.
2023-02-19 00:19:10 -03:00

49 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# Usage: {script}
# Update the list of currency codes
set -euo pipefail
declare -r IFS=$'\n'
declare -r LIST_URL="https://download.geonames.org/export/dump/countryInfo.txt"
declare -r LIST_FILENAME=$(mktemp)
declare -r RULE_FILENAME=$(dirname "${BASH_SOURCE}")/../library/Rules/PostalCode.php
declare -r RULE_FILENAME_TEMPORARY=$(mktemp)
echo "- Downloading list"
curl --silent --location "${LIST_URL}" --output "${LIST_FILENAME}"
declare -r CURRENCY_CODES_COUNT=$(grep "<CcyNtry>" "${LIST_FILENAME}" | wc --lines)
echo "- Creating temporary file"
{
sed -n "/^</,/private const POSTAL_CODES = \[/p" "${RULE_FILENAME}"
echo ' // phpcs:disable Generic.Files.LineLength.TooLong'
cat "$LIST_FILENAME" |
sed '/^#/d' |
sed '/^$/d' |
cut -f1,14,15 |
sort -u | while read -r country_postal_code
do
country_code="${country_postal_code%% *}"
country_postal="${country_postal_code#$country_code }"
country_format="${country_postal%% *}"
country_regex="${country_postal#$country_format }"
country_regex="${country_regex%% }"
country_format="$(echo "$country_format" | sed 's/#/\\d/g' | sed 's/@/\\w/g')"
if test -n "$country_regex"
then
echo " '$country_code' => ['/^$country_format$/', '/$country_regex/'],"
fi
done
echo ' // phpcs:disable Generic.Files.LineLength.TooLong'
sed --silent '/^ \];\/\/end/,/^}/p' "${RULE_FILENAME}"
} > "${RULE_FILENAME_TEMPORARY}"
echo "- Updating content of '$(basename ${RULE_FILENAME})'"
mv "${RULE_FILENAME_TEMPORARY}" "${RULE_FILENAME}"
echo "Finished!"