Glider
Loading...
Searching...
No Matches
FileCleaning.py
Go to the documentation of this file.
1import os
2import sys
3import json
4import boto3
5import traceback
6from datetime import datetime
7from dotenv import load_dotenv
8f_path = __file__
9index = f_path.find("upload_module/")
10f_path = f_path[:index+len("upload_module/")]
11sys.path.insert(1, f_path+"common")
12from DownloadFiles import download_file_or_folder
13from UploadFilesS3 import clean_files
14from Tools import gen_file_list_drive, gen_file_list_event, send_notification
15
16load_dotenv()
17
18def get_path(local_path):
19 """
20 Adds the last folder downloaded to root path
21
22 Args:
23 local_path (str): Root path where is saved files
24 Returns : The actual folder where files where downloaded
25 """
26 path_child = os.popen("ls {}".format(local_path)).read()
27 local_path_full = local_path+path_child
28 local_path_full = local_path_full.strip("\n")
29 local_path_renamed = local_path_full.replace(" ", "_")
30 os.rename(local_path_full, local_path_renamed)
31 if os.path.isdir(local_path_renamed):
32 return local_path_renamed+"/"
33 return local_path
34
35def download_local_file(event, local_path):
36 """Downloads files uploaded directly via Otto Light interface in a temporal folder
37
38 Args:
39 event (dict): Contains the bucket and folder info where file was uploaded
40 local_path (str): Root path where is saved files
41 Returns : Nothing
42 """
43 # Access keys for AWS
44 ACCESS_ID, ACCESS_KEY = os.environ.get("AWS_KEY_ID"), os.environ.get("AWS_KEY_SECRET")
45 # Connection for AWS using boto3
46 s3_client = boto3.client("s3", aws_access_key_id=ACCESS_ID, aws_secret_access_key=ACCESS_KEY)
47 bucket = event["bucket"]
48 s3_path = event["path"]
49 file = event["file"]
50 if type(file) == list and len(file) > 0:
51 uploaded_files = local_path+"uploaded_files/"
52 if not os.path.exists(uploaded_files):
53 os.mkdir(uploaded_files)
54 for i in file:
55 s3_client.download_file(bucket, s3_path+"/"+i, uploaded_files+i)
56 else:
57 s3_client.download_file(bucket, s3_path+"/"+file, local_path+file)
58
59
60
62 """
63 Transform Gdrive link to ID drive to be downloaded by Google API and execute all procedure
64
65 Args:
66 event (dict): Shared link of the folder/file to download
67 Returns : The actual folder where files where downloaded
68 """
69 _ROOT_DIR = os.path.expanduser('~')
70 path = _ROOT_DIR+"/GDrive_downloads/"
71 os.makedirs(path, exist_ok=True)
72 webhook_url = event["on_finish"]
73 try:
74 if event["drive_link"] != "":
75 drive_link = event["drive_link"]
76 if "folder" in drive_link:
77 # Example: https://drive.google.com/drive/folders/1l6A3zjNGw6qWJQ-mkeep4W4ooCw7uy6W?usp=share_link
78 drive_link = drive_link.split("folders/")[-1].split("?usp")[0]
79 elif "file" in drive_link:
80 # Example: https://drive.google.com/file/d/1o2q6RFa6wi7XMSJcZJhLtr-pNJxWQsVl/view?usp=share_link
81 drive_link = drive_link.split("d/")[-1].split("/")[0]
82 elif "spreadsheets" in drive_link:
83 drive_link = drive_link.split("spreadsheets/d/")[-1].split("/")[0]
84 download_file_or_folder(drive_link, path)
85 else:
86 download_local_file(event, path)
87 local_path = get_path(path)
88 print(local_path)
89 path_output, files = clean_files(event, local_path)
90 os.system("rm -r {}*".format(path))
91 print(f"Files uploaded to {path_output}")
92 # if event["file"] == "" or event["file"] == []:
93 # files = gen_file_list_drive(path_output)
94 # else:
95 # files = gen_file_list_event(event, path_output)
96 # send_notification(event["module"], webhook_url, "Successful", files=files)
97 return path_output, files
98 except Exception as e:
99 raise Exception("An error occurred: {}".format(e))
100 # print("An error occurred: {}".format(e))
101 # # print(sys.exc_info()[2])
102 # # print(traceback.format_exc())
103 # m = "{}\n{}".format(sys.exc_info()[2], traceback.format_exc())
104 # send_notification(event["module"], webhook_url, "Failed", error=m)