5
5
# ]
6
6
# ///
7
7
8
- import requests
9
- import re
10
- import luadata
11
8
import json
12
9
import os
10
+ import re
11
+
12
+ import luadata
13
+ import requests
14
+
13
15
14
16
def download_and_parse_data ():
15
17
# Download the data
16
18
url = "https://gist.githubusercontent.com/Bilka2/6b8a6a9e4a4ec779573ad703d03c1ae7/raw"
17
19
response = requests .get (url )
18
20
if response .status_code != 200 :
19
21
raise Exception (f"Failed to download data: { response .status_code } " )
20
-
22
+
21
23
# Get the content
22
24
content = response .text
23
25
24
26
# Clean up the content to make it valid Lua
25
27
# Remove the script header if present
26
- content = re .sub (r' ^Script.*?: ' , '' , content )
27
-
28
+ content = re .sub (r" ^Script.*?: " , "" , content )
29
+
28
30
# Remove Lua multi-line comments
29
- content = re .sub (r' --\[=\[.*?\]=\]' , '' , content , flags = re .DOTALL )
30
-
31
+ content = re .sub (r" --\[=\[.*?\]=\]" , "" , content , flags = re .DOTALL )
32
+
31
33
# Convert scientific notation to decimal notation
32
34
def convert_scientific (match ):
33
35
num = float (match .group (0 ))
34
- return f"{ num :.10f} " .rstrip ('0' ).rstrip ('.' )
35
-
36
- content = re .sub (r' -?\d+\.?\d*[eE][+-]?\d+' , convert_scientific , content )
37
-
36
+ return f"{ num :.10f} " .rstrip ("0" ).rstrip ("." )
37
+
38
+ content = re .sub (r" -?\d+\.?\d*[eE][+-]?\d+" , convert_scientific , content )
39
+
38
40
# Remove any trailing commas in arrays/tables
39
- content = re .sub (r' ,(\s*[}\]])' , r'\1' , content )
40
-
41
+ content = re .sub (r" ,(\s*[}\]])" , r"\1" , content )
42
+
41
43
# Ensure proper line endings
42
- content = content .replace (' \r \n ' , ' \n ' )
43
-
44
+ content = content .replace (" \r \n " , " \n " )
45
+
44
46
try :
45
47
a = luadata .unserialize (content , encoding = "utf-8" , multival = False )
46
48
return a
47
49
except Exception as e :
48
- print (f"Error position in content: { str ( e ) } " )
50
+ print (f"Error position in content: { e } " )
49
51
# Save problematic content to a file for inspection
50
- with open (' debug_lua_content.txt' , 'w' ) as f :
52
+ with open (" debug_lua_content.txt" , "w" ) as f :
51
53
f .write (content )
52
54
raise
53
55
56
+
54
57
def remove_layers_recursively (data ):
55
58
if isinstance (data , dict ):
56
59
# Remove 'layers' and 'sprites' keys if present
57
- data .pop (' layers' , None )
58
- data .pop (' sprites' , None )
60
+ data .pop (" layers" , None )
61
+ data .pop (" sprites" , None )
59
62
# Recursively process all values
60
63
for value in data .values ():
61
64
remove_layers_recursively (value )
@@ -65,115 +68,110 @@ def remove_layers_recursively(data):
65
68
remove_layers_recursively (item )
66
69
return data
67
70
71
+
68
72
if __name__ == "__main__" :
69
73
data = download_and_parse_data ()
70
- data .pop ('achievement' , None )
71
- data .pop ('technology' , None )
72
- data .pop ('tips-and-tricks-item' , None )
73
- data .pop ('rail-planner' , None )
74
-
75
- recipe_data = {
76
- 'recipe' : data .pop ('recipe' , {}),
77
- 'recipe-category' : data .pop ('recipe-category' , {})
78
- }
74
+ data .pop ("achievement" , None )
75
+ data .pop ("technology" , None )
76
+ data .pop ("tips-and-tricks-item" , None )
77
+ data .pop ("rail-planner" , None )
78
+
79
+ recipe_data = {"recipe" : data .pop ("recipe" , {}), "recipe-category" : data .pop ("recipe-category" , {})}
79
80
80
81
item_data = {
81
- ' item' : data .pop (' item' , {}),
82
- ' item-group' : data .pop (' item-group' , {}),
83
- ' item-subgroup' : data .pop (' item-subgroup' , {}),
82
+ " item" : data .pop (" item" , {}),
83
+ " item-group" : data .pop (" item-group" , {}),
84
+ " item-subgroup" : data .pop (" item-subgroup" , {}),
84
85
}
85
86
86
87
filtered_keys = [
87
- ' accumulator' ,
88
- ' agricultural-tower' ,
89
- ' arithmetic-combinator' ,
90
- ' assembling-machine' ,
91
- ' asteroid-collector' ,
92
- ' beacon' ,
93
- ' boiler' ,
94
- ' burner-generator' ,
95
- ' cargo-bay' ,
96
- ' cargo-landing-pad' ,
97
- ' constant-combinator' ,
98
- ' container' ,
99
- ' curved-rail-a' ,
100
- ' curved-rail-b' ,
101
- ' decider-combinator' ,
102
- ' electric-pole' ,
103
- ' electric-turret' ,
104
- ' elevated-curved-rail-a' ,
105
- ' elevated-curved-rail-b' ,
106
- ' elevated-half-diagonal-rail' ,
107
- ' elevated-straight-rail' ,
108
- ' fluid-turret' ,
109
- ' furnace' ,
110
- ' fusion-generator' ,
111
- ' fusion-reactor' ,
112
- ' gate' ,
113
- ' generator' ,
114
- ' half-diagonal-rail' ,
115
- ' heat-interface' , # TODO: check if this is needed
116
- ' heat-pipe' ,
117
- ' highlight-box' ,
118
- ' inserter' ,
119
- ' lab' ,
120
- ' lamp' ,
121
- ' lightning-attractor' ,
122
- ' logistic-container' ,
123
- ' mining-drill' ,
124
- ' offshore-pump' ,
125
- ' pipe' ,
126
- ' pipe-to-ground' ,
127
- ' power-switch' ,
128
- ' programmable-speaker' ,
129
- ' pump' ,
130
- ' radar' ,
131
- ' rail-chain-signal' ,
132
- ' rail-ramp' ,
133
- ' rail-remnants' , # TODO: check if this is needed
134
- ' rail-signal' ,
135
- ' rail-support' ,
136
- ' reactor' ,
137
- ' roboport' ,
138
- ' rocket-silo' ,
139
- ' selector-combinator' ,
140
- ' solar-panel' ,
141
- ' splitter' ,
142
- ' storage-tank' ,
143
- ' straight-rail' ,
144
- ' thruster' ,
145
- ' train-stop' ,
146
- ' transport-belt' ,
147
- ' underground-belt' ,
148
- ' wall'
88
+ " accumulator" ,
89
+ " agricultural-tower" ,
90
+ " arithmetic-combinator" ,
91
+ " assembling-machine" ,
92
+ " asteroid-collector" ,
93
+ " beacon" ,
94
+ " boiler" ,
95
+ " burner-generator" ,
96
+ " cargo-bay" ,
97
+ " cargo-landing-pad" ,
98
+ " constant-combinator" ,
99
+ " container" ,
100
+ " curved-rail-a" ,
101
+ " curved-rail-b" ,
102
+ " decider-combinator" ,
103
+ " electric-pole" ,
104
+ " electric-turret" ,
105
+ " elevated-curved-rail-a" ,
106
+ " elevated-curved-rail-b" ,
107
+ " elevated-half-diagonal-rail" ,
108
+ " elevated-straight-rail" ,
109
+ " fluid-turret" ,
110
+ " furnace" ,
111
+ " fusion-generator" ,
112
+ " fusion-reactor" ,
113
+ " gate" ,
114
+ " generator" ,
115
+ " half-diagonal-rail" ,
116
+ " heat-interface" , # TODO: check if this is needed
117
+ " heat-pipe" ,
118
+ " highlight-box" ,
119
+ " inserter" ,
120
+ " lab" ,
121
+ " lamp" ,
122
+ " lightning-attractor" ,
123
+ " logistic-container" ,
124
+ " mining-drill" ,
125
+ " offshore-pump" ,
126
+ " pipe" ,
127
+ " pipe-to-ground" ,
128
+ " power-switch" ,
129
+ " programmable-speaker" ,
130
+ " pump" ,
131
+ " radar" ,
132
+ " rail-chain-signal" ,
133
+ " rail-ramp" ,
134
+ " rail-remnants" , # TODO: check if this is needed
135
+ " rail-signal" ,
136
+ " rail-support" ,
137
+ " reactor" ,
138
+ " roboport" ,
139
+ " rocket-silo" ,
140
+ " selector-combinator" ,
141
+ " solar-panel" ,
142
+ " splitter" ,
143
+ " storage-tank" ,
144
+ " straight-rail" ,
145
+ " thruster" ,
146
+ " train-stop" ,
147
+ " transport-belt" ,
148
+ " underground-belt" ,
149
+ " wall" ,
149
150
]
150
151
filtered_data = {key : data [key ] for key in filtered_keys }
151
152
152
153
filtered_data = data
153
154
154
155
filtered_data = remove_layers_recursively (filtered_data )
155
-
156
-
157
-
156
+
158
157
# Create factorio_data directory if it doesn't exist
159
- os .makedirs (' factorio_data' , exist_ok = True )
160
-
158
+ os .makedirs (" factorio_data" , exist_ok = True )
159
+
161
160
# Extract recipe-related data
162
-
163
161
164
162
print (data .keys ())
165
-
163
+
166
164
# Save recipe data to a separate file
167
- with open (' factorio_data/recipe_data.json' , 'w' , encoding = ' utf-8' ) as f :
165
+ with open (" factorio_data/recipe_data.json" , "w" , encoding = " utf-8" ) as f :
168
166
json .dump (recipe_data , f , indent = 2 , ensure_ascii = False )
169
- print (f "Recipe data saved to factorio_data/recipe_data.json" )
167
+ print ("Recipe data saved to factorio_data/recipe_data.json" )
170
168
171
169
# Save item data to a separate file
172
- with open (' factorio_data/item_data.json' , 'w' , encoding = ' utf-8' ) as f :
170
+ with open (" factorio_data/item_data.json" , "w" , encoding = " utf-8" ) as f :
173
171
json .dump (item_data , f , indent = 2 , ensure_ascii = False )
174
- print (f "Item data saved to factorio_data/item_data.json" )
175
-
172
+ print ("Item data saved to factorio_data/item_data.json" )
173
+
176
174
# Save remaining data
177
- with open (' factorio_data/factorio_data.json' , 'w' , encoding = ' utf-8' ) as f :
175
+ with open (" factorio_data/factorio_data.json" , "w" , encoding = " utf-8" ) as f :
178
176
json .dump (filtered_data , f , indent = 2 , ensure_ascii = False )
179
- print (f "Main data saved to factorio_data/factorio_data.json" )
177
+ print ("Main data saved to factorio_data/factorio_data.json" )
0 commit comments