plagiat.py 3.5 KB

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