Skip to content

Commit

Permalink
release v0.2.6
Browse files Browse the repository at this point in the history
- 默认不支持上传大于 100M 文件
- api 同步更新
- 修复已知bug
- 最后一个提供打包的版本,以后不再打包
  • Loading branch information
rachpt committed Apr 19, 2020
1 parent f8a219b commit 4900f24
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@

lanzou-gui: Copyright (c) [rachpt](https://gitee.com/rachpt/). See the [MIT LICENSE](https://github.com/rachpt/lanzou-gui/blob/master/LICENSE) for details.

lanzou-api: Copyright (c) [zaxtyson](https://github.com/zaxtyson/).
lanzou-api: Copyright (c) [zaxtyson](https://github.com/zaxtyson/). [MIT LICENSE](https://github.com/zaxtyson/LanZouCloud-API/blob/master/LICENSE).
117 changes: 79 additions & 38 deletions dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
QTextEdit, QGridLayout, QListView, QDialogButtonBox, QVBoxLayout, QHBoxLayout,
QComboBox, QCheckBox, QSizePolicy)

from tools import UserInfo, encrypt, decrypt, UpJob, FileInfos, FolderInfos
from tools import UserInfo, encrypt, decrypt, UpJob, FileInfos
KEY = 89


Expand Down Expand Up @@ -90,7 +90,7 @@ def set_file_icon(name):
border: 2px solid gray;
border-radius: 8px;
}
#btn_chooseMutiFile, #btn_chooseDir {
#btn_chooseMultiFile, #btn_chooseDir {
min-width: 90px;
max-width: 90px;
}
Expand Down Expand Up @@ -476,16 +476,24 @@ def __init__(self):
self.set_desc = False
self.pwd = ''
self.desc = ''
self.allow_big_file = False
self.max_size = 100
self.selected = []
self.initUI()
self.set_size()
self.setStyleSheet(dialog_qss_style)

def set_pwd_desc(self, set_pwd, pwd, set_desc, desc):
def set_pwd_desc_bigfile(self, set_pwd, pwd, set_desc, desc, allow_big_file, max_size):
self.set_pwd = set_pwd
self.set_desc = set_desc
self.pwd = pwd
self.desc = desc
self.allow_big_file = allow_big_file
self.max_size = max_size
if self.allow_big_file:
self.btn_chooseMultiFile.setToolTip("")
else:
self.btn_chooseMultiFile.setToolTip(f"文件大小上线 {self.max_size}MB")

def set_values(self, folder_name, folder_id, files):
self.setWindowTitle("上传文件至 ➩ " + str(folder_name))
Expand All @@ -511,10 +519,10 @@ def initUI(self):
self.btn_chooseDir.setIcon(QIcon("./src/folder.gif"))

# btn 2
self.btn_chooseMutiFile = QPushButton("选择多文件", self)
self.btn_chooseDir.setObjectName("btn_chooseMutiFile")
self.btn_chooseMutiFile.setObjectName("btn_chooseMutiFile")
self.btn_chooseMutiFile.setIcon(QIcon("./src/file.ico"))
self.btn_chooseMultiFile = QPushButton("选择多文件", self)
self.btn_chooseDir.setObjectName("btn_chooseMultiFile")
self.btn_chooseMultiFile.setObjectName("btn_chooseMultiFile")
self.btn_chooseMultiFile.setIcon(QIcon("./src/file.ico"))

# btn 3
self.btn_deleteSelect = QPushButton("移除", self)
Expand Down Expand Up @@ -545,7 +553,7 @@ def initUI(self):
hbox_button = QHBoxLayout()
hbox_head.addWidget(self.btn_chooseDir)
hbox_head.addStretch(1)
hbox_head.addWidget(self.btn_chooseMutiFile)
hbox_head.addWidget(self.btn_chooseMultiFile)
hbox_button.addWidget(self.btn_deleteSelect)
hbox_button.addStretch(1)
hbox_button.addWidget(self.buttonBox)
Expand All @@ -558,7 +566,7 @@ def initUI(self):

# 设置信号
self.btn_chooseDir.clicked.connect(self.slot_btn_chooseDir)
self.btn_chooseMutiFile.clicked.connect(self.slot_btn_chooseMutiFile)
self.btn_chooseMultiFile.clicked.connect(self.slot_btn_chooseMultiFile)
self.btn_deleteSelect.clicked.connect(self.slot_btn_deleteSelect)

self.buttonBox.accepted.connect(self.slot_btn_ok)
Expand Down Expand Up @@ -642,13 +650,16 @@ def slot_btn_chooseDir(self):
self.selected.append(dir_choose)
self.show_selected()

def slot_btn_chooseMutiFile(self):
def slot_btn_chooseMultiFile(self):
files, _ = QFileDialog.getOpenFileNames(self, "选择多文件", self.cwd, "All Files (*)")
if len(files) == 0:
return
for _file in files:
if _file not in self.selected:
self.selected.append(_file)
if os.path.getsize(_file) <= self.max_size * 1048576:
self.selected.append(_file)
elif self.allow_big_file:
self.selected.append(_file)
self.show_selected()

def keyPressEvent(self, e):
Expand Down Expand Up @@ -1041,7 +1052,6 @@ def btn_ok(self):
new_pwd = self.tx_newpwd.text()
for infos in self.infos:
infos.new_pwd = new_pwd
# if new_pwd != self.infos[5]:
self.new_infos.emit(self.infos) # 最后一位用于标示文件还是文件夹


Expand All @@ -1053,39 +1063,41 @@ def __init__(self, parent=None):
super(MoveFileDialog, self).__init__(parent)
self.infos = None
self.dirs = {}
self.initUI()
self.setStyleSheet(dialog_qss_style)

def update_ui(self):
names = "\n".join([i.name for i in self.infos])
names_tip = "\n".join([i.name for i in self.infos])
self.tx_name.setText(names)
self.tx_name.setToolTip(names_tip)

self.tx_new_path.clear()
f_icon = QIcon("./src/folder.gif")
for f_name, fid in self.dirs.items():
if len(f_name) > 50: # 防止文件夹名字过长?
f_name = f_name[:47] + "..."
self.tx_new_path.addItem(f_icon, "id:{:>8},name:{}".format(fid, f_name))

def set_values(self, infos, all_dirs_dict):
self.infos = infos
self.dirs = all_dirs_dict
self.show_ui()
self.update_ui()
self.exec()

def show_ui(self):
def initUI(self):
self.setWindowTitle("移动文件(夹)")
self.setWindowIcon(QIcon("./src/move.ico"))
self.lb_name = QLabel()
self.lb_name.setText("文件(夹)名:")
self.lb_name.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
self.tx_name = AutoResizingTextEdit()
names = "\n".join([i.name for i in self.infos])
names_tip = "\n".join([i.name for i in self.infos])
self.tx_name.setText(names)
self.tx_name.setToolTip(names_tip)
# 只读
self.tx_name.setFocusPolicy(Qt.NoFocus)
self.tx_name.setFocusPolicy(Qt.NoFocus) # 只读
self.tx_name.setReadOnly(True)
self.lb_new_path = QLabel()
self.lb_new_path.setText("目标文件夹:")
self.lb_new_path.setAlignment(
Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter
)
self.lb_new_path.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
self.tx_new_path = QComboBox()
f_icon = QIcon("./src/folder.gif")
for f_name, fid in self.dirs.items():
if len(f_name) > 50: # 防止文件夹名字过长?
f_name = f_name[:47] + "..."
self.tx_new_path.addItem(f_icon, "id:{:>8},name:{}".format(fid, f_name))

self.buttonBox = QDialogButtonBox()
self.buttonBox.setOrientation(Qt.Horizontal)
Expand All @@ -1107,9 +1119,10 @@ def show_ui(self):
self.setMinimumWidth(280)

def btn_ok(self):
selected = self.tx_new_path.currentText().split(",")[0].split(":")[1]
# file_id, folder_id, f_name, type(size)
self.new_infos.emit([(info[0], selected, info[1], info[2]) for info in self.infos])
new_id = self.tx_new_path.currentText().split(",")[0].split(":")[1]
for index in range(len(self.infos)):
self.infos[index].new_id = int(new_id)
self.new_infos.emit(self.infos)


class DeleteDialog(QDialog):
Expand Down Expand Up @@ -1283,16 +1296,18 @@ def __init__(self, config_file: str, default_settings: dict, parent=None):
self._config_file = config_file
self._default_settings = default_settings
self._user = None
self.download_threads = None
self.max_size = None
self.timeout = None
self.download_threads = 3
self.max_size = 100
self.timeout = 5
self.dl_path = None
self.time_fmt = False
self.to_tray = False
self.watch_clipboard = False
self.debug = False
self.set_pwd = False
self.set_desc = False
self.upload_delay = 0
self.allow_big_file = False
self.pwd = ""
self.desc = ""
self.initUI()
Expand Down Expand Up @@ -1339,6 +1354,9 @@ def show_values(self):
self.set_desc_box.setChecked(self.set_desc)
self.set_desc_var.setEnabled(self.set_desc)
self.set_desc_var.setText(self.desc)
self.upload_delay_var.setText(str(self.upload_delay))
self.big_file_box.setChecked(self.allow_big_file)
self.big_file_box.setText(f"允许上传超过 {self.max_size}MB 的大文件")

def set_values(self, reset=False):
"""设置控件对应变量初始值"""
Expand All @@ -1355,13 +1373,19 @@ def set_values(self, reset=False):
self.pwd = settings["pwd"] if "pwd" in settings else "" # 兼容
self.set_desc = settings["set_desc"] if "set_desc" in settings else False # 兼容
self.desc = settings["desc"] if "desc" in settings else "" # 兼容
self.upload_delay = settings["upload_delay"] if "upload_delay" in settings else 0 # 兼容
self.show_values()

def get_values(self) -> dict:
"""读取输入控件的值"""
self.download_threads = int(self.download_threads_var.text())
self.max_size = int(self.max_size_var.text())
self.timeout = int(self.timeout_var.text())
if self.download_threads_var.text():
self.download_threads = int(self.download_threads_var.text())
if self.max_size_var.text():
self.max_size = int(self.max_size_var.text())
if self.timeout_var.text():
self.timeout = int(self.timeout_var.text())
if self.upload_delay_var.text():
self.upload_delay = int(self.upload_delay_var.text())
self.dl_path = str(self.dl_path_var.text())
self.pwd = str(self.set_pwd_var.toPlainText())
self.desc = str(self.set_desc_var.toPlainText())
Expand All @@ -1376,7 +1400,9 @@ def get_values(self) -> dict:
"set_pwd": self.set_pwd,
"pwd": self.pwd,
"set_desc": self.set_desc,
"desc": self.desc}
"desc": self.desc,
"upload_delay": self.upload_delay,
"allow_big_file": self.allow_big_file}

def initUI(self):
self.setWindowTitle("设置")
Expand All @@ -1399,6 +1425,11 @@ def initUI(self):
self.timeout_var.setPlaceholderText("范围:1-99")
self.timeout_var.setToolTip("范围:1-99")
self.timeout_var.setInputMask("D9")
self.upload_delay_lb = QLabel("上传延时(秒)")
self.upload_delay_var = QLineEdit()
self.upload_delay_var.setPlaceholderText("范围:1-99")
self.upload_delay_var.setToolTip("范围:1-99")
self.upload_delay_var.setInputMask("D9")
self.dl_path_lb = QLabel("下载保存路径")
self.dl_path_var = MyLineEdit(self)
self.dl_path_var.clicked.connect(self.set_download_path)
Expand All @@ -1412,6 +1443,7 @@ def initUI(self):
self.set_pwd_var.setToolTip("2-8 位数字或字母")
self.set_desc_box = QCheckBox("上传文件自动设置描述")
self.set_desc_var = AutoResizingTextEdit()
self.big_file_box = QCheckBox(f"允许上传超过 {self.max_size}MB 的大文件")

self.time_fmt_box.toggle()
self.time_fmt_box.stateChanged.connect(self.change_time_fmt)
Expand All @@ -1421,6 +1453,7 @@ def initUI(self):
self.set_pwd_box.stateChanged.connect(self.change_set_pwd)
self.set_pwd_var.editingFinished.connect(self.check_pwd)
self.set_desc_box.stateChanged.connect(self.change_set_desc)
self.big_file_box.stateChanged.connect(self.change_big_file)

buttonBox = QDialogButtonBox()
buttonBox.setOrientation(Qt.Horizontal)
Expand All @@ -1437,6 +1470,7 @@ def initUI(self):
form.setSpacing(10)
form.addRow(self.download_threads_lb, self.download_threads_var)
form.addRow(self.timeout_lb, self.timeout_var)
form.addRow(self.upload_delay_lb, self.upload_delay_var)
form.addRow(self.max_size_lb, self.max_size_var)
form.addRow(self.dl_path_lb, self.dl_path_var)

Expand All @@ -1461,6 +1495,7 @@ def initUI(self):
hbox_3.addWidget(self.set_desc_box)
hbox_3.addWidget(self.set_desc_var)
vbox.addLayout(hbox_3)
vbox.addWidget(self.big_file_box)
vbox.addStretch(2)
vbox.addWidget(buttonBox)
self.setLayout(vbox)
Expand Down Expand Up @@ -1490,6 +1525,12 @@ def change_debug(self, state):
else:
self.debug = False

def change_big_file(self, state):
if state == Qt.Checked:
self.allow_big_file = True
else:
self.allow_big_file = False

def change_set_pwd(self, state):
if state == Qt.Checked:
self.set_pwd = True
Expand Down
27 changes: 22 additions & 5 deletions lanzou/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from time import sleep
from datetime import datetime
from urllib3 import disable_warnings
from random import shuffle, random
from random import shuffle, random, uniform
from typing import List
from concurrent.futures import ThreadPoolExecutor, as_completed

Expand Down Expand Up @@ -42,6 +42,7 @@ def __init__(self):
self._captcha_handler = None
self._timeout = 5 # 每个请求的超时(不包含下载响应体的用时)
self._max_size = 100 # 单个文件大小上限 MB
self._upload_delay = (0, 0) # 文件上传延时
self._host_url = 'https://www.lanzous.com'
self._doupload_url = 'https://pc.woozooo.com/doupload.php'
self._account_url = 'https://pc.woozooo.com/account.php'
Expand Down Expand Up @@ -83,6 +84,13 @@ def set_max_size(self, max_size=100) -> int:
self._max_size = max_size
return LanZouCloud.SUCCESS

def set_upload_delay(self, t_range: tuple) -> int:
"""设置上传大文件数据块时,相邻两次上传之间的延时,减小被封号的可能"""
if 0 <= t_range[0] <= t_range[1]:
self._upload_delay = t_range
return LanZouCloud.SUCCESS
return LanZouCloud.FAILED

def set_captcha_handler(self, captcha_handler):
"""设置下载验证码处理函数
:param captcha_handler (img_data) -> str 参数为图片二进制数据,需返回验证码字符
Expand Down Expand Up @@ -661,7 +669,8 @@ def get_move_folders(self) -> FolderList:
resp = self._post(self._doupload_url, data={"task": 19, "file_id": -1})
if not resp or resp.json()['zt'] != 1: # 获取失败或者网络异常
return result
for folder in resp.json()['info']:
info = resp.json()['info'] or [] # 新注册用户无数据, info=None
for folder in info:
folder_id, folder_name = int(folder['folder_id']), folder['folder_name']
result.append(FolderId(folder_name, folder_id, "", ""))
return result
Expand Down Expand Up @@ -827,6 +836,11 @@ def _callback(name, t_size, now_size): # 重新封装回调函数,隐藏数
else:
logger.debug(f"Upload data file failed: {data_path=}")
return LanZouCloud.FAILED, 0, False
os.remove(data_path) # 删除临时数据块
min_s, max_s = self._upload_delay # 设置两次上传间的延时,减小封号可能性
sleep_time = uniform(min_s, max_s)
logger.debug(f"Sleeping, Upload task will resume after {sleep_time:.2f}s...")
sleep(sleep_time)

# 全部数据块上传完成
record_name = list(file_name.replace('.', '')) # 记录文件名也打乱
Expand All @@ -843,14 +857,17 @@ def _callback(name, t_size, now_size): # 重新封装回调函数,隐藏数
logger.debug(f"Upload finished, Delete tmp folder:{tmp_dir}")
return LanZouCloud.SUCCESS, int(dir_id), False # 大文件返回文件夹id

def upload_file(self, file_path, folder_id=-1, callback=None) -> (int, int, bool):
def upload_file(self, file_path, folder_id=-1, callback=None, allow_big_file=False) -> (int, int, bool):
"""解除限制上传文件"""
if not os.path.isfile(file_path):
return LanZouCloud.PATH_ERROR, 0, True

# 单个文件不超过 max_size 直接上传
if os.path.getsize(file_path) <= self._max_size * 1048576:
return self._upload_small_file(file_path, folder_id, callback)
elif not allow_big_file:
logger.debug(f'Forbid upload big file!{file_path=}, {self._max_size=}')
return LanZouCloud.FAILED, 0, False # 不允许上传超过 max_size 的文件

# 上传超过 max_size 的文件
folder_name = os.path.basename(file_path) # 保存分段文件的文件夹名
Expand All @@ -859,7 +876,7 @@ def upload_file(self, file_path, folder_id=-1, callback=None) -> (int, int, bool
return LanZouCloud.MKDIR_ERROR, 0, False # 创建文件夹失败就退出
return self._upload_big_file(file_path, dir_id, callback)

def upload_dir(self, dir_path, folder_id=-1, callback=None, failed_callback=None):
def upload_dir(self, dir_path, folder_id=-1, callback=None, failed_callback=None, allow_big_file=False):
"""批量上传文件夹中的文件(不会递归上传子文件夹)
:param folder_id: 网盘文件夹 id
:param dir_path: 文件夹路径
Expand All @@ -878,7 +895,7 @@ def upload_dir(self, dir_path, folder_id=-1, callback=None, failed_callback=None
file_path = dir_path + os.sep + filename
if not os.path.isfile(file_path):
continue # 跳过子文件夹
code, _, _ = self.upload_file(file_path, dir_id, callback)
code, _, _ = self.upload_file(file_path, dir_id, callback=callback, allow_big_file=allow_big_file)
if code != LanZouCloud.SUCCESS:
if failed_callback is not None:
failed_callback(code, filename)
Expand Down
Loading

0 comments on commit 4900f24

Please sign in to comment.