moveBlog.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import os
  2. from pickle import FALSE, TRUE
  3. from secrets import choice
  4. import shutil
  5. import os
  6. import re
  7. import time
  8. BlogMdPath = os.path.dirname(os.getcwd()) + "\myblog\content\post"
  9. BlogPicPath = os.path.dirname(os.getcwd()) + "\myblog\static"
  10. NotePath = os.getcwd().replace('\\','/') + str("\笔记文件\\2.笔记")
  11. PicPath = os.getcwd().replace('\\','/') + str("\笔记文件\\附件")
  12. outStr = open(r'out.md','r',encoding="utf-8")
  13. outStrData = outStr.read()
  14. inStr= open(r'in.md','r',encoding="utf-8")
  15. inStrData = inStr.read()
  16. usualStr = open(r'usual.md','r',encoding="utf-8")
  17. usualStrData = usualStr.read()
  18. currentPath = os.getcwd().replace('\\','/') # 获取当前路径
  19. fileList = [] #文本列表容器
  20. dirPath = []
  21. allMdPath = []
  22. allPicPath = []
  23. blogOutPath = []
  24. pattern = re.compile(r'\[\[.*?\]\]')
  25. def ChangeDir(path):
  26. global currentPath
  27. os.chdir(path)
  28. currentPath = os.getcwd().replace('\\','/')
  29. dataNames = os.listdir(currentPath)
  30. for fileObj in dataNames:
  31. name = str(fileObj)
  32. if(".md" in name):
  33. file = open(name,'r',encoding= "utf-8")
  34. fileList =file.readlines()
  35. for i in range(len(fileList)):
  36. if('#unity/教程' in fileList[i]):
  37. dirPath.append(name)
  38. if(name not in allMdPath):
  39. allMdPath.append(name)
  40. blogOutPath.append(name.replace(' ',''))
  41. for dirfile in dirPath:
  42. file = open(dirfile,'r',encoding= "utf-8")
  43. fileList =file.readlines()
  44. for i in range(len(fileList)):
  45. if(r'[[' in fileList[i] and r']]' in fileList[i]):
  46. if("|" in fileList[i]):
  47. name = str(fileList[i].replace(r'[[','').replace(']]','').replace("\n","").split('|')[0])
  48. else:
  49. name = str(fileList[i].replace(r'[[','').replace(']]','').replace("\n",""))
  50. CheckLink(name)
  51. def CheckLink(fileName):
  52. if(r'.png' not in fileName and r'.jpg' not in fileName):
  53. newName = str(fileName.replace(r'[[','').replace(r']]','')+".md")
  54. if(newName not in allMdPath):
  55. allMdPath.append(newName)
  56. file = open(newName,'r',encoding= "utf-8")
  57. fileList =file.readlines()
  58. for i in range(len(fileList)):
  59. matches = pattern.findall(fileList[i])
  60. for match in matches:
  61. CheckLink(str(match.replace(r'[[','').replace(']]','').split('|')[0]))
  62. def ChangeTxt(fileObj):
  63. global outStrData
  64. global inStrData
  65. global usualStrData
  66. trueName = fileObj.split('.')[0]
  67. modify_time = os.path.getmtime(fileObj)
  68. modify_time_local = time.localtime(modify_time)
  69. file = open(fileObj,'r',encoding= "utf-8")
  70. filedata = file.read()
  71. timeStr = str(time.strftime('%Y-%m-%d', modify_time_local)).replace("2024-04-24","2024-04-22").replace("2024-04-23","2024-04-22")
  72. if(fileObj in blogOutPath):
  73. newhearstrdata = outStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"')
  74. else:
  75. if(r'#unity/日常积累' in filedata):
  76. newhearstrdata = usualStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"')
  77. else:
  78. newhearstrdata = inStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"')
  79. togetherTxt = newhearstrdata + filedata
  80. matches = pattern.findall(togetherTxt)
  81. allnewStrDic = {}
  82. for match in matches:
  83. if(r'.png' not in match):
  84. newName = str(match.replace(r'[[','').replace(r']]','').split('|')[0])
  85. newStr = r'[' + newName + r']({{< ref "/post/' + newName.replace(' ','') + r'.md"' + r" >}})"
  86. elif(r'.png' in match):
  87. newName = str(match.replace(r'[[','').replace(r']]',''))
  88. allPicPath.append(newName)
  89. newStr = r'[](/'+newName.replace(' ','')+r')'
  90. allnewStrDic[match] = newStr
  91. for key,value in allnewStrDic.items():
  92. togetherTxt = togetherTxt.replace(key,value)
  93. return togetherTxt
  94. def CopyMdFile():
  95. for mdName in allMdPath:
  96. name = str(mdName)
  97. shutil.copy2(currentPath + "/" + name,BlogMdPath + "/" + name.strip().replace(' ',''))
  98. def CopyPicFile():
  99. for picName in allPicPath:
  100. name = str(picName)
  101. shutil.copy2(PicPath + "/" + name,BlogPicPath + "/" + name.strip().replace(' ',''))
  102. def ChangeMdFile():
  103. os.chdir(BlogMdPath)
  104. currentPath = os.getcwd().replace('\\','/')
  105. dataNames = os.listdir(currentPath)
  106. for fileObj in dataNames:
  107. name = str(fileObj)
  108. newTxt = ChangeTxt(fileObj)
  109. with open(name,'w',encoding= "utf-8") as file:
  110. file.write(newTxt)
  111. def delete_files_in_folder(folder_path):
  112. for filename in os.listdir(folder_path):
  113. file_path = os.path.join(folder_path, filename)
  114. try:
  115. if os.path.isfile(file_path) or os.path.islink(file_path):
  116. os.unlink(file_path)
  117. elif os.path.isdir(file_path):
  118. shutil.rmtree(file_path)
  119. except Exception as e:
  120. print(f'Failed to delete {file_path}. Reason: {e}')
  121. ChangeDir(NotePath)
  122. delete_files_in_folder(BlogMdPath)
  123. CopyMdFile()
  124. ChangeMdFile()
  125. delete_files_in_folder(BlogPicPath)
  126. CopyPicFile()