forked from dhtdht020/osc-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.py
199 lines (167 loc) · 6.53 KB
/
metadata.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json
import logging
import os
import re
import sys
import requests
import lxml.etree
# Get resource when frozen with PyInstaller
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
# Get icon of homebrew app. Should be updated to use API sooner or later!
def icon(app_name, repo="hbb1.oscwii.org"):
try:
request = requests.get("https://" + repo + "/hbb/" + app_name + ".png")
icon = request.content
# If icon is not there
if str(request.status_code) != "200":
with open(resource_path("assets/gui/missing.png"), mode='rb') as file:
icon = file.read()
file.close()
# icon = resource_path("assets/gui/missing.png")
except Exception:
with open(resource_path("assets/gui/missing.png"), mode='rb') as file:
icon = file.read()
file.close()
return icon
# Get direct download url of app. Should be replaced with API response sooner or later.
def url(app_name, repo="hbb1.oscwii.org"):
app_url = "https://" + repo + "/hbb/" + app_name + "/" + app_name + ".zip"
return app_url
# Get JSON of specified packages from API
def get_apps(host_name="primary"):
try:
json_req = requests.get(f"https://api.oscwii.org/v2/{host_name}/packages", timeout=10)
if json_req.status_code != 200:
raise Exception("Cannot reach Open Shop Channel API.")
except Exception as e:
# Return fake apps list with offline app
return json.loads('''
[{
"category": "demos",
"coder": "-",
"contributors": "",
"controllers": "",
"display_name": "1 : You are not connected to the internet.",
"downloads": 0,
"extra_directories": [],
"extracted": 0,
"icon_url": "https://hbb1.oscwii.org/hbb/offline.png",
"internal_name": "FakeAppOfflineError",
"long_description": "Could not connect to the server. Please check your internet connection.",
"package_type": "dol",
"rating": "",
"release_date": 1557464400,
"shop_title_id": "",
"shop_title_version": "",
"short_description": "Please check your internet connection.",
"updated": 1557464400,
"version": "1",
"zip_size": 0,
"zip_url": "https://hbb1.oscwii.org/hbb/offline/offline.zip"
},
{
"category": "demos",
"coder": "-",
"contributors": "",
"controllers": "",
"display_name": "2 : The server is potentially down.",
"downloads": 0,
"extra_directories": [],
"extracted": 0,
"icon_url": "https://hbb1.oscwii.org/hbb/offline.png",
"internal_name": "FakeAppOfflineInfo",
"long_description": "OSCWII.ORG is potentially down. Please contact us.",
"package_type": "dol",
"rating": "",
"release_date": 1557464400,
"shop_title_id": "",
"shop_title_version": "",
"short_description": "Please contact us.",
"updated": 1557464400,
"version": "1",
"zip_size": 0,
"zip_url": "https://hbb1.oscwii.org/hbb/offline/offline.zip"
}]''')
return json.loads(json_req.text)
# Get long description from an app's meta XML
def long_description(app_name, repo="hbb1.oscwii.org"):
try:
xml = requests.get("https://" + repo + "/unzipped_apps/" + app_name + "/apps/" + app_name + "/meta.xml").text
except requests.exceptions.SSLError:
xml = requests.get("http://" + repo + "/unzipped_apps/" + app_name + "/apps/" + app_name + "/meta.xml").text
# Remove unicode declaration
xml = xml.split("\n", 1)[1]
# Remove HTML comments
xml = re.sub(r'<!--.*?-->', '', xml, flags=re.DOTALL)
# Parse XML
try:
root = lxml.etree.fromstring(xml)
except Exception as e:
logging.error(f"[{app_name}] With the intention to load the long description, "
f"OSCDL could not parse the application metadata XML. Information:\n{str(e)}")
return "No description provided"
return root.find('long_description').text
# Returns readable file size from file length
def file_size(length):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(length) < 1024.0:
return "%3.1f%s%s" % (length, unit, "B")
length /= 1024.0
return "%.1f%s%s" % (length, 'Yi', "B")
# Get display name of category with internal name
def category_display_name(category):
if category == "demos":
return "Demo"
elif category == "emulators":
return "Emulator"
elif category == "games":
return "Game"
elif category == "media":
return "Media"
elif category == "utilities":
return "Utility"
else:
return ""
# Parse controllers string
def parse_peripherals(peripherals):
peripherals_dict = {"wii_remotes": 0, "nunchuk": False, "classic": False, "gamecube": False,
"wii_zapper": False, "keyboard": False, "sdhc": False}
# One day OSCDL will drop support for <3.10 and this will be a switch statement..
for character in peripherals:
if character is "w":
peripherals_dict["wii_remotes"] += 1
elif character is "n":
peripherals_dict["nunchuk"] = True
elif character is "c":
peripherals_dict["classic"] = True
elif character is "g":
peripherals_dict["gamecube"] = True
if character is "z":
peripherals_dict["wii_zapper"] = True
if character is "k":
peripherals_dict["keyboard"] = True
if character is "s":
peripherals_dict["sdhc"] = True
return peripherals_dict
# API-related functions
class API:
host_name = "primary"
packages = None
def get_packages(self):
self.packages = get_apps()
# Change repository
def set_host(self, host):
if host == self.host_name and (self.packages):
pass
else:
self.host_name = host
self.packages = get_apps(host_name=host)
# Metadata for given application
def information(self, internal_name):
for i in self.packages:
if i["internal_name"] == internal_name:
return i
return