Skip to content

Commit

Permalink
Downloads no longer freeze the program
Browse files Browse the repository at this point in the history
  • Loading branch information
dhtdht020 committed May 18, 2021
1 parent a39c322 commit 35cf97a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions download.py
Expand Up @@ -30,6 +30,10 @@ def get(app_name, output=None, extract=False, repo="hbb1.oscwii.org"):
print(FAIL + f"Download failed. HTTP status code is {str(app_data.status_code)}, not 200.")


def get_url(app_name, repo="hbb1.oscwii.org"):
return "https://" + repo + "/hbb/" + app_name + "/" + app_name + ".zip"


def hbb(output):
try:
app_data = requests.get("https://wii.guide/assets/files/homebrew_browser_v0.3.9e.zip")
Expand Down
36 changes: 33 additions & 3 deletions xosc_dl.py
Expand Up @@ -326,15 +326,44 @@ def download_button(self):
self.status_message(f"Downloading {self.app_name} from Open Shop Channel..")
path_to_file, _ = QFileDialog.getSaveFileName(None, 'Save Application', self.ui.FileNameLineEdit.text())
output = path_to_file
self.ui.progressBar.setValue(25)
self.ui.progressBar.setValue(0)
console_output = io.StringIO()
if output != '':
with redirect_stdout(console_output):
download.get(app_name=self.app_name, repo=HOST, output=output)
# get url to app
url = download.get_url(app_name=self.app_name, repo=HOST)

# stream file, so I can iterate
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))

# set progress bar
self.ui.progressBar.setMaximum(total_size)

block_size = 1024 # 1 Kibibyte

if response.status_code == 200:
# disable download button
self.ui.ViewMetadataBtn.setEnabled(False)
# disable apps list
self.ui.listAppsWidget.setEnabled(False)

with open(output, "wb") as app_data_file:
for data in response.iter_content(block_size):
self.ui.progressBar.setValue(self.ui.progressBar.value() + 1024)
self.status_message(f"Downloading {self.app_name} from Open Shop Channel.. ({metadata.file_size(self.ui.progressBar.value())}/{metadata.file_size(total_size)})")
app.processEvents()
app_data_file.write(data)

self.ui.progressBar.setValue(100)
self.status_message(utils.escape_ansi(console_output.getvalue()))
self.ui.progressBar.setMaximum(100)
self.ui.ViewMetadataBtn.setEnabled(True)
self.ui.listAppsWidget.setEnabled(True)
self.status_message(f"Download success! Output: {output}")
else:
self.ui.progressBar.setValue(0)
self.ui.ViewMetadataBtn.setEnabled(True)
self.ui.listAppsWidget.setEnabled(True)
self.status_message("Cancelled Download.")

def wiiload_button(self):
Expand Down Expand Up @@ -767,6 +796,7 @@ def populate_stylesheets(self):


if __name__ == "__main__":
global app
app = QApplication()

global splash
Expand Down

0 comments on commit 35cf97a

Please sign in to comment.