def __init__(self, *args, **kwargs): super(Pixiv, self).__init__(*args, **kwargs) self.downloader = None self.url, self.username, self.password, \ self.proxy, self.save_path = mk_pixiv_inputs(self) self.btn_group = self.build_buttons() self.progress = ProgressBar(self) self.status = StatusBar(self) self.start_update()
def __init__(self, *args, store_name=None, user_home_name=None, **kwargs): super(UserHomeDownloader, self).__init__(*args, **kwargs) self.downloader = None self.url, self.save_path = mk_normal_inputs( self, store_name=store_name, user_home_name=user_home_name, ) self.btn_group = self.build_buttons() self.progress = ProgressBar(self) self.status = StatusBar(self) self.start_update()
def __init__(self, *args, **kwargs): super(PinryImporterGUI, self).__init__(*args, **kwargs) self._url = NamedInput(self, name="Pinry部署地址") self._min_size = NamedInput(self, name="最小上传大小(KB)(低于此值的文件不上传,不限制请留空)") self._username = NamedInput(self, name="用户名") self._password = PasswordInput(self, name="密码") self._csv_file = FilePathBrowse(self, store_name="import_csv", text_label="CSV文件文件路径") self.btn_group = self.build_buttons() self._importer = None self.progress = ProgressBar(self) self.status = StatusBar(self) self.start_update()
class UserHomeDownloader(tk.Frame): title = "请更改此名字" def __init__(self, *args, store_name=None, user_home_name=None, **kwargs): super(UserHomeDownloader, self).__init__(*args, **kwargs) self.downloader = None self.url, self.save_path = mk_normal_inputs( self, store_name=store_name, user_home_name=user_home_name, ) for attr_name, value in self.user_inputs().items(): setattr(self, attr_name, value) self.btn_group = self.build_buttons() self.progress = ProgressBar(self) self.status = StatusBar(self) self.start_update() def user_inputs(self): return {} def run(self, url, path_prefix): raise NotImplementedError() def build_buttons(self): btn_args = dict(height=1, ) btn_group = tk.Frame(self) buttons = [ tk.Button(btn_group, text=text, command=command, **btn_args) for text, command in ( ("开始下载", self.start_download), ("停止下载", self.stop_download), ("打开下载文件夹", self.open_download_folder), ) ] for index, btn in enumerate(buttons): btn.grid(column=index, row=0, sticky=tk.N) btn_group.pack(fill=tk.BOTH, expand=1) return btn_group def open_download_folder(self): path = self.save_path.get_path() open_sys_explorer(path) def start_download(self): self.url.assert_no_error() self.save_path.assert_no_error() url = self.url.get_input() path_prefix = self.save_path.get_path() if not os.access(path_prefix, os.W_OK): return info("对下载文件夹没有写权限,请重新选择") if self.downloader is not None: if not self.downloader.done: return info("请停止后再重新点击下载...") self.downloader = self.run( url=url, path_prefix=path_prefix, ) def stop_download(self): if self.downloader is not None: self.downloader.stop() self.downloader = None def start_update(self): run_as_thread(self._update_loop) def _update_loop(self): while True: time.sleep(0.1) try: self.update_progress() except AttributeError: pass def update_progress(self): if self.downloader is None: self.progress.update_progress(0, 100) self.status.set("") else: self.progress.update_progress( self.downloader.counter.done, self.downloader.counter.total, ) msg = self.downloader.counter.format() if self.downloader.done: msg = msg + " 全部下载完毕,可以开始新的下载了:)" self.status.set(msg)
class PinryImporterGUI(tk.Frame): title = "导入到Pinry" def __init__(self, *args, **kwargs): super(PinryImporterGUI, self).__init__(*args, **kwargs) self._url = NamedInput(self, name="Pinry部署地址") self._min_size = NamedInput(self, name="最小上传大小(KB)(低于此值的文件不上传,不限制请留空)") self._username = NamedInput(self, name="用户名") self._password = PasswordInput(self, name="密码") self._csv_file = FilePathBrowse(self, store_name="import_csv", text_label="CSV文件文件路径") self.btn_group = self.build_buttons() self._importer = None self.progress = ProgressBar(self) self.status = StatusBar(self) self.start_update() def _get_importer(self): min_size = self._min_size.get_input() if min_size: try: min_size = int(min_size) except Exception: info("最小文件上传大小应该是整数") else: min_size = None return PinryImporter( base_url=self._url.get_input(), username=self._username.get_input(), password=self._password.get_input(), min_upload_size_kb=min_size, ) def build_buttons(self): btn_args = dict(height=1, ) btn_group = tk.Frame(self) buttons = [ tk.Button(btn_group, text=text, command=command, **btn_args) for text, command in ( ("测试登录", self._test_login), ("开始导入", self._start_import), ) ] for index, btn in enumerate(buttons): btn.grid(column=index, row=0, sticky=tk.N) btn_group.pack(fill=tk.BOTH, expand=1) return btn_group def _test_login(self): importer = self._get_importer() if importer.test_login() is True: info("登录成功") else: info("情检查用户名密码以及部署路径是否可访问") def _start_import(self): self._importer = self._get_importer() run_as_thread(self._importer.do_import, self._csv_file.get_path(), name="import2pinry") def start_update(self): run_as_thread(self._update_loop) def _update_loop(self): while True: time.sleep(0.1) self.update_progress() def update_progress(self): if self._importer is not None: self.progress.update_progress( self._importer.done_pins, self._importer.total_pins, ) self.status.set(self._importer.status_text()) else: self.progress.update_progress(0, 0) self.status.set("待机...")