-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
125 lines (89 loc) · 3.37 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import glob
from os import path
import json
import random
import pyrebase
import requests
from typing import Generator, Tuple
from pyrebase.pyrebase import Database
from blueprintString import decode_blueprint_string
from config import Config
def connect_database() -> Database:
config = {
"authDomain": "facorio-blueprints.firebaseapp.com",
"databaseURL": "https://facorio-blueprints.firebaseio.com",
"apiKey": "AIzaSyAcZJ7hGfxYKhkGHJwAnsLS3z5Tg9kWw2s",
"storageBucket": "facorio-blueprints.appspot.com"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
return db
def download_blueprint_summaries():
db = connect_database()
summaries_response = db.child("blueprintSummaries").get()
summaries = {}
for s in summaries_response.each():
summaries[s.key()] = s.val()
with open(Config.RawSummariesFName, "w") as f:
json.dump(summaries, f)
def download_blueprints():
db = connect_database()
blueprint_response = db.child("blueprints").get()
bps = {}
for b in blueprint_response.each():
bps[b.key()] = b.val()
with open(Config.RawBlueprintDataFName, "w") as f:
json.dump(bps, f)
def load_raw_blueprints() -> dict:
with open(Config.RawBlueprintDataFName) as f:
return json.load(f)
def get_factorio_prints_tags() -> dict:
r = requests.get(Config.TagsUrl)
return json.loads(r.text)
def filter_mod_tags(raw_factorioprints_data: dict) -> dict:
tags: dict = get_factorio_prints_tags()
tag_key = Config.FactorioPrintsTagKey
base_mod_tag = Config.FactorioPrintsModTag
mod_tag_strings = [f"/{base_mod_tag}/{t}/" for t in tags[base_mod_tag] if t != "vanilla"]
filtered_blueprints = {}
for fp_key, raw_fp_data in raw_factorioprints_data.items():
if tag_key in raw_fp_data and any(tag in raw_fp_data[tag_key] for tag in mod_tag_strings):
continue
filtered_blueprints[fp_key] = raw_fp_data
return filtered_blueprints
def store_blueprints(factorioprints_data: dict, max_blueprint_size=1000000):
for k, v in factorioprints_data.items():
if len(v["blueprintString"]) > max_blueprint_size:
continue
try:
blueprint = decode_blueprint_string(v["blueprintString"])
with open(f"{path.join(Config.BlueprintsDir, k)}.json", "w") as f:
json.dump(blueprint, f)
except:
print(f"Failed to store blueprint for key {k}.")
def load_blueprints(key: str) -> dict:
fpath = path.join(Config.BlueprintsDir, f"{key}.json")
if path.isfile(fpath):
with open(fpath) as f:
data: dict = json.load(f)
return data
raise FileNotFoundError()
def iter_stored_blueprints(seed: int = None) -> Generator[Tuple[str, dict], None, None]:
paths = glob.glob(path.join(Config.BlueprintsDir, "*.json"))
if seed is not None:
random.seed = seed
random.shuffle(paths)
for bp_path in paths:
base = path.basename(bp_path)
key: str = path.splitext(base)[0]
with open(bp_path) as f:
data: dict = json.load(f)
yield key, data
if __name__ == "__main__":
# download_blueprints()
raw = load_raw_blueprints()
filtered = filter_mod_tags(raw)
store_blueprints(filtered)
for k, d in iter_stored_blueprints():
print(k, d)
break