Skip to content

Instantly share code, notes, and snippets.

@Brandon-Rozek
Created June 12, 2022 22:33
Show Gist options
  • Save Brandon-Rozek/294fcd59c8d65dc49b4318c71e949c29 to your computer and use it in GitHub Desktop.
Save Brandon-Rozek/294fcd59c8d65dc49b4318c71e949c29 to your computer and use it in GitHub Desktop.
Grab photo information from a Google Photos Album URL (Not maintained)
"""
Tool to grab list of photo information (URLs/etc) of
images in Google Photos album.
Adapted from Matthieu Bessat
https://github.com/lefuturiste/google-photos-album-crawler/blob/f33026d379058912c597d5802296b55b62ed27a4/src/Crawler.php
"""
import json
import re
import sys
from http.client import HTTPResponse
from typing import Optional
from urllib import request
ALBUM_URL = "https://photos.app.goo.gl/7drjX2h4nWoLUsgE9"
# Get HTML from Album URL
response: Optional[HTTPResponse] = None
try:
response = request.urlopen(ALBUM_URL)
except Exception:
print("Unable to obtain response from Google Photos")
if response is None:
sys.exit(-1)
# Parse out the relevant data JSON from the HTML
rexp = r'<script nonce=".+">AF_initDataCallback\(\{.+, data:(.+), sideChannel:.+\);<\/script>'
match = re.search(rexp, str(response.read()))
if len(match.groups()) != 1:
print("Unable to identify data dictionary from server response")
sys.exit(-1)
# Parse JSON data dictionary
jsonResponse: Optional[list] = None
try:
jsonResponse = json.loads(match.group(1))
except Exception:
print("Unable to parse JSON response from server")
sys.exit(-1)
if len(jsonResponse) < 1:
print("Unexpected JSON response from server")
sys.exit(-1)
# Obtain photo information
jsonResult = [
{
'id': image[0],
'url': image[1][0],
'width': image[1][1],
'height': image[1][2]
}
for image in jsonResponse[1]
]
print(json.dumps(jsonResult))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment