Created
December 23, 2023 19:19
-
-
Save tairosonloa/1e8b7181e29c087fe516a69369904965 to your computer and use it in GitHub Desktop.
Script to correct file and exif dates in photos from Facebook dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# This script will parse the JSON files from your Facebook dump and will copy the photos to a new folder | |
# with the correct date and time. It will also add the correct date and time to the EXIF data of the photo. | |
# | |
# Author: @tairosonloa at https://github.com/tairosonloa | |
# License: MIT | |
# Version: 1.0.0 | |
# Date: 2023-12-23 | |
# | |
# Usage: Execute this script with python3 in the root of the extracted zip file from the Facebook dump | |
# In that path, you should have a `your_activity_across_facebook/posts` folder with the JSON files and photos. | |
# Note: you might need to install third party libs (ftfy, piexif) with pip3 | |
import os | |
import json | |
import codecs | |
import ftfy | |
import shutil | |
import piexif | |
from datetime import datetime | |
# This code was executed on a MacOS. Be sure to change `parse_path` to an existing folder in your computer. | |
# There is where photos with correct data and names will appear | |
parse_path = '/Users/example/Pictures/facebook_dump/' # note the trailing slash | |
albums = 'your_activity_across_facebook/posts/album' | |
for album in os.listdir(albums): | |
with codecs.open(albums + '/' + album, 'r', encoding='ascii') as f: | |
data = json.load(f) | |
if len(data['photos']) > 0: | |
# Correct bad encoding | |
album_name = ftfy.ftfy(data['name']) | |
print(f'Found {len(data["photos"])} photos for album \"{album_name}\"') | |
# Create folder with the album name if it doesn't exist | |
if not os.path.exists(parse_path + album_name): | |
os.makedirs(parse_path + album_name) | |
for photo in data['photos']: | |
# Get the date from the photo. If exif data is not available, use the upload date of the photo | |
taken_date = photo.get('media_metadata', {}).get('photo_metadata', {}).get('exif_data', [{}])[0].get('taken_timestamp', None) | |
if taken_date is None: | |
taken_date = photo.get('creation_timestamp') | |
# Copy the photo to the new folder | |
destination_photo = parse_path + album_name + '/' + str(taken_date) + '.jpg' | |
shutil.copy(photo['uri'], destination_photo) | |
# Update EXIF data with the correct date | |
exif_dict = piexif.load(destination_photo) | |
new_date = datetime.fromtimestamp(taken_date).strftime("%Y:%m:%d %H:%M:%S") | |
exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date | |
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date | |
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date | |
exif_bytes = piexif.dump(exif_dict) | |
piexif.insert(exif_bytes, destination_photo) | |
# Update the file creation and modification date | |
os.utime(destination_photo, (taken_date, taken_date)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment