1
0

plagiat.py 3.1 KB

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