Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
feat(ui): ✨ add download function (resolve #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jun 16, 2022
1 parent 1e8e704 commit c7de2fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"conventionalCommits.scopes": [
"requester",
"download",
"ui"
]
}
18 changes: 17 additions & 1 deletion utils/window/frames/buttons.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
from tkinter import Frame, Button
from typing import TYPE_CHECKING

from utils.constant import PATH
from utils.download import Download
from utils.requester import Requester

if TYPE_CHECKING:
from utils.window.main import Main
Expand All @@ -22,7 +25,8 @@ def __init__(self, master: 'Main'):
self.__download_button = Button(
self,
text='下载',
background='white'
background='white',
command=self.download
)
self.__exit_button = Button(
self,
Expand All @@ -34,3 +38,15 @@ def __init__(self, master: 'Main'):
self.__exit_button.pack(side='right')
self.__download_button.pack(side='right', padx=10)
self.__import_button.pack(side='right')

def download(self):
modpack_id = self.__main_window.show_frame.selected_modpack_id
file_name = self.__main_window.show_frame.selected_file_name

if modpack_id != -1 and file_name != '':
download_url = self.__main_window.show_frame.selected_download_url

file_path = os.path.join(os.getcwd(), PATH.TEMP_DIR_PATH, file_name)
with open(file_path, 'wb') as f:
f.write(Requester.get(download_url).content)
Download(file_path).main()
24 changes: 24 additions & 0 deletions utils/window/frames/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,27 @@ def run():
def selected_index(self) -> int:
selection = self.__list_listbox.curselection()
return selection[0] if selection else -1

@property
def selected_modpack_id(self) -> int:
index = self.selected_index
return self.__data[index].get('id') if index != -1 else -1

@property
def selected_files(self) -> Dict:
if self.selected_index != -1:
return self.__data[self.selected_index].get('files')

@property
def selected_file_name(self) -> str:
display_name = self.__main_window.filters_frame.modpack_version
if display_name != '' and self.selected_files is not None:
return self.selected_files[display_name]['fileName']
return ''

@property
def selected_download_url(self) -> str:
display_name = self.__main_window.filters_frame.modpack_version
if display_name != '' and self.selected_files is not None:
return self.selected_files[display_name]['downloadUrl']
return ''

0 comments on commit c7de2fd

Please sign in to comment.