|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import requests |
| 4 | +from clabe.banks import BANK_NAMES |
| 5 | + |
| 6 | + |
| 7 | +def fetch_banxico_data(): |
| 8 | + current_date = datetime.now().strftime('%d-%m-%Y') |
| 9 | + url = f'https://www.banxico.org.mx/cep/instituciones.do?fecha={current_date}' |
| 10 | + try: |
| 11 | + response = requests.get(url) |
| 12 | + response.raise_for_status() |
| 13 | + data = response.json() |
| 14 | + return dict(data.get('instituciones', [])) |
| 15 | + except Exception as e: |
| 16 | + print(f'Error fetching data from Banxico: {e}') |
| 17 | + return {} |
| 18 | + |
| 19 | + |
| 20 | +def compare_bank_data(): |
| 21 | + current_banks = dict(BANK_NAMES) |
| 22 | + banxico_banks = fetch_banxico_data() |
| 23 | + |
| 24 | + differences = {'additions': {}, 'removals': {}, 'changes': {}} |
| 25 | + |
| 26 | + print('Comparing bank data...\n') |
| 27 | + |
| 28 | + # Check for additions (in Banxico but not in package) |
| 29 | + additions = { |
| 30 | + code: name for code, name in banxico_banks.items() if code not in current_banks |
| 31 | + } |
| 32 | + differences['additions'] = additions |
| 33 | + if additions: |
| 34 | + print('=== ADDITIONS (in Banxico but not in package) ===') |
| 35 | + for code, name in sorted(additions.items()): |
| 36 | + print(f' {code}: {name}') |
| 37 | + print() |
| 38 | + |
| 39 | + # Check for removals (in package but not in Banxico) |
| 40 | + removals = { |
| 41 | + code: name for code, name in current_banks.items() if code not in banxico_banks |
| 42 | + } |
| 43 | + differences['removals'] = removals |
| 44 | + if removals: |
| 45 | + print('=== REMOVALS (in package but not in Banxico) ===') |
| 46 | + for code, name in sorted(removals.items()): |
| 47 | + print(f' {code}: {name}') |
| 48 | + print() |
| 49 | + |
| 50 | + # Check for changes (different names for the same code) |
| 51 | + changes = { |
| 52 | + code: (current_banks[code], banxico_banks[code]) |
| 53 | + for code in set(current_banks) & set(banxico_banks) |
| 54 | + if current_banks[code].upper() != banxico_banks[code].upper() |
| 55 | + } |
| 56 | + differences['changes'] = changes |
| 57 | + if changes: |
| 58 | + print('=== CHANGES (different names for the same code): Package -> Banxico ===') |
| 59 | + for code, (current_name, banxico_name) in sorted(changes.items()): |
| 60 | + print(f' {code}: {current_name} -> {banxico_name}') |
| 61 | + print() |
| 62 | + |
| 63 | + if not additions and not removals and not changes: |
| 64 | + print('No differences found. The data is in sync.') |
| 65 | + |
| 66 | + return differences |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + differences = compare_bank_data() |
0 commit comments