From 44575a1826b5e3e8bec80b7b4999284ee5257d2b Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Mon, 5 May 2025 18:02:45 +0200 Subject: Allow reading IBAN registry from the command line It seems that the Swift website currently uses TLS fingerprinting to block downloads of the IBAN registry except in certain browsers. It also fixes an idiosyncrasy in the IBAN registry iteself where the Norwegian BBAN format string was not correct. --- update/iban.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'update') diff --git a/update/iban.py b/update/iban.py index 3e954e6..d74984a 100755 --- a/update/iban.py +++ b/update/iban.py @@ -24,6 +24,8 @@ Financial Telecommunication which is the official IBAN registrar) to get the data needed to correctly parse and validate IBANs.""" import csv +import os.path +import sys from collections import defaultdict import requests @@ -31,6 +33,7 @@ import requests # The place where the current version of # swift_standards_infopaper_ibanregistry_1.txt can be downloaded. +# Linked from https://www.swift.com/standards/data-standards/iban-international-bank-account-number download_url = 'https://www.swift.com/node/11971' @@ -44,13 +47,20 @@ def get_country_codes(line): if __name__ == '__main__': - response = requests.get(download_url, timeout=30) - response.raise_for_status() - print('# generated from swift_standards_infopaper_ibanregistry_1.txt,') - print('# downloaded from %s' % download_url) + if len(sys.argv) > 1: + f = open(sys.argv[1], 'rt', encoding='iso-8859-15') + lines = iter(f) + print(f'# generated from {os.path.basename(sys.argv[1])}') + print(f'# downloaded from {download_url}') + else: + response = requests.get(download_url, timeout=30) + response.raise_for_status() + print('# generated from iban-registry_1.txt') + print(f'# downloaded from {download_url}') + lines = response.iter_lines(decode_unicode=True) values = defaultdict(dict) # the file is CSV but the data is in columns instead of rows - for row in csv.reader(response.iter_lines(decode_unicode=True), delimiter='\t', quotechar='"'): + for row in csv.reader(lines, delimiter='\t', quotechar='"'): # skip first row if row and row[0] != 'Data element': # first column contains label @@ -66,6 +76,8 @@ if __name__ == '__main__': cname = data['Name of country'] if bban.startswith(cc + '2!n'): bban = bban[5:] + if bban.startswith(cc): + bban = bban[2:] # print country line print('%s country="%s" bban="%s"' % (cc, cname, bban)) # TODO: some countries have a fixed check digit value -- cgit v1.2.3