-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpresskit.py
102 lines (92 loc) · 3.57 KB
/
presskit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2.service_account import Credentials
import io
import zipfile
import shutil
# Credentials and folder ID
credentials_path = 'gregoryai-41cd67dab7a5.json'
folder_id = '1KuEj8mERv5FcLfmJ1hP840GMrREoJpRc'
scopes = ['https://www.googleapis.com/auth/drive.readonly']
# Authenticate and build service
credentials = Credentials.from_service_account_file(credentials_path, scopes=scopes)
service = build('drive', 'v3', credentials=credentials)
# Dictionary to hold folder ID to path mapping
def setup_dir(directory_name):
print('''
####
## Check for press kit directory
####
''')
if not os.path.exists(directory_name):
os.makedirs(directory_name)
print(f"The directory {directory_name} has been created.")
else:
if os.listdir(directory_name): # Check if the directory is not empty
print(f"The directory {directory_name} exists and is not empty. Cleaning it...")
for file_or_folder in os.listdir(directory_name):
full_path = os.path.join(directory_name, file_or_folder)
if os.path.isfile(full_path) or os.path.islink(full_path):
os.unlink(full_path) # Remove file or symbolic link
elif os.path.isdir(full_path):
shutil.rmtree(full_path) # Remove subdirectory
print(f"The directory {directory_name} has been cleaned.")
else:
print(f"The directory {directory_name} exists and is already empty.")
def create_zip_from_folder(folder_path, zip_name):
print('''
####
## Create zip file for press kit
####
''')
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Iterate through all the directories and files in the given folder
for root, dirs, files in os.walk(folder_path):
for file in files:
# Create a relative file path to keep the folder structure inside the ZIP
rel_path = os.path.relpath(os.path.join(root, file), os.path.join(folder_path, '..'))
zipf.write(os.path.join(root, file), rel_path)
# Recursive function to process folders
def process_folder(folder_id, directory_name, is_first_call=True):
if is_first_call:
print('''
####
## Download press kit files
####
''')
folder_structure = {folder_id: directory_name}
if not os.path.exists(directory_name):
os.makedirs(directory_name)
# Query for items in folder
query = f"'{folder_id}' in parents"
results = service.files().list(q=query, fields="files(id, name, mimeType)").execute()
items = results.get('files', [])
for item in items:
item_id = item['id']
item_name = item['name']
item_type = item['mimeType']
file_path = os.path.join(directory_name, item_name)
if item_type == 'application/vnd.google-apps.folder':
folder_structure[item_id] = os.path.join(directory_name, item_name)
process_folder(item_id, folder_structure[item_id], is_first_call=False)
else:
if item_type.startswith('application/vnd.google-apps.'):
# Export Google Docs Editors files as PDF
request = service.files().export_media(fileId=item_id, mimeType='application/pdf')
file_path += '.pdf' # Append PDF extension
else:
# Download binary files directly
request = service.files().get_media(fileId=item_id)
with io.FileIO(file_path, mode='wb') as file_handle:
downloader = MediaIoBaseDownload(file_handle, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(f'Download {int(status.progress() * 100)}% for {item_name}.')
print(f'{item_name} Download complete.')
###
# Usage
###
# process_folder(folder_id, directory_name )
# create_zip_from_folder(directory_name, 'content/press-kit/press-kit.zip')