edsmAround/edsmAround.py

97 lines
3.2 KiB
Python

import requests as r
import re
import json
from datetime import datetime as dt
from math import floor
#IMPORTANT BITS
###This is the name of the sector you want to search into (Do not use caps!)
sectorName = "Cleeque"
### "weights" to query, beware of big results when searching for lighter systems in more dense area
weightsToQuery = ["h","g","f"]
### don't touch any of this. Or do, I'm a comment, not a cop.
postFixList =["AA-A","BA-A","CL-Y","DL-Y","EG-Y","FG-Y","YE-A","ZE-A"]
###Call to EDSM API, don't run this script too much in a short timeframe.
def apiCall(systemName):
systems = r.get("https://www.edsm.net/api-v1/systems", params={'systemName' : systemName})
return systems
# takes a sector name, a postfix and a weight, returns list of known systems in EDSM matching that
def fetchEdsmSystems(sectorName, postFix, weight):
systemList = []
pattern = re.compile(f".*{postFix} {weight}.*")
for system in json.loads(apiCall(sectorName +" "+postFix).content):
if pattern.match(system['name']):
systemList.append(system['name'])
return systemList
#given a list of systems (assumed to be all the same weight and postfix) returns the maximum value of the integer value in the system name for that list
def maxSystemValue(systemList):
currentMax = 0
pattern = re.compile(f".*[A-Z][A-Z]-[A-Z] [A-Za-z]([0-9]+)")
for system in systemList:
val = 0
match = pattern.match(system)
val = int(match.group(1))
if val > currentMax:
currentMax = val
return currentMax
#dict of dict of systems keyed by postfix, keyed by weight ( dict["AA-A"]["g"] gives back all the AA-A GXXX systems)
sysWeightDict = {}
#dict of dict of systems maximum value by postfix, keyed by weight ( dict["AA-A"]["g"] gives back the maximum recorded value for X where X is the integer in a system name : AA-A GX)
sysMaxDict = {}
#dict of dict of all the systems in a sector keyed by postfix, keyed by weight, used later for substracting EDSM values from ALL systems
sysCandidates = {}
#for each weight class, we initialize our dicts with our postfix list
for weight in weightsToQuery:
sysWeightDict[weight] = {}
sysMaxDict[weight] = {}
sysCandidates[weight] = {}
#then we populate our dicts with the provided postfix
for postfix in postFixList:
sysWeightDict[weight][postfix] = fetchEdsmSystems(sectorName, postfix, weight) # this is reversed from IG "AA-A G"
sysMaxDict[weight][postfix] = maxSystemValue(sysWeightDict[weight][postfix])
#now we generate the inverse of the known system list
# for all systems indexes to the max known index
sysCandidates[weight][postfix] = []
for i in range(sysMaxDict[weight][postfix]):
candidate = f"{sectorName} {postfix} {weight}{i}"
if candidate not in sysWeightDict[weight][postfix]:
sysCandidates[weight][postfix].append(candidate)
print("These are the systems known to EDSM")
print(sysMaxDict)
#Finally writes the results to a JSON file.
with open(f"{floor(dt.now().timestamp())}-{sectorName}-RESULTS-v2.json",'w') as f:
f.write(json.dumps(sysCandidates, indent=4))