Skip to content

Commit

Permalink
避免重复写入desktop.ini、执行 cmd
Browse files Browse the repository at this point in the history
检测到 desktop.ini 文件,就当作已经成功修改封面过
  • Loading branch information
yodhcn committed Dec 21, 2022
1 parent 90dd2f9 commit c7c711c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def rename(self, root_path: str):
if self.__make_folder_icon:
try:
icon_name, _ = Renamer.changeIcon(self, rjcode, metadata['cover_url'], folder_path) # 修改封面
Renamer.logger.info(f'[{rjcode}] -> 修改封面成功:"{icon_name}"')
except RequestException as err:
Renamer.__handle_request_exception(rjcode, '下载封面图', err) # 下载封面图失败
continue
Expand All @@ -179,26 +178,27 @@ def changeIcon(self, rjcode: str, cover_url: str, icon_dir: str):
os.chmod(icon_dir, stat.S_IREAD)
icon_name, jpg_name = self.__scraper.scrape_icon(rjcode, cover_url, icon_dir)

# 编写 desktop.ini
iniline1 = "[.ShellClassInfo]"
iniline2 = "IconResource=" + "\"" + icon_name + "\"" + ",0"
iniline3 = "[ViewState]" + "\n" + "Mode=" + "\n" + "Vid=" + "\n" + "FolderType=StorageProviderGeneric"
iniline = iniline1 + "\n" + iniline2 + "\n" + iniline3

# 写入 desktop.ini
inifile_path = Path(os.path.join(icon_dir, "desktop.ini"))
inifile_path.unlink(missing_ok=True) # 删除旧的 .ini 文件
with open(inifile_path, "w", encoding='utf-8') as inifile:
inifile.write(iniline)
inifile.close()

# 编写 cmd
cmd1 = icon_dir[0:2]
cmd2 = "cd " + '\"' + icon_dir + '\"'
cmd3 = "attrib +h +s " + 'desktop.ini'
cmd4 = "attrib +h +s " + icon_name
cmd = cmd1 + " & " + cmd2 + " & " + cmd3 + " & " + cmd4
os.system(cmd) # 运行 cmd
ini_file_path = Path(os.path.join(icon_dir, "desktop.ini"))
if not os.path.exists(ini_file_path):
# 编写 desktop.ini
iniline1 = "[.ShellClassInfo]"
iniline2 = "IconResource=" + "\"" + icon_name + "\"" + ",0"
iniline3 = "[ViewState]" + "\n" + "Mode=" + "\n" + "Vid=" + "\n" + "FolderType=StorageProviderGeneric"
iniline = iniline1 + "\n" + iniline2 + "\n" + iniline3

# 写入 desktop.ini
with open(ini_file_path, "w", encoding='utf-8') as inifile:
inifile.write(iniline)
inifile.close()

# 隐藏 desktop.ini 文件 & .ico 文件
cmd1 = icon_dir[0:2]
cmd2 = "cd " + '\"' + icon_dir + '\"'
cmd3 = "attrib +h +s " + 'desktop.ini'
cmd4 = "attrib +h +s " + icon_name
cmd = cmd1 + " & " + cmd2 + " & " + cmd3 + " & " + cmd4
os.system(cmd) # 运行 cmd
Renamer.logger.info(f'[{rjcode}] -> 修改封面成功:"{icon_name}"')

if self.__remove_jpg_file:
# 删除 .jpg 文件
Expand Down

0 comments on commit c7c711c

Please sign in to comment.