wxy1343 / aliyunpan

阿里云盘cli
GNU General Public License v3.0
475 stars 65 forks source link

可以支持批量重命名就好了 #103

Open pheotry opened 1 year ago

wxy1343 commented 1 year ago
import functools
import threading
import time

from aliyunpan.api.type import FileInfo
from aliyunpan.cli import Commander

lock = threading.Lock()

def enum_files(commander: Commander, file_id, func):
    for path in commander.get_fid_list(file_id):  # 获取文件列表
        path: FileInfo
        if not path.type:  # 判断是否是文件
            enum_files(commander, path.id, func)  # 递归查询
        else:
            threading.Thread(target=func, args=(path,)).start()  # 多线程调用回调

def rename(commander: Commander, path: FileInfo):
    if path.type:  # 判断是否是文件
        name: str = path.name
        if name.endswith('.png'):  # 判断文件后缀为.png的文件
            while True:
                with lock:  # 加锁防止多线程打印内容混乱
                    print(name, end=' -> ')
                    name = name.rsplit('.')[0] + '.jpg'  # 修改后缀为.jpg
                    print(name)
                if path.id != commander.disk.update_file(path.id, name):  # 重命名文件
                    """这里是重命名失败的操作,有可能是由于网络问题或者接口限制"""
                    print('重命名失败:' + path.name)
                    time.sleep(0.5)
                    continue
                break

if __name__ == '__main__':
    c = Commander()
    enum_files(c, c.path_list.get_path_fid('test', update=False), functools.partial(rename, c))
pheotry commented 1 year ago
import functools
import threading
import time

from aliyunpan.api.type import FileInfo
from aliyunpan.cli import Commander

lock = threading.Lock()

def enum_files(commander: Commander, file_id, func):
    for path in commander.get_fid_list(file_id):  # 获取文件列表
        path: FileInfo
        if not path.type:  # 判断是否是文件
            enum_files(commander, path.id, func)  # 递归查询
        else:
            threading.Thread(target=func, args=(path,)).start()  # 多线程调用回调

def rename(commander: Commander, path: FileInfo):
    if path.type:  # 判断是否是文件
        name: str = path.name
        if name.endswith('.png'):  # 判断文件后缀为.png的文件
            while True:
                with lock:  # 加锁防止多线程打印内容混乱
                    print(name, end=' -> ')
                    name = name.rsplit('.')[0] + '.jpg'  # 修改后缀为.jpg
                    print(name)
                if path.id != commander.disk.update_file(path.id, name):  # 重命名文件
                    """这里是重命名失败的操作,有可能是由于网络问题或者接口限制"""
                    print('重命名失败:' + path.name)
                    time.sleep(0.5)
                    continue
                break

if __name__ == '__main__':
    c = Commander()
    enum_files(c, c.path_list.get_path_fid('test', update=False), functools.partial(rename, c))

明白了