copy_empire.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import sys
  2. import typing
  3. import httpx
  4. def main():
  5. from_jwt, empire_uuid, to_jwt = sys.argv[1:]
  6. with httpx.Client(headers={'Authorization': 'Bearer ' + from_jwt}) as from_client:
  7. empire: typing.Sequence[EmpirePlan] = from_client.get('https://api.prunplanner.org/baseplanner/empire/' + empire_uuid).raise_for_status().json()
  8. shared: typing.Mapping[str, str] = {s['plan_uuid']: s['shared_uuid']
  9. for s in from_client.get('https://api.prunplanner.org/shared/list').raise_for_status().json()}
  10. for plan in empire:
  11. if plan['uuid'] not in shared:
  12. print('sharing', plan['uuid'], plan['name'])
  13. from_client.put('https://api.prunplanner.org/shared/baseplanner/' + plan['uuid']).raise_for_status()
  14. # refresh shared UUIDs
  15. shared: typing.Mapping[str, str] = {s['plan_uuid']: s['shared_uuid']
  16. for s in from_client.get('https://api.prunplanner.org/shared/list').raise_for_status().json()}
  17. to_client = httpx.Client(headers={'Authorization': 'Bearer ' + to_jwt})
  18. for plan in empire:
  19. print('cloning', plan['uuid'], plan['name'])
  20. shared_uuid = shared[plan['uuid']]
  21. to_client.put(f'https://api.prunplanner.org/shared/baseplanner/{shared_uuid}/clone').raise_for_status()
  22. cloned_plan, = (p for p in to_client.get('https://api.prunplanner.org/baseplanner/').raise_for_status().json() if p['name'].startswith('(Shared) '))
  23. rename_plan(to_client, cloned_plan['uuid'], plan['name'])
  24. def rename_plan(client: httpx.Client, uuid: str, new_name: str) -> None:
  25. plan = client.get('https://api.prunplanner.org/baseplanner/' + uuid).raise_for_status().json()
  26. plan['name'] = new_name
  27. plan.update(plan.pop('baseplanner_data'))
  28. del plan['empires']
  29. client.patch('https://api.prunplanner.org/baseplanner/' + uuid, json=plan).raise_for_status()
  30. class EmpirePlan(typing.TypedDict):
  31. uuid: str
  32. name: str
  33. if __name__ == '__main__':
  34. main()