Release v4.18994 - Backdoor
[RRG-proxmark3.git] / client / update_amiibo_tools_lua.py
blob8a31b9d60bbee442cbad75193cafd67eb9ab66ea
1 #!/usr/bin/env python3
3 """
4 -----------------------------------------------------------------------------
5 Name: update_amiibo_tools_lua.py
7 Author: Cory Solovewicz
9 Description:
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
21 Dependencies:
22 python3 -m pip install jq
24 How to run:
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 -----------------------------------------------------------------------------
31 """
33 import os
34 import re
35 import subprocess
36 import json
37 from jq import jq
39 def fetch_data():
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())
48 return None
49 return output
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)
58 return filtered_data
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):
67 # Open the file
68 with open(filename, 'r', encoding='utf-8') as file:
69 data = file.read()
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)
81 # Prepend the text
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
85 # Append the text
86 append_text = '\n\nreturn amiibo_tools\n'
87 data = data + append_text
89 return data
91 def write_to_file(data, filename):
92 # Write the output
93 with open(filename, 'w', encoding='utf-8') as file:
94 file.write(data)
95 print(f"Output written to {filename}")
97 def delete_file(filename):
98 try:
99 os.remove(filename)
100 print(f"Temporary file {filename} deleted")
101 except OSError as e:
102 print(f"Error deleting file {filename}: ", e)
104 def main():
105 data = fetch_data()
106 if data:
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__":
114 main()