-
Notifications
You must be signed in to change notification settings - Fork 14
/
slides.py
288 lines (230 loc) · 9.94 KB
/
slides.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Create or Select a Project in Google Cloud Console:
Go to the Google Cloud Console.
If you haven't already created a project, you will need to create one. Otherwise, select an existing project.
Enable the Google Drive API:
In the dashboard of your project, navigate to the "APIs & Services > Library".
Search for "Google Drive API" and select it.
Click "Enable" to enable the Google Drive API for your project.
Create Credentials:
After enabling the API, go to "APIs & Services > Credentials".
Click "Create Credentials" at the top of the page.
Choose “OAuth client ID”.
Configure the OAuth consent screen:
You'll be prompted to configure the OAuth consent screen before creating credentials. This is the screen that users will see when they authenticate with your application.
Make sure to add a test user that is your mail.
Create OAuth 2.0 Client ID:
After configuring the consent screen, you’ll be taken back to the "Create credentials" screen.
For "Application type", select "Web application" or another type relevant to your application.
Set a name for the OAuth 2.0 client.
Under "Authorized redirect URIs", add http://localhost:8080/.
Click “Create”.
Download the Credentials:
credentials.json
After creating the client ID, you'll see a confirmation screen showing your client ID and client secret.
Click the download button (it looks like a download icon) to download the JSON file containing your credentials.
Rename this file to credentials.json and place it in the directory of your Python script.
Install Required Libraries:
If you haven’t installed the required libraries for the Google API client and OAuth, you can install them via pip:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
"""
import io
import json
import os
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
def get_credentials():
scopes = ["https://www.googleapis.com/auth/drive"]
creds = None
# The file token.json stores the user's access and refresh tokens.
if os.path.exists("secrets/token.json"):
creds = Credentials.from_authorized_user_file("secrets/token.json", scopes)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
assert os.path.isfile(
"secrets/credentials.json"
), "You need secrets/credentials.json. Download it from client secrets in: https://console.cloud.google.com/apis/credentials/oauthclient/385659825496-0123vosqnmabsha0bkdfahktrhqg5d8v.apps.googleusercontent.com?project=slides-to-pdf-412609"
flow = InstalledAppFlow.from_client_secrets_file(
"secrets/credentials.json", scopes
)
creds = flow.run_local_server(port=8080)
# Save the credentials for the next run
with open("secrets/token.json", "w") as token:
token.write(creds.to_json())
return creds
def download_as_pdf(service, file_id, output_file_path):
request = service.files().export_media(fileId=file_id, mimeType="application/pdf")
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
print(f"Download PDF to {output_file_path}...")
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
with open(output_file_path, "wb") as f:
f.write(fh.getbuffer())
def find_item_id_by_path(service, path):
folder_id = "root" # start from the root
for name in path.split("/"):
if not name:
continue
query = f"name='{name}' and '{folder_id}' in parents"
response = (
service.files()
.list(
q=query, spaces="drive", fields="files(id, name, mimeType)", pageSize=10
)
.execute()
)
files = response.get("files", [])
if not files:
print(f"No such file/dir named {name} in path {path}")
return None
# Assuming the first found file/folder is the correct one
folder_id = files[0]["id"]
return folder_id
def generate_current_slides_info(service, folder_path):
"""
Retrieves all Google Slides files along with their last modified times within a specified directory on Google Drive.
:param service: Authorized Google Drive service instance.
:param folder_path: The path of the directory to search in, e.g., "CV Course/slides".
:return: A list of dictionaries containing file IDs, names, and last modified times of the Google Slides files.
"""
folder_id = find_item_id_by_path(service, folder_path)
slides_mime_type = "application/vnd.google-apps.presentation"
query = f"'{folder_id}' in parents and mimeType='{slides_mime_type}'"
response = (
service.files()
.list(
q=query,
spaces="drive",
fields="files(id, name, modifiedTime)",
pageSize=100, # Adjust pageSize as needed
)
.execute()
)
slides_info = response.get("files", [])
slides_info_dict = {item["id"]: item for item in slides_info}
return slides_info_dict
def write_slides_info_to_drive(service, folder_path, slides_files, json_filename):
"""
Writes data about Google Slides files to a JSON file and updates or uploads it to a specified directory on Google Drive.
"""
# Write the data to a JSON file locally
with open(json_filename, "w") as json_file:
json.dump(slides_files, json_file, indent=4)
# Check if the JSON file already exists in the specified folder
drive_json_id = find_item_id_by_path(
service, os.path.join(folder_path, json_filename)
)
file_metadata = {
"name": json_filename,
"parents": [find_item_id_by_path(service, folder_path)],
}
media = MediaFileUpload(json_filename, mimetype="application/json")
if drive_json_id is None:
# File doesn't exist, create a new one
file_metadata = {
"name": json_filename,
"parents": [find_item_id_by_path(service, folder_path)],
}
created_file = (
service.files()
.create(body=file_metadata, media_body=media, fields="id")
.execute()
)
print(
f"Uploaded {json_filename} to Google Drive with ID: {created_file.get('id')}"
)
else:
# File exists, update it
# Note: Removed 'parents' from file_metadata as it's not needed for update
file_metadata = {
"name": json_filename,
# Do not include 'parents' here for the update operation
}
updated_file = (
service.files()
.update(
fileId=drive_json_id, body=file_metadata, media_body=media, fields="id"
)
.execute()
)
print(
f"Updated {json_filename} in Google Drive with ID: {updated_file.get('id')}"
)
# Clean up the local file
os.remove(json_filename)
def read_json_from_drive(service, file_id):
"""
Reads data from a JSON file stored on Google Drive.
:param service: Authorized Google Drive service instance.
:param file_id: The ID of the JSON file to read data from.
:return: The data read from the JSON file.
"""
# Step 1: Download the file
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
_, done = downloader.next_chunk()
fh.seek(0)
# Step 2: Read the JSON data from the file
json_data = json.load(fh)
return json_data
def has_file_changed(old_info, new_info):
"""
Check if the file's modified time in the new info is more recent than in the old info.
"""
old_modified_time = old_info.get("modifiedTime")
new_modified_time = new_info.get("modifiedTime")
return new_modified_time > old_modified_time
def get_old_slides_info(folder_path, json_filename, service):
drive_json_id = find_item_id_by_path(
service, os.path.join(folder_path, json_filename)
)
if drive_json_id is None:
return {}
slides_info_dict = read_json_from_drive(service, drive_json_id)
# Convert json_data into a dict for easier comparison
return slides_info_dict
def generate_changed_slides_as_pdfs(
service, last_slides_info, slides_info, out_folder_path
):
for slide_id, slide_data in slides_info.items():
slides_info[slide_id]
if (
slide_id in last_slides_info
and has_file_changed(last_slides_info[slide_id], slide_data)
) or (slide_id not in last_slides_info):
output_filename = f"{slide_data['name']}.pdf"
download_as_pdf(
service, slide_id, os.path.join(out_folder_path, output_filename)
)
print(f"Updated slide downloaded as PDF: {output_filename}")
def connect_to_google_drive_service():
creds = get_credentials()
service = build("drive", "v3", credentials=creds)
return service
if __name__ == "__main__":
GOOGLE_DRIVE_SLIDES_FOLDER_PATH = "CV Course/slides"
JSON_FILENAME = "last_slides_info.json"
PDF_OUT_FOLDER_PATH = "/home/yoni/Desktop/AI_is_Math/lectures"
service = connect_to_google_drive_service()
last_slides_info = get_old_slides_info(
GOOGLE_DRIVE_SLIDES_FOLDER_PATH, JSON_FILENAME, service
)
slides_info = generate_current_slides_info(service, GOOGLE_DRIVE_SLIDES_FOLDER_PATH)
generate_changed_slides_as_pdfs(
service, last_slides_info, slides_info, PDF_OUT_FOLDER_PATH
)
write_slides_info_to_drive(
service, GOOGLE_DRIVE_SLIDES_FOLDER_PATH, slides_info, JSON_FILENAME
)