Organize Google Photos originating from shared albums and WhatsApp. It processes a Google Takeout and adds all media (photos & videos) that are saved to your Photos feed from Partner Sharing and WhatsApp to new albums. You can then review the albums and delete the photos if everything looks good.
NB: Adding Photos to albums alone does not remove them from your main feed.
NB: Partner photos could probably be deleted more easily by just removing the partner. I'm not aware of an easier way to organize WhatsApp photos though.
- Traverses a Photos Takeout (code in
takeout.ts
), building a list of files which come from Partner sharing or WhatsApp respectively. - Using the Photos API (
photosApi.ts
), creates albums calledCleanup - Partner Photos
andCleanup - WhatsApp Photos
. - Fetches the Takeout private item URLs and parses the redirect targets to get the media item IDs for the Takeout items, caching to a sqlite database
links.sqlite
.- Cookies are loaded from a Firefox profile.
- Uses the Photos UI API (
photosWebApi.ts
) to add the items that aren't already in these albums respectively.- The lists of existing items in the albums are fetched with the Photos API.
NB: This tool is pretty flaky due to opaque rate-limits on the Photos UI API. You'll probably need to re-run it several times - it should be idempotent.
The official Google Photos API is not very useful:
- API 'apps' can only add photos created by the app to albums, so you can't use the API to organize an existing set of photos.
- There is no access to the partner sharing / WhatsApp source metadata.
- The 'Media Item' IDs it exposes are not included or readily mappable to the Takeout image URLs/identifiers.
So we use a Frankenstein combination of the Google Photos API, the undocumented & obfuscated Photos UI API, and Takeout data.
NB: It might be possible to cut Takeout out of the process if the Photos UI API has the relevant info - I'm not sure if it does or not.
- Google account
- Firefox install logged into that Google account (for filling the cookie jar in
photosWebApi.ts
).- The script could be adapted to use hardcoded cookies or another source.
git clone https://github.com/inversion/google-shared-photos-cleanup
cd google-shared-photos-cleanup
- Generate a Google Takeout - you can select 'Google Photos' only to save time generating it. I used
tgz
with 50GB segments. - Download the
tgz
files to the current directory. - Extract the metadata JSON from all of them:
for file in *.tgz; do tar -zxvf "$file" --wildcards --no-anchored '*/*.json'; done
- Extracting only the JSON saves time and disk space, but this might take a long time anyway - about 15-20 minutes with my ~150GB library.
- There should now be a 'Takeout' directory in the current directory.
- Create a project at console.cloud.google.com
- Activate the 'Photos Library API' from the search box.
- Hit 'Create Credentials'
- Select 'User data'.
- 'Add or Remove Scopes'
- Select
auth/photoslibrary
andauth/photoslibrary.sharing
. - Application type 'Desktop App'
- Download the client secret JSON and save it as
client_secret.json
in the current directory.
cp .env{.example,}
- Open
.env
:- Open photos.google.com with the logged-in Firefox.
- Set
WIZ_*
values in .env (based on https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c):- Open Firefox dev tools.
copy(window.WIZ_global_data.FdrFJe)
-> WIZ_SIDcopy(window.WIZ_global_data.SNlM0e)
-> WIZ_AT
- Set
FIREFOX_COOKIES_DB_PATH
to your real profile's path.
npm install
npm run main
- (First-run only) Follow the OAuth instructions to generate auth credentials for your Photos API app, which will be saved to
oauth_tokens.json
. - Watch the output and re-run it if it fails. You will probably need to update WIZ_SID and WIZ_AT if it starts consistently failing.