-
Notifications
You must be signed in to change notification settings - Fork 6
/
google_photo_export.py
83 lines (62 loc) · 2.75 KB
/
google_photo_export.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
# google_photos_to_jekyll_single_post.py
import json
import os
import shutil
import zipfile
from datetime import datetime
def extract_google_photos(input_zip, output_folder):
with zipfile.ZipFile(input_zip, "r") as zip_ref:
zip_ref.extractall(output_folder)
metadata_files = []
for root, dirs, files in os.walk(output_folder):
for file in files:
if file.endswith(".json"):
metadata_files.append(os.path.join(root, file))
return metadata_files
def parse_metadata(metadata_files, extracted_photos_folder, jekyll_img_folder):
elements = []
for metadata_file in metadata_files:
with open(metadata_file, "r") as f:
metadata = json.load(f)
if "description" in metadata:
elements.append(
("text", metadata["creationTime"], metadata["description"])
)
if "photo" in metadata:
photo_filename = metadata["filename"]
elements.append(("photo", metadata["creationTime"], photo_filename))
shutil.copy2(
os.path.join(extracted_photos_folder, photo_filename),
os.path.join(jekyll_img_folder, photo_filename),
)
elements.sort(key=lambda x: x[1])
return elements
def create_jekyll_post(elements, jekyll_posts_folder):
post_date = datetime.now().strftime("%Y-%m-%d")
post_filename = f"{post_date}-google-photos-album.markdown"
with open(os.path.join(jekyll_posts_folder, post_filename), "w") as f:
f.write("---\n")
f.write("title: Google Photos Album\n")
f.write(f"date: {post_date}\n")
f.write("---\n\n")
for element in elements:
if element[0] == "photo":
f.write(f"![Photo]({{ site.baseurl }}/assets/img/{element[2]})\n\n")
elif element[0] == "text":
f.write(f"{element[2]}\n\n")
def generate_jekyll_blog(metadata_files, extracted_photos_folder, jekyll_folder):
jekyll_posts_folder = os.path.join(jekyll_folder, "_posts")
jekyll_img_folder = os.path.join(jekyll_folder, "assets", "img")
os.makedirs(jekyll_img_folder, exist_ok=True)
elements = parse_metadata(
metadata_files, extracted_photos_folder, jekyll_img_folder
)
create_jekyll_post(elements, jekyll_posts_folder)
print("Generated single Jekyll blog post with photos and text.")
if __name__ == "__main__":
input_zip = "path/to/your/google_photos.zip"
output_folder = "path/to/extracted_photos"
os.makedirs(output_folder, exist_ok=True)
metadata_files = extract_google_photos(input_zip, output_folder)
jekyll_folder = "path/to/my-jekyll-blog"
generate_jekyll_blog(metadata_files, output_folder, jekyll_folder)