Skip to content

Commit

Permalink
Main: added backuping logic for json files
Browse files Browse the repository at this point in the history
  • Loading branch information
DvaMishkiLapa committed May 19, 2024
1 parent a4548ba commit f029eb2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ async def main():
if delete_output_folder:
logger.info(f'📁 {output_folder} будет отчищена перед началом работы 🗑️')
tools.clear_folder(output_folder)
tools.clear_jsons(output_folder)
else:
logger.info(f'📁 {output_folder} останется нетронутой')

Expand All @@ -403,7 +404,10 @@ async def main():
tools.create_folder(join(output_folder, folder))

if 'DEBUG' in log_level:
with open(join(output_folder, 'dirty_links.json'), 'w', encoding='utf8') as f:
dirty_links_path = join(output_folder, 'dirty_links.json')
if not delete_output_folder:
tools.backup_file(dirty_links_path)
with open(dirty_links_path, 'w', encoding='utf8') as f:
f.write(dumps(obj.link_info, indent=4, ensure_ascii=False))

disable_ssl = config['main_parameters'].getboolean('disable_ssl', False)
Expand Down Expand Up @@ -432,7 +436,10 @@ async def main():

logger.info(f'Количество обработанных 🔗: {full_count}')
logger.info(f'⌛ обработки 🔗 и скачивания возможных: {full_end - start}')
with open(join(output_folder, 'links_info.json'), 'w', encoding='utf8') as f:
links_info_path = join(output_folder, 'links_info.json')
if not delete_output_folder:
tools.backup_file(links_info_path)
with open(links_info_path, 'w', encoding='utf8') as f:
f.write(dumps(result, indent=4, ensure_ascii=False))
logger.info(f'Общее ⌛ обработки архива VK: {full_end - first_start}')

Expand Down
21 changes: 19 additions & 2 deletions tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from configparser import ConfigParser
from os import listdir, makedirs, remove
from os.path import exists, isdir, join
from datetime import datetime
from os import listdir, makedirs, remove, rename
from os.path import exists, getmtime, isdir, join, split, splitext
from shutil import rmtree
from typing import Generator

Expand Down Expand Up @@ -90,3 +91,19 @@ def clear_jsons(path: str) -> None:
for f in listdir_nohidden(path):
if '.json' in f:
remove(join(path, f))


def backup_file(file_path: str):
"""
Функция для создания резервной копии файла (если файл существует) и сохранения данных.
`file_path`: Путь к файлу, в который будут сохраняться данные.
`data`: Словарь с данными, которые будут записаны в файл.
"""
if exists(file_path):
creation_date = datetime.fromtimestamp(getmtime(file_path)).strftime('%d-%m-%Y-%H-%M-%S')
base_dir, filename = split(file_path)
filename_base, ext = splitext(filename)
new_filename = f"{filename_base}-{creation_date}{ext}"
new_filepath = join(base_dir, new_filename)
rename(file_path, new_filepath)

0 comments on commit f029eb2

Please sign in to comment.