diff --git a/.gitignore b/.gitignore index 3b3cc6b..b7a3def 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ demo_planet.html demo_edsm.html js/ed3dplanet.js json_samples/eddb.json -json_samples/edsm.json \ No newline at end of file +json_samples/edsm.json +json_samples/factions.json +.vscode \ No newline at end of file diff --git a/demo_factions.html b/demo_factions.html new file mode 100644 index 0000000..d5f5d60 --- /dev/null +++ b/demo_factions.html @@ -0,0 +1,94 @@ + + + + + + + + Milky Way + + + + + + + + +

Milky Way

+ +
+ +
+ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/json_samples/generator.py b/json_samples/generator.py new file mode 100644 index 0000000..fbcae48 --- /dev/null +++ b/json_samples/generator.py @@ -0,0 +1,100 @@ +import os +import sys +import time +import json +import aiohttp +import asyncio +import aiofiles + +# Lock for file writing +file_lock = asyncio.Lock() + +async def clear_json(filename='json_samples/factions.json'): + async with aiofiles.open(filename, 'w') as file: + Government = { + "1": { + "name": "Communist Systems", + "color": "CC0000" + }, + "2": { + "name": "Federal Systems", + "color": "0000EF" + } + } + file_data = {"categories": {"Government": Government}, "systems": []} + await file.write(json.dumps(file_data, indent=4)) + +async def write_json(new_data, filename='json_samples/factions.json'): + async with file_lock: + async with aiofiles.open(filename, 'r+') as file: + file_data = json.loads(await file.read()) + file_data["systems"].append(new_data) + await file.seek(0) + await file.write(json.dumps(file_data, indent=4)) + +async def getEDBGSData(session, url, pageNumber): + async with session.get(url + str(pageNumber)) as response: + if response.status == 200: + data = await response.json() + rawSystemData = data['docs'] + for rawSystemDataArray in rawSystemData: + systemName = rawSystemDataArray['name'] + coords = { + "x": rawSystemDataArray['x'], + "y": rawSystemDataArray['y'], + "z": rawSystemDataArray['z'] + } + government = rawSystemDataArray['government'] + allegiance = rawSystemDataArray['allegiance'] + if government == "$government_communism;": + cat = [1] + elif allegiance == "federation": + cat = [2] + else: + continue + + systemDict = {"name": systemName, "coords": coords, "cat": cat} + await write_json(systemDict) + if data['hasNextPage']: + nextPage = data['nextPage'] + pageNumber += 1 + await getEDBGSData(session, url, pageNumber) + return True + else: + return False + +async def process_allegiance(url, allegiance): + print(f"{allegiance} SYSTEMS STARTED AT {time.strftime('%X')}") + start_time = time.time() + + async with aiohttp.ClientSession() as session: + page_number = 1 + await getEDBGSData(session, url, page_number) + + elapsed_time = time.time() - start_time + minutes, seconds = divmod(elapsed_time, 60) + print(f"{allegiance} SYSTEMS COMPLETED AT {time.strftime('%X')} (Duration: {int(minutes):02d} minutes {seconds:.2f} seconds)", flush=True) + +async def main(): + await clear_json() + sys.setrecursionlimit(3000) + + allegiance_urls = [ + ("https://elitebgs.app/api/ebgs/v5/systems?allegiance=empire&page=", "IMPERIAL"), + ("https://elitebgs.app/api/ebgs/v5/systems?allegiance=federation&page=", "FEDERAL"), + ("https://elitebgs.app/api/ebgs/v5/systems?allegiance=alliance&page=", "ALLIED"), + ("https://elitebgs.app/api/ebgs/v5/systems?allegiance=independent&page=", "INDEPENDENT"), + ] + + total_start_time = time.time() # Record the start time for total duration + + await asyncio.gather(*(process_allegiance(url, allegiance) for url, allegiance in allegiance_urls)) + + total_elapsed_time = time.time() - total_start_time + total_minutes, total_seconds = divmod(total_elapsed_time, 60) + print(f"TOTAL DURATION: {int(total_minutes):02d} minutes {total_seconds:.2f} seconds") + + sys.setrecursionlimit(1000) + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/json_samples/routes.json b/json_samples/routes.json index 2d534de..b75a84e 100644 --- a/json_samples/routes.json +++ b/json_samples/routes.json @@ -1,88 +1,88 @@ -{ - "systems": [ - { - "name": "Checkpoint 1", - "coords": { - "x": "-6644", - "y": "-21", - "z": "11738" - } - }, - { - "name": "Checkpoint 2", - "coords": { - "x": "-10534", - "y": "-21", - "z": "16384" - } - }, - { - "name": "Checkpoint 3", - "coords": { - "x": "-4969", - "y": "-21", - "z": "22262" - } - }, - { - "name": "Checkpoint 4", - "coords": { - "x": "3553", - "y": "-21", - "z": "8732" - } - }, - { - "name": "Checkpoint 5", - "coords": { - "x": "6297", - "y": "-21", - "z": "14626" - } - }, - { - "name": "Checkpoint 6", - "coords": { - "x": "18975", - "y": "-21", - "z": "25676" - } - }, - { - "name": "Sagittarius A*", - "coords": { - "x": "25", - "y": "-21", - "z": "25900" - } - } - ], - "routes": [ - { - "title":"1st test route", - "points": [ - { - "s":"Test", - "label":"My home", - "coords": { - "x": "25", - "y": "-2100", - "z": "-3900" - } - }, - {"s":"Checkpoint 1"}, - {"s":"Checkpoint 2","label":"Great discovery here"}, - {"s":"Checkpoint 3"}, - {"s":"Sagittarius A*","label":"Center of the galaxy"} - ] - }, - { - "title":"2nd test route", - "points": [ - {"s":"Checkpoint 4"}, - {"s":"Checkpoint 5"}, - {"s":"Checkpoint 6"} - ] - } - ] +{ + "systems": [ + { + "name": "Checkpoint 1", + "coords": { + "x": "-6644", + "y": "-21", + "z": "11738" + } + }, + { + "name": "Checkpoint 2", + "coords": { + "x": "-10534", + "y": "-21", + "z": "16384" + } + }, + { + "name": "Checkpoint 3", + "coords": { + "x": "-4969", + "y": "-21", + "z": "22262" + } + }, + { + "name": "Checkpoint 4", + "coords": { + "x": "3553", + "y": "-21", + "z": "8732" + } + }, + { + "name": "Checkpoint 5", + "coords": { + "x": "6297", + "y": "-21", + "z": "14626" + } + }, + { + "name": "Checkpoint 6", + "coords": { + "x": "18975", + "y": "-21", + "z": "25676" + } + }, + { + "name": "Sagittarius A*", + "coords": { + "x": "25", + "y": "-21", + "z": "25900" + } + } + ], + "routes": [ + { + "title":"1st test route", + "points": [ + { + "s":"Test", + "label":"My home", + "coords": { + "x": "25", + "y": "-2100", + "z": "-3900" + } + }, + {"s":"Checkpoint 1"}, + {"s":"Checkpoint 2","label":"Great discovery here"}, + {"s":"Checkpoint 3"}, + {"s":"Sagittarius A*","label":"Center of the galaxy"} + ] + }, + { + "title":"2nd test route", + "points": [ + {"s":"Checkpoint 4"}, + {"s":"Checkpoint 5"}, + {"s":"Checkpoint 6"} + ] + } + ] } \ No newline at end of file