plagiat.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. https://www.cyberforum.ru/python-graphics/thread2712141.html
  3. """
  4. import os
  5. from difflib import SequenceMatcher
  6. from tqdm import tqdm
  7. import datetime
  8. # директория файла
  9. BASE_DIR = os.path.abspath(os.path.dirname(__file__))
  10. print()
  11. files_paths = []
  12. exec_files_paths = [] # уже проверенные
  13. remove_files_paths = [] # файлы на удаление
  14. dirs = os.listdir(BASE_DIR)
  15. for dir in dirs:
  16. dir_path = os.path.join(BASE_DIR, dir)
  17. if os.path.isdir(dir_path) and (dir != "__pycache__"):
  18. files = os.listdir(dir_path)
  19. for file in files:
  20. file_path = os.path.join(BASE_DIR, dir, file)
  21. filename, fileext = os.path.splitext(file)
  22. if os.path.isfile(file_path) and (fileext=='.md'):
  23. files_paths.append(file_path)
  24. now = datetime.datetime.now().strftime('%d-%m-%Y %H:%M')
  25. out_str = f"Время проверки: {now} \n"
  26. print(out_str)
  27. for file_1 in tqdm(files_paths):
  28. for file_2 in files_paths:
  29. if (file_1 != file_2):
  30. small_filename_1 = str(file_1).replace(BASE_DIR, "").strip("\\")
  31. small_filename_2 = str(file_2).replace(BASE_DIR, "").strip("\\")
  32. if not (f"{small_filename_2}|{small_filename_1}") in exec_files_paths: # проверка на уже пройденное сравнение
  33. try:
  34. with open(file_1, encoding="utf-8") as f_1:
  35. str1 = f_1.read()
  36. except:
  37. with open(file_1, encoding="cp1251") as f_1:
  38. str1 = f_1.read()
  39. f_1.close()
  40. with open(file_1, 'w', encoding="utf-8") as f_1:
  41. f_1.write(str1)
  42. f_1.close()
  43. try:
  44. with open(file_2, encoding="utf-8") as f_2:
  45. str2 = f_2.read()
  46. except:
  47. with open(file_2, encoding="cp1251") as f_2:
  48. str2 = f_2.read()
  49. f_2.close()
  50. with open(file_2, 'w', encoding="utf-8") as f_2:
  51. f_2.write(str2)
  52. f_2.close()
  53. ratio = int(SequenceMatcher(None, str1.lower(), str2.lower()).ratio() * 100)
  54. if (ratio > 70):
  55. # время создания файла
  56. later_file = small_filename_1
  57. early_file = small_filename_2
  58. if (os.path.getctime(file_1) < os.path.getctime(file_2)):
  59. early_file = small_filename_1
  60. later_file = small_filename_2
  61. out_str += f"{later_file} позже {early_file}\n"
  62. out_str += f"ratio = {ratio}, удален\n"
  63. remove_files_paths.append(os.path.join(BASE_DIR, later_file))
  64. exec_files_paths.append(f"{small_filename_1}|{small_filename_2}")
  65. out_str +="\n\n"
  66. print(out_str)
  67. # запись лога
  68. log_path = os.path.join(BASE_DIR, "log.md")
  69. with open(log_path, "r", encoding="utf-8") as f_log:
  70. prev_str = f_log.read()
  71. prev_str = out_str + prev_str
  72. with open(log_path, "w", encoding="utf-8") as f_log:
  73. f_log.write(prev_str)
  74. f_log.close()
  75. # удаление файлов
  76. for remove_path in remove_files_paths:
  77. os.remove(remove_path)
  78. print("success")