-
Notifications
You must be signed in to change notification settings - Fork 29
/
gparch.py
520 lines (451 loc) · 18.1 KB
/
gparch.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import io
import json
import os
import pickle
import sqlite3
from multiprocessing.pool import ThreadPool
from time import time
import piexif
import piexif.helper
import requests
from google.auth.transport.requests import Request
from sanitize_filename import sanitize
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from PIL import Image
from tqdm import tqdm
"""
Archiver for Google Photos
By: Nick Dawson | [email protected]
"""
"""
Archiver For Google Photos
- A tool to maintain an archive/mirror of your Google Photos library for backup purposes.
Copyright (C) 2021 Nicholas Dawson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
VERSION = "2.2.0"
# Define Scopes for Application
SCOPES = [
"https://www.googleapis.com/auth/photoslibrary.readonly", # Read Only Photos Library API
]
# Define constants
DATABASE_NAME = "database.sqlite3"
def safe_mkdir(path):
"""
Creates directory only if it doesn't exist already to prevent errors
"""
if not os.path.exists(path):
os.mkdir(path)
def auto_mkdir(path, instance=0):
"""
Recursively creates directory and appends a number -> (#) to the end
if that directory already exists
"""
if instance:
new_path = path + " (" + str(instance) + ")"
else:
new_path = path
if not os.path.exists(new_path):
os.mkdir(new_path)
return os.path.abspath(new_path)
else:
return auto_mkdir(path, instance + 1)
def auto_filename(path, instance=0):
"""
Recursively finds an available name for a new file and
appends a number -> (#) to the end if that file already exists
"""
if instance:
extension_index = path.rfind(".")
new_path = (
path[:extension_index] + " (" + str(instance) + ")" + path[extension_index:]
)
else:
new_path = path
if not os.path.exists(new_path):
return new_path
else:
return auto_filename(path, instance + 1)
def save_json(variable, path):
json.dump(variable, open(auto_filename(path), "w"))
def load_json(path):
# If file exists load the json as a dict
if os.path.isfile(path):
return json.load(open(path, "r"))
# If file doesn't exist return None
else:
return None
def load_database(path, init_dict):
# Create and/or Load the databases
db = load_json(path)
if db is None:
db = init_dict
save_json(db, path)
return db
class PhotosAccount(object):
def __init__(self, credentials_path, directory, thread_count, debug):
# Define directory instance variables
self.base_dir = directory
self.lib_dir = self.base_dir + "/Library"
self.albums_dir = self.base_dir + "/Albums"
self.shared_albums_dir = self.base_dir + "/Shared Albums"
self.favorites_dir = self.base_dir + "/Favorites"
# Define/initialize other instance variables
self.thread_count = thread_count
self.credentials = credentials_path
self.service = None # is None because it will be defined later by calling "get_google_api_service"
self.timer = time()
self.downloads = 0
self.debug = debug
if self.debug:
safe_mkdir("debug")
# Define/Init Database
self.db_path = self.base_dir + "/" + DATABASE_NAME
self.con = self.init_db()
self.cur = self.con.cursor()
# Create the directories (if not already there)
safe_mkdir(self.base_dir)
safe_mkdir(self.lib_dir)
safe_mkdir(self.albums_dir)
safe_mkdir(self.shared_albums_dir)
safe_mkdir(self.favorites_dir)
def get_google_api_service(self):
# The file photos_token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time.
credentials = None
token_path = self.base_dir + "/photoslibrary_token.pickle"
if os.path.exists(token_path):
with open(token_path, "rb") as token:
credentials = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
if not os.path.exists(self.credentials):
raise FileNotFoundError(self.credentials + " is not found.")
flow = InstalledAppFlow.from_client_secrets_file(
self.credentials, SCOPES
)
credentials = flow.run_local_server()
# Save the credentials for the next run
with open(token_path, "wb") as token:
pickle.dump(credentials, token)
self.service = build(
"photoslibrary", "v1", credentials=credentials, static_discovery=False
)
def init_db(self):
if not os.path.exists(self.db_path):
con = sqlite3.connect(self.db_path)
cur = con.cursor()
# Create Media Table - Used to store basic information about each media item
cur.execute(
"""CREATE TABLE media (uuid text, path text, album_uuid text)"""
)
# Create Albums Table - Used to store information about each album
cur.execute(
"""CREATE TABLE albums (uuid text, path text, title text, is_shared integer)"""
)
con.commit() # Save changes
return con
else:
return sqlite3.connect(self.db_path)
def get_session_stats(self):
return time() - self.timer, self.downloads
def download_media_item(self, entry):
try:
uuid, album_uuid, url, path, description = entry
if not os.path.isfile(path):
r = requests.get(url)
if r.status_code == 200:
path = auto_filename(path)
if description:
try:
img = Image.open(io.BytesIO(r.content))
exif_dict = piexif.load(img.info["exif"])
exif_dict["Exif"][
piexif.ExifIFD.UserComment
] = piexif.helper.UserComment.dump(
description, encoding="unicode"
)
# This is a known bug with piexif (https://github.com/hMatoba/Piexif/issues/95)
if 41729 in exif_dict["Exif"]:
exif_dict["Exif"][41729] = bytes(
exif_dict["Exif"][41729]
)
exif_bytes = piexif.dump(exif_dict)
img.save(path, exif=exif_bytes)
except ValueError:
# This value here is to catch a specific scenario with file extensions that have
# descriptions that are unsupported by Pillow so the program can't modify the EXIF data.
print(
" [INFO] media file unsupported, can't write description to EXIF data."
)
open(path, "wb").write(r.content)
else:
open(path, "wb").write(r.content)
self.downloads += 1
return (
uuid,
path,
album_uuid,
)
else:
return False
except Exception as e:
print(" [ERROR] media item could not be downloaded because:", e)
return False
def download(self, entries, desc, thread_count):
result = ThreadPool(thread_count).imap_unordered(
self.download_media_item, entries
)
for downloaded_entry in tqdm(
result, unit=" media items", total=len(entries), desc=desc
):
if downloaded_entry:
uuid, path, album_uuid = downloaded_entry
self.insert_media_item(
uuid,
path,
album_uuid,
)
def select_media_item(self, uuid):
return self.cur.execute(
"""SELECT * FROM media WHERE uuid=?""", (uuid,)
).fetchone()
def insert_media_item(self, uuid, path, album_uuid):
self.cur.execute(
"""INSERT INTO media VALUES (?, ?, ?)""", (uuid, path, album_uuid)
)
self.con.commit()
def select_album(self, uuid):
return self.cur.execute(
"""SELECT * FROM albums WHERE uuid=?""", (uuid,)
).fetchone()
def insert_album(self, uuid, path, title, is_shared=False):
self.cur.execute(
"""INSERT INTO albums VALUES (?, ?, ?, ?)""", (uuid, path, title, is_shared)
)
self.con.commit()
def process_media_items(self, media_items, save_directory, album_uuid=None):
media = []
for item in media_items:
# Path where the media item will be saved to
item_path = None
# Select the media item from the database
# -> if it doesn't exist then generate the item_path
# -> if it already exists then just pull the item_path from the existing db entry
item_db_entry = self.select_media_item(item["id"])
if not item_db_entry:
item["filename"] = sanitize(item["filename"])
item_path = f'{save_directory}/{item["filename"]}'
else:
item_path = item_db_entry[1]
# Set description to none if not there so a key error won't occur below
# This keeps the code simpler when dealing with descriptions
if "description" not in item:
item["description"] = None
# Process Media
# - Image
if "image" in item["mimeType"]:
media.append(
(
item["id"],
album_uuid,
item["baseUrl"] + "=d",
item_path,
item["description"],
)
)
# - Video
elif "video" in item["mimeType"]:
media.append(
(
item["id"],
album_uuid,
item["baseUrl"] + "=dv",
item_path,
item["description"],
)
)
return media
def download_library(self):
items = self.process_media_items(self.list_media_items(), self.lib_dir)
self.download(items, "Downloading Library", self.thread_count)
def download_favorites(self):
items = self.process_media_items(self.search_favorites(), self.favorites_dir)
self.download(items, "Downloading Favorites", self.thread_count)
def download_all_albums(self):
for album in self.list_albums():
self.download_single_album(album)
def download_all_shared_albums(self):
for album in self.list_shared_albums():
self.download_single_album(album, True)
def download_single_album(self, album, shared=False):
# Return if the album has no mediaItems to download
# Unsure of how this occurs, but there are album entries that exist
# where there I don't have permission, weird bug...
if "mediaItemsCount" not in album:
return
# Next check to see if the album has a title, if it doesn't give it default name
if "title" not in album:
album["title"] = "Unnamed Album"
# Sanitize album title
album["title"] = sanitize(album["title"])
# Make request
album_items = []
request_body = {
"albumId": album["id"],
"pageSize": 100, # Max is 100
"pageToken": "",
}
num = 0
request = (
self.service.mediaItems().search(body=request_body).execute()
) # 100 is max
if not request:
return
while True:
if "mediaItems" in request:
album_items += request["mediaItems"]
if "nextPageToken" in request:
request_body["pageToken"] = request["nextPageToken"]
request = self.service.mediaItems().search(body=request_body).execute()
else:
break
if self.debug:
save_json(album_items, "debug/" + album["title"] + ".json")
# Directory where the album exists
album_path = None
# Select the album item from the database
# -> if it exists then insert a new album entry and set the album_path to the newly set path
# -> if it already exists then just pull the album_path from the existing db entry
album_db_entry = self.select_album(album["id"])
if album_db_entry:
album_path = album_db_entry[1]
elif not shared:
album_path = auto_mkdir(self.albums_dir + "/" + album["title"])
self.insert_album(album["id"], album_path, album["title"], shared)
else:
album_path = auto_mkdir(self.shared_albums_dir + "/" + album["title"])
self.insert_album(album["id"], album_path, album["title"], shared)
processed_items = self.process_media_items(album_items, album_path, album["id"])
if processed_items:
self.download(
processed_items,
f"Downloading {'Shared ' if shared else ''}Album: \"{album['title']}\"",
self.thread_count,
)
else:
print(
f"Downloading {'Shared ' if shared else ''}Album: \"{album['title']}\""
)
print("Everything already downloaded.")
def list_media_items(self):
num = 0
media_items_list = []
request = self.service.mediaItems().list(pageSize=100).execute() # Max is 50
if not request:
return {}
while True:
if self.debug:
save_json(request, "debug/media" + str(num) + ".json")
if "mediaItems" in request:
media_items_list += request["mediaItems"]
if "nextPageToken" in request:
next_page = request["nextPageToken"]
request = (
self.service.mediaItems()
.list(pageSize=100, pageToken=next_page)
.execute()
)
else:
break
num += 1
if self.debug:
save_json(media_items_list, "debug/media_items_list.json")
return media_items_list
def list_albums(self):
num = 0
album_list = []
request = self.service.albums().list(pageSize=50).execute() # Max is 50
if not request:
return {}
while True:
if self.debug:
save_json(request, "debug/albums" + str(num) + ".json")
if "albums" in request:
album_list += request["albums"]
if "nextPageToken" in request:
next_page = request["nextPageToken"]
request = (
self.service.albums()
.list(pageSize=50, pageToken=next_page)
.execute()
)
else:
break
num += 1
if self.debug:
save_json(album_list, "debug/album_list.json")
return album_list
def list_shared_albums(self):
shared_album_list = []
request = self.service.sharedAlbums().list(pageSize=50).execute() # Max is 50
num = 0
if not request:
return {}
while True:
if self.debug:
save_json(request, "debug/shared_albums" + str(num) + ".json")
shared_album_list += request["sharedAlbums"]
if "nextPageToken" in request:
next_page = request["nextPageToken"]
request = (
self.service.sharedAlbums()
.list(pageSize=50, pageToken=next_page)
.execute()
)
else:
break
num += 1
if self.debug:
save_json(shared_album_list, "debug/shared_album_list.json")
return shared_album_list
def search_favorites(self):
# Form request body using media_types_list above
request_body = {
"filters": {"featureFilter": {"includedFeatures": ["FAVORITES"]}},
"pageSize": 100, # Max is 100
"pageToken": "",
}
num = 0
# Make request
favorites_list = []
request = self.service.mediaItems().search(body=request_body).execute()
if not request:
return {}
while True:
if self.debug:
save_json(request, "debug/favorites" + str(num) + ".json")
if "mediaItems" in request:
favorites_list += request["mediaItems"]
if "nextPageToken" in request:
request_body["pageToken"] = request["nextPageToken"]
request = self.service.mediaItems().search(body=request_body).execute()
else:
break
num += 1
if self.debug:
save_json(favorites_list, "debug/favorites_list.json")
return favorites_list