from __future__ import annotations import json import tomllib import typing import httpx def main() -> None: with open('config.toml', 'rb') as f: config = tomllib.load(f) fio_api_key = config['fio_api_key'] with open('www/data/knownCompanies.json', 'r') as f: companies = json.load(f) all_companies: typing.Collection[FIOCompany] = httpx.get('https://rest.fnar.net/company/all', headers={'Authorization': fio_api_key}).raise_for_status().json() try: for company in all_companies: username = company['UserName'] if username is None: print(company['CompanyId'], 'has no username:', company) continue known_company = {'Username': username} if corp := company.get('CorporationCode'): known_company['Corporation'] = corp if known_company == companies.get(company['CompanyId']): print(username, 'had no change') else: print(known_company) companies[company['CompanyId']] = known_company finally: with open('www/data/knownCompanies.json', 'w') as f: json.dump(companies, f) class FIOCompany(typing.TypedDict): CompanyId: str UserName: str | None CorporationCode: str | None if __name__ == '__main__': main()