From 39e2e2cce0b1af28d9d86833597facc4c2b30b84 Mon Sep 17 00:00:00 2001 From: lobanov Date: Wed, 28 Sep 2022 16:58:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + csv_parser.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/.gitignore b/.gitignore index 129f236..5a18b03 100644 --- a/.gitignore +++ b/.gitignore @@ -153,3 +153,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +/csv_files/ diff --git a/csv_parser.py b/csv_parser.py index e69de29..46d347c 100644 --- a/csv_parser.py +++ b/csv_parser.py @@ -0,0 +1,65 @@ +import csv +import os +from datetime import datetime, timedelta +from glob import glob +import shutil +import webbrowser +from time import sleep, time + +# папка для загрузки файлов с помощью браузере, должна быть пустой +in_dir_download_files = '/home/lobanov/Загрузки' +# список файлов csv в начальной директории +list_files = [file for file in os.listdir('csv_files') if file.endswith('.csv')] +dt_start = datetime.now().utcnow() +print(dt_start) +for file in list_files: + try: + os.mkdir(f'{file[0:-4]}') # создаем директорию для файлов + except FileExistsError: + pass + print(file[0:-4]) + with open(f'csv_files/{file}', encoding='utf-8') as f: + cnt_document = 0 + parse_project = list(csv.reader(f, delimiter=';')) + for idx, row in enumerate(parse_project): + if row[8] and row[8] != 'Прикрепленные файлы': + temp_address = [] + sleep(1) + lst_address = row[8].split('\n') + for address in lst_address: + cnt_document += 1 + file_not_exist = False + start_time = time() + webbrowser.open(address) + sleep(0.3) + # проверяем и если надо, ждем, начался ли скачиваться файл, скачался ли он, файлов больше 1го + while len(os.listdir(in_dir_download_files)) == 0 or glob(f'{in_dir_download_files}/*.part') or len( + os.listdir(in_dir_download_files)) > 1: + if time() - start_time > 10 and len(os.listdir(in_dir_download_files)) == 0: + print('-------------------------') + print(f'файл не найден примерная строка {idx}') + print('-------------------------') + file_not_exist = True + break + sleep(0.5) + if file_not_exist: + continue + try: + new_path_to_file = shutil.move( + f'{in_dir_download_files}/{os.listdir(in_dir_download_files)[0]}', f'{file[0:-4]}') + except shutil.Error: + new_path_to_file = shutil.move( + f'{in_dir_download_files}/{os.listdir(in_dir_download_files)[0]}', + f'{file[0:-4]}/{idx}_{os.listdir(in_dir_download_files)[0]}') + temp_address.append(new_path_to_file) + row[8] = '\n'.join(temp_address) + with open(f'{file[0:-4]}_local.csv', 'w') as fp: + file_writer = csv.writer(fp, delimiter=';') + file_writer.writerows(parse_project) + print(cnt_document, 'Количество ссылок') + print(len(os.listdir(file[0:-4])), 'Количество документов') + sleep(5) # перерыв между файлами csv + dt_stop = datetime.now().utcnow() + print(dt_stop, 'stop date') + if dt_stop - timedelta(hours=14) > dt_start: + break