8000 Allow reading IBAN registry from the command line · zipus/python-stdnum@44575a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 44575a1

Browse files
committed
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.
1 parent 44c2355 commit 44575a1

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

update/iban.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
the data needed to correctly parse and validate IBANs."""
2525

2626
import csv
27+
import os.path
28+
import sys
2729
from collections import defaultdict
2830

2931
import requests
3032

3133

3234
# The place where the current version of
3335
# swift_standards_infopaper_ibanregistry_1.txt can be downloaded.
36+
# Linked from https://www.swift.com/standards/data-standards/iban-international-bank-account-number
3437
download_url = 'https://www.swift.com/node/11971'
3538

3639

@@ -44,13 +47,20 @@ def get_country_codes(line):
4447

4548

4649
if __name__ == '__main__':
47-
response = requests.get(download_url, timeout=30)
48-
response.raise_for_status()
49-
print('# generated from swift_standards_infopaper_ibanregistry_1.txt,')
50-
print('# downloaded from %s' % download_url)
50+
if len(sys.argv) > 1:
51+
f = open(sys.argv[1], 'rt', encoding='iso-8859-15')
52+
lines = iter(f)
53+
print(f'# generated from {os.path.basename(sys.argv[1])}')
54+
print(f'# downloaded from {download_url}')
55+
else:
56+
response = requests.get(download_url, timeout=30)
57+
response.raise_for_status()
58+
print('# generated from iban-registry_1.txt')
59+
print(f'# downloaded from {download_url}')
60+
lines = response.iter_lines(decode_unicode=True)
5161
values = defaultdict(dict)
5262
# the file is CSV but the data is in columns instead of rows
53-
for row in csv.reader(response.iter_lines(decode_unicode=True), delimiter='\t', quotechar='"'):
63+
for row in csv.reader(lines, delimiter='\t', quotechar='"'):
5464
# skip first row
5565
if row and row[0] != 'Data element':
5666
# first column contains label
@@ -66,6 +76,8 @@ def get_country_codes(line):
6676
cname = data['Name of country']
6777
if bban.startswith(cc + '2!n'):
6878
bban = bban[5:]
79+
if bban.startswith(cc):
80+
bban = bban[2:]
6981
# print country line
7082
print('%s country="%s" bban="%s"' % (cc, cname, bban))
7183
# TODO: some countries have a fixed check digit value

0 commit comments

Comments
 (0)
0