|
|
@@ -33,33 +33,15 @@ def main() -> None:
|
|
|
|
|
|
all_users: typing.Collection[str] = httpx.get('https://rest.fnar.net/user/allusers',
|
|
|
headers={'Authorization': fio_api_key}).raise_for_status().json()
|
|
|
-
|
|
|
try:
|
|
|
for username in all_users:
|
|
|
- if username.casefold() in cached:
|
|
|
- continue
|
|
|
-
|
|
|
- response = get_with_retry('https://rest.fnar.net/user/' + username)
|
|
|
- if response.status_code in (204, 404):
|
|
|
- print(username, 'not found')
|
|
|
- continue
|
|
|
- fio_user = response.json()
|
|
|
- assert username.casefold() == fio_user['UserName'].casefold(), f'{username} != {fio_user["UserName"]}'
|
|
|
- known_user = {'Username': username, 'LastUpdated': now}
|
|
|
- if corp := fio_user.get('CorporationCode'):
|
|
|
- known_user['Corporation'] = corp
|
|
|
- if known_user == companies.get(fio_user['CompanyId']):
|
|
|
- print(username, 'had no change')
|
|
|
- else:
|
|
|
- print(known_user)
|
|
|
- companies[fio_user['CompanyId']] = known_user
|
|
|
+ update_company_from_fio(companies, cached, now, username)
|
|
|
finally:
|
|
|
with open('www/data/knownCompanies.json', 'w') as f:
|
|
|
json.dump(companies, f)
|
|
|
|
|
|
unknown_companies = company_ids - companies.keys()
|
|
|
print(len(unknown_companies), 'unknown companies')
|
|
|
-
|
|
|
try:
|
|
|
for batch in itertools.batched(unknown_companies, 10):
|
|
|
company_data: typing.Collection[FIOCompany | None] = httpx.get('https://api.fnar.net/company/lookup',
|
|
|
@@ -77,6 +59,37 @@ def main() -> None:
|
|
|
with open('www/data/knownCompanies.json', 'w') as f:
|
|
|
json.dump(companies, f)
|
|
|
|
|
|
+ fio_rest_users = frozenset(user.casefold() for user in all_users)
|
|
|
+ known_usernames = frozenset(co['Username'].casefold() for co in companies.values() if co['Username'] is not None)
|
|
|
+ users_without_fio_accounts = known_usernames - fio_rest_users
|
|
|
+ print(len(users_without_fio_accounts), 'users without FIO REST accounts')
|
|
|
+ try:
|
|
|
+ for username in users_without_fio_accounts:
|
|
|
+ update_company_from_fio(companies, cached, now, username)
|
|
|
+ finally:
|
|
|
+ with open('www/data/knownCompanies.json', 'w') as f:
|
|
|
+ json.dump(companies, f)
|
|
|
+
|
|
|
+def update_company_from_fio(companies: dict[str, dict[str, typing.Any]], cached: typing.AbstractSet[str], now: int,
|
|
|
+ username: str) -> None:
|
|
|
+ if username.casefold() in cached:
|
|
|
+ return
|
|
|
+
|
|
|
+ response = get_with_retry('https://rest.fnar.net/user/' + username)
|
|
|
+ if response.status_code in (204, 404):
|
|
|
+ print(username, 'not found')
|
|
|
+ return
|
|
|
+ fio_user = response.json()
|
|
|
+ assert username.casefold() == fio_user['UserName'].casefold(), f'{username} != {fio_user["UserName"]}'
|
|
|
+ known_user = {'Username': username, 'LastUpdated': now}
|
|
|
+ if corp := fio_user.get('CorporationCode'):
|
|
|
+ known_user['Corporation'] = corp
|
|
|
+ if known_user == companies.get(fio_user['CompanyId']):
|
|
|
+ print(username, 'had no change')
|
|
|
+ else:
|
|
|
+ print(known_user)
|
|
|
+ companies[fio_user['CompanyId']] = known_user
|
|
|
+
|
|
|
def get_with_retry(url) -> httpx.Response:
|
|
|
for attempt in range(10):
|
|
|
try:
|