4 -----------------------------------------------------------------------------
5 Name: update_amiibo_tools_lua.py
7 Author: Cory Solovewicz
10 This is a python script to automate what the updating of the amiibo_tools.lua
11 file which holds a lua table of all known amiibos. Previously updating the
12 amiibo_tools.lua was a very manual process.
14 This script automates the following original command:
15 curl https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/database/amiibo.json | jq 'del(.amiibos[].release)' | jq 'del(.characters)' | pbcopy --> transform to table
16 And outputs the formatted file as amiibo_tools.lua
17 If everything goes well, this should be an updated copy of amiibo_tools.lua
18 which can then be placed in the /lualibs/ directory.
19 The temporary amiibo.json file is then deleted
22 python3 -m pip install jq
25 python update_amiibo_tools_lua.py
26 The script will create the file amiibo_tools.lua
28 After running, manually backup the original /lualibs/amiibo_tools.lua and move the
29 updated amiibo_tools.lua to the /lualibs/ directory.
30 -----------------------------------------------------------------------------
40 print("Fetching amiibo.json")
41 # Perform the curl command
42 curl_command
= "curl https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/database/amiibo.json"
43 curl_process
= subprocess
.Popen(curl_command
.split(), stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
44 output
, error
= curl_process
.communicate()
46 if curl_process
.returncode
!= 0:
47 print("Error fetching data: ", error
.decode())
51 def filter_data(data
):
52 print("Filtering downloaded data")
53 # Convert the output to JSON and use jq to filter data
54 data_json
= json
.loads(data
)
55 filtered_data_json
= jq('del(.amiibos[].release) | del(.characters)').transform(data_json
)
56 # Convert the filtered JSON data back to a string, preserving Unicode characters
57 filtered_data
= json
.dumps(filtered_data_json
, indent
=2, ensure_ascii
=False)
60 def save_data(filtered_data
, filename
):
61 # Save filtered data to file
62 with
open(filename
, 'w', encoding
='utf-8') as file:
63 file.write(filtered_data
)
64 print(f
"Data saved to {filename}")
66 def process_file(filename
):
68 with
open(filename
, 'r', encoding
='utf-8') as file:
71 # Perform the replacements
72 data
= data
.replace('"name"', 'name')
73 data
= data
.replace('"amiibo_series"', 'amiibo_series')
74 data
= data
.replace('"amiibos"', 'amiibos')
75 data
= data
.replace('"game_series"', 'game_series')
76 data
= data
.replace('"types"', 'types')
77 data
= data
.replace(':', ' =')
78 data
= re
.sub('"0x', '["0x', data
)
79 data
= re
.sub('" =', '"] =', data
)
82 prepend_text
= 'local amiibo_tools = {}\n\n-- curl https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/database/amiibo.json | jq \'del(.amiibos[].release)\' | jq \'del(.characters)\' | pbcopy --> transform to table\namiibo_tools.db =\n'
83 data
= prepend_text
+ data
86 append_text
= '\n\nreturn amiibo_tools\n'
87 data
= data
+ append_text
91 def write_to_file(data
, filename
):
93 with
open(filename
, 'w', encoding
='utf-8') as file:
95 print(f
"Output written to {filename}")
97 def delete_file(filename
):
100 print(f
"Temporary file {filename} deleted")
102 print(f
"Error deleting file {filename}: ", e
)
107 filtered_data
= filter_data(data
)
108 save_data(filtered_data
, 'amiibo.json')
109 processed_data
= process_file('amiibo.json')
110 write_to_file(processed_data
, 'amiibo_tools.lua')
111 delete_file('amiibo.json')
113 if __name__
== "__main__":