-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path8bitdo-firmware.py
executable file
·84 lines (58 loc) · 2.07 KB
/
8bitdo-firmware.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
#!/usr/bin/python3
# (c) 2025 Florian 'floe' Echtler <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
# based on https://ladis.cloud/blog/posts/firmware-update-8bitdo.html
import requests, json, sys, os
import urllib.request
baseurl = "http://dl.8bitdo.com:8080"
products = {}
def help():
print("Usage: 8bitdo-firmware.py ...\n")
print("\t-l\t\tlist all available devices")
print("\t-l [num]\tlist all firmware versions for device [num]")
print("\t-f [num] [ver]\tfetch firmware version [ver] for device [num]\n")
exit(0)
print("8BitDo Firmware Fetcher v0.0.1\n")
if len(sys.argv) == 1 or sys.argv[1] in ["-?", "-h", "--help"]:
help()
response = requests.post(baseurl + "/firmware/select", headers={"Beta": "1"})
result = response.json()
for item in result["list"]:
num = item["type"]
if not num in products:
products[num] = []
products[num].append(item)
if sys.argv[1] == "-l":
if len(sys.argv) == 2:
for num, item in products.items():
print(f"{num}:\t{item[0]["fileName"]}")
else:
num = int(sys.argv[2])
if num not in products:
print("... device number not found.\n")
exit(1)
fws = products[num]
print(f"Firmware versions for {fws[0]["fileName"]} (#{num}):\n")
for fw in fws:
ver = str(fw["version"])
beta = " (beta)" if fw["beta"] != "" else ""
print(f"{ver[0:4]} (build {ver[4:]})" + beta)
print("")
exit(0)
if sys.argv[1] == "-f":
if len(sys.argv) != 4:
help()
num = int(sys.argv[2])
ver = sys.argv[3]
fws = products[num]
print(f"Fetching firmware {ver} for {fws[0]["fileName"]} (#{num}):\n")
for fw in fws:
if str(fw["version"]).startswith(ver):
url = baseurl + fw["filePathName"]
file = os.path.basename(url)
print("Downloading: " + url)
urllib.request.urlretrieve(url, file)
print("Saved firmware to " + file + ".\n")
exit(0)
print("... version not found.\n")
exit(1)