Last active
October 29, 2019 15:31
-
-
Save jbs1/d57226e0edf422f98517b9399493c570 to your computer and use it in GitHub Desktop.
Eve Online: Manufacturing PI Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import requests | |
import bz2 | |
import sqlite3 | |
from math import ceil | |
import argparse | |
def main(): | |
parser =argparse.ArgumentParser(description='''\ | |
Takes a json file and returns the total number of PI needed to produce everything in the JSON input file. | |
No structure/skill/blueprint bonuses are takes into account. Downloads SDE from "fuzzwork" as needed.''', formatter_class=argparse.RawTextHelpFormatter) | |
parser.add_argument('-i', '--input', default='input.json', help='''\ | |
name/path to the JSON input file (default: input.json) | |
format (see example file): [{"typeid": <tid>,"amount": <amount>},...] | |
multiple of the same type IDs may exist)''') | |
args = parser.parse_args() | |
pi_filename = args.input | |
sqlite_filename = "sqlite-latest.sqlite" | |
sqlite_zipfilename = sqlite_filename+".bz2" | |
sqlite_url = "https://www.fuzzwork.co.uk/dump/sqlite-latest.sqlite.bz2" | |
#adjustment_ratio = 1/4 | |
print("IMPORT",pi_filename) | |
with open(pi_filename,"r") as pi_json: | |
rawpi = json.load(pi_json) | |
pi_num_dict = {} | |
for item in rawpi: | |
if item["typeid"] not in pi_num_dict: | |
pi_num_dict[item["typeid"]]=item["amount"] | |
else: | |
pi_num_dict[item["typeid"]]+=item["amount"] | |
if not os.path.isfile(sqlite_filename): | |
print("DOWNLOAD",sqlite_zipfilename) | |
with open(sqlite_zipfilename,"wb") as sqlite_zipfile: | |
response = requests.get(sqlite_url, stream=True) | |
downloaded = 0 | |
count = 0 | |
for data in response.iter_content(chunk_size=None): | |
downloaded += len(data) | |
count += 1 | |
sqlite_zipfile.write(data) | |
if count >= 1000: | |
count = 0 | |
print("Downloaded {} bytes".format(downloaded)) | |
print("DECOMPRESS", sqlite_zipfilename) | |
with open(sqlite_filename,"wb") as sqlite_file: | |
with bz2.open(sqlite_zipfilename,"rb") as sqlite_zipfile: | |
sqlite_file.write(sqlite_zipfile.read()) | |
os.remove(sqlite_zipfilename) | |
print("OPEN",sqlite_filename) | |
conn = sqlite3.connect(sqlite_filename) | |
c = conn.cursor() | |
pi_dict = {} | |
for item_tid in pi_num_dict: | |
single_pi = get_pi(c, item_tid, pi_num_dict[item_tid]) | |
for item in single_pi: | |
if item not in pi_dict: | |
pi_dict[item]=single_pi[item] | |
else: | |
pi_dict[item]+=single_pi[item] | |
print("{0:=<35s}\n|{1:^33s}|\n{0:=<35s}".format("",'Result:')) | |
pi_final_dict = [] | |
for pi_tid in pi_dict: | |
pi_final_dict.append({"typeID": pi_tid, "typeName": get_typeid_name(c, pi_tid), "amount": pi_dict[pi_tid]}) | |
for pi_single in pi_final_dict: | |
print("{0:<27s}{1:>8d}".format(pi_single["typeName"],pi_single["amount"])) | |
with open("result.json", "w") as result_json: | |
json.dump(pi_final_dict, result_json, indent=2) | |
def get_blueprint_typeid_quantity(dbc, typeid): | |
dbc.execute("SELECT typeID,quantity FROM industryActivityProducts WHERE productTypeID = {}".format(typeid)) | |
ret = dbc.fetchone() | |
if ret: #meta mods don't have bp's | |
return ret | |
else: | |
return ret | |
def get_typeid_name(dbc, typeid): | |
dbc.execute("SELECT typeName FROM invTypes WHERE typeID = {}".format(typeid)) | |
return dbc.fetchone()[0] | |
def get_pi(dbc, typeid, multiplier): | |
try: | |
bpctid,quantity = get_blueprint_typeid_quantity(dbc,typeid) | |
pi_str = "(" | |
for pitid in get_pi_tids(dbc): | |
pi_str += str(pitid) + "," | |
pi_str = pi_str[:-1] + ")" | |
dbc.execute("SELECT materialTypeID,quantity FROM industryActivityMaterials WHERE activityID = 1 and typeID = {} AND materialTypeID IN {}".format(bpctid,pi_str)) | |
pi_dict = {} | |
for tid in dbc: | |
if tid[0] not in pi_dict: | |
pi_dict[tid[0]]=tid[1]*ceil(multiplier/quantity) | |
else: | |
pi_dict[tid[0]]+=tid[1]*ceil(multiplier/quantity) | |
except TypeError: | |
return {} | |
else: | |
return pi_dict | |
def get_pi_tids(dbc): | |
#parent group planetary materials 1332 | |
dbc.execute("SELECT marketGroupID FROM invMarketGroups WHERE hasTypes = 1 and parentGroupID = 1332") | |
pitid_string = "SELECT typeID FROM invTypes WHERE marketGroupID in (" | |
for row in dbc: | |
pitid_string += str(row[0]) + "," | |
pitid_string = pitid_string[:-1] + ")" | |
dbc.execute(pitid_string) | |
pitids = [] | |
for tid in dbc: | |
pitids.append(tid[0]) | |
return pitids | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"typeid": 28668, | |
"amount": 240 | |
}, | |
{ | |
"typeid": 2613, | |
"amount": 15 | |
}, | |
{ | |
"typeid": 28668, | |
"amount": 100 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment